raffie-rest / adapter
Laravel 5 远程 REST 适配器
dev-master
2016-11-14 13:20 UTC
Requires
- php: >=5.5.0
- guzzlehttp/guzzle: 5.3.*
- illuminate/support: 5.1.*|5.2.*|5.3.*|5.4.*
This package is not auto-updated.
Last update: 2024-10-02 09:00:42 UTC
README
目录
简介
我主要基于 guzzle 构建了这个工具,旨在以更“抽象”的方式方便 Laravel 5 应用程序进行远程 REST 调用。在 L4 及之前版本中,有很多实用的包和抽象,它们似乎能很好地完成这项任务,尽管 L5 的过渡使得许多这些包与新的框架不兼容。
虽然我永远无法报答我对 Laravel 框架创造者的巨大债务,但我认为我能做到的至少是分享这个。
请注意,它仍在开发/错误测试中。任何帮助/反馈都非常欢迎
未来开发
集成 Google oAuth2 服务帐户身份验证
我已经在另一个自定义远程 REST 实现中设置了此功能,使用的是这个 自定义 JWT 工具,我想将其集成为此项目中 Guzzle 身份验证方法。
单元测试
它还没有准备好,因为它在很大程度上依赖于使用的 API。
开发用于映射到 Eloquent\Model
事件的基模型/特质
不确定这是否有必要
入门
将包添加到您的仓库
"raffie-rest/adapter": "dev-master"
注册服务提供者 - config/app.php
'Raffie\REST\Adapter\AdapterServiceProvider'
发布配置文件
php artisan vendor:publish
修改它 - config/rest_resources.php
'pushover_v1' => [
'data_type' => 'json',
'defaults' => [
'base_url' => 'https://api.pushover.net/1',
'defaults' => [
'query' => [
'token' => '', // Input your created app token here
'user' => '' // User / delivery group token
]
]
]
如你所见,就默认设置而言,它全部都是 Guzzle。
这就完成了!别急...
支持的方法
例如
Foo::get()
Foo::get(1)
Foo::get(1, 'addresses')
Foo::get(1, 'addresses', 2)
Foo::post([])
Foo::post(1, 'addresses' , [])
Foo::put(1, [])
Foo::put(1, 'addresses', 2, [])
Foo::delete(1)
Foo::delete(1, 'addresses', 2)
注意它与 Laravel 5 资源控制器 URI 格式的相似性吗?
同样的原则也适用于非静态。
支持的 HTTP 请求类型
- GET
- POST
- PUT
- DELETE
- HEAD
- OPTIONS
示例
从 Laravel 5 ShouldBeQueued,静态地
App\Commands\SendPushOver.php
:
use Raffie\REST\Adapter\Adapters\PushOver\v1\Message;
class SendPushover extends Command implements SelfHandling, ShouldBeQueued
{
use InteractsWithQueue, SerializesModels;
protected $message = [
'title' => 'Something went wrong',
'message' => 'Comrade Leader, something went wrong',
'url' => 'http://foo.com',
'url_title' => 'Foo',
'priority' => 0,
'sound' => 'bugle'
];
/**
* Create a new command instance.
*
* @return void
*/
public function __construct(array $data = [])
{
$this->message = $data;
//
}
/**
* Execute the command.
*
* @return void
*/
public function handle()
{
//
$message = Message::send($this->message);
return $message;
}
Foo.php
:
Queue::bulk([new SendPushover($message)]);
从控制器/命令,使用委托处理器
实现 DelegateInterface 并从这里开始
PushOverSend.php
:
use Raffie\REST\Adapter\Adapters\PushOver\v1\Message,
Raffie\REST\Adapter\Interfaces\DelegateInterface;
class PushOverSend extends Command implements DelegateInterface {
protected $name = 'pushover:send';
protected $description = 'Sends a test push';
protected $message = [
'title' => 'Something went wrong',
'message' => 'Comrade Leader, something went wrong',
'url' => 'http://foo.com',
'url_title' => 'Foo',
'priority' => 0,
'sound' => 'bugle'
];
public function fire()
{
$message = new Message($this);
return $message->post($this->message);
}
/**
* Request Succeeds
*
* @param mixed $data
* @return mixed
*/
public function requestSucceeds($data)
{
$this->info($data);
}
/**
* Request Fails
*
* @param Illuminate\Support\MessageBag $errors
* @return mixed
*/
public function requestFails(MessageBag $errors)
{
foreach($errors->all() as $error)
{
$this->error($error);
}
}
}
实现自己的远程 REST 端点
轻松简单,所有内容都继承自其抽象父类
Postcode.php
:
<?php namespace Raffie\REST\Adapter\Adapters;
use Raffie\REST\Adapter\Adapters\Base;
/*
|--------------------------------------------------------------------------
| Postcode.nl search implementation
|--------------------------------------------------------------------------
*/
class Postcode extends Base
{
public $resource = 'postcode'; // Corresponds to the rest_resources config key
public $relativePath = 'addresses'; // Set the relative path for your resource
/**
* Static stub to your regular GET data
*
* @param args - GET query string
*
* @return Postcode instance
*/
public static function search($postcode, $housenumber, $housenumber_addition = '')
{
return (new static)->get($postcode, $housenumber, $housenumber_addition);
}
}
确保您修改了相应的配置文件
config/rest_resources.php
:
'postcode' => [
'data_type' => 'json', // plain, xml, json
'defaults' => [ // guzzle defaults
'base_url' => 'https://api.postcode.nl/rest',
'defaults' => [
'auth' => ['user', 'pass']
]
]
],