incraigulous/rest-repositories
Composer 安装命令:
composer require incraigulous/rest-repositories
包简介
Fetch data from any rest API with a common interface.
README 文档
README
Fetch data from any rest API with a common interface. Api results are returned wrapped in easy-to-work-with, fluent collection and object wrappers.
In a nutshell
You should be able to fetch data from any webservice and then access it using the following API:
$posts = PostsRepository::all()->first()->title;
or
$post = PostsRepository::find(1)->author->image->url;
This keeps you from having to create a separate mental map for each web service you work with.
Repositories
It's the job of the repository to abstract working with your webservice to a common interface. Extend Incraigulous\RestRepositories\Single, Incraigulous\RestRepositories\Listing or Incraigulous\RestRepositories\Resource for minimal setup.
Resources
A resource is for API resources with full read/write capability. The resource object has the following methods:
public static function get($params = []);
public static function update($id, $params = []);
public static function all();
public static function find($id);
public static function create($payload);
public static function delete($payload);
To create a new resource extend Incraigulous\RestRepositories\Resource:
For example:
use Incraigulous\RestRepositories\Resource; class PostsResource extends Resource { static $resource = 'posts' public static function sdk() { return new YourSdk(); } }
Listings
A listing is for API resources with full read only capability. The resource object has the following methods:
public static function get($params = []);
public static function all();
public static function find($id);
To create a new listing extend Incraigulous\RestRepositories\Listing:
Singles
A single is for API resources for a single listing endpoint. The resource object has only the get method.:
public static function get($params = []);
To create a new single extend Incraigulous\RestRepositories\Single:
Creating a base repository for a webservice
//This is just an example, you should create your own SDK implementation
use Incraigulous\RestRepositories\Sdks\JsonPlaceholderSdk;
use Incraigulous\RestRepositories\Listing;
class JsonPlaceholderBaseRepository extends Listing
{
public static function sdk() {
return new JsonPlaceholderSdk();
}
}
Creating a repository
class JsonPlaceholderPostsRepository extends JsonPlaceholderBaseRepository
{
//This will be passed to your SDK as the resource. This is usually the last part of the URL.
public static $resource = 'posts';
//In case you need to set default paramaters on all requests from repository.
protected static function defaultParams()
{
return [
'somefilter' => 'fromapi'
]
}
}
$posts = JsonPlaceholderPostsRepository::all();
Getting the original data
Responses are wrapped in Collection and Object wrappers by default and data keys are stripped out. To access response meta data like pagination or links, you can retrieve the original object like so:
PostRepository::all()->getOriginal()['page'];
//Or
PostRepository::all()->first()->getOriginal()['links'];
//Or
PostRepository::all()->first()->author->getOriginal()['links'];
SDKs
It's the job of an SDK class to make requests to a webservice. SDKs should implement Incraigulous\RestRepositories\Contracts\SdkInterface. To make this easy, a base Incraigulous\RestRepositories\Sdks\HttpSdk class is provided.
Using the HttpSdk class on the fly
Without authentication
$sdk = new HttpSdk('https://jsonplaceholder.typicode.com/');
$result = $sdk->post('posts', ['title' => 'foo', 'body' => 'bar', 'userId' => 1]);
With authentication
$sdk = new HttpSdk('http://example.com/api/', ['headers' => ['Authorization' => 'password123']]);
$result = $sdk->post('posts', ['title' => 'foo', 'body' => 'bar', 'userId' => 1]);
Even Better: Creating your own SDK class
Without authentication
<?php
use Incraigulous\RestRepositories\Sdks\HttpSdk;
class JsonPlaceholderSdk extends HttpSdk
{
protected $endpoint = 'https://jsonplaceholder.typicode.com/';
}
$sdk = new JsonPlaceholderSdk();
$result = $sdk->post('posts', ['title' => 'foo', 'body' => 'bar', 'userId' => 1]);
With authentication
<?php
use Incraigulous\RestRepositories\Sdks\HttpSdk;
class ExampleSdk extends HttpSdk
{
protected $endpoint = 'http://example.com/api/';
protected function defaultHeaders()
{
return ['Authorization' => $however->you->get->api->key];
}
}
$sdk = new ExampleSdk();
$result = $sdk->post('posts', ['title' => 'foo', 'body' => 'bar', 'userId' => 1]);
Testing
/vendor/bin/phpunit
incraigulous/rest-repositories 适用场景与选型建议
incraigulous/rest-repositories 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 734 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 incraigulous/rest-repositories 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 incraigulous/rest-repositories 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 734
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-03-15