torann/api-client

一个可重复使用的基于远程服务的基API客户端。

0.2.1 2017-05-16 20:14 UTC

This package is auto-updated.

Last update: 2024-09-08 09:50:39 UTC


README

Latest Stable Version Total Downloads Patreon donate button Donate weekly to this project using Gratipay Donate to this project using Flattr Donate to this project using Paypal

一个可重复使用的基于远程服务的基API客户端。

安装

使用composer安装

$ composer require torann/api-client

创建客户端

安装完成后,我们需要创建一些客户端。首先,我们需要扩展 \BaseApiClient\Client 类并设置端点命名空间。

客户端

<?php

namespace App\BlogApi;

class Client extends \BaseApiClient\Client
{
    /**
     * Namespace for the endpoints
     *
     * @var string
     */
    protected $endpointNamespace = 'App\BlogApi\Endpoints';
}

$endpointNamespace 变量是端点命名空间的prefix。

端点

从端点进行API调用并返回模型。

<?php

namespace App\BlogApi\Endpoints;

use App\BlogApi\Models\Post;

use BaseApiClient\Endpoint;
use BaseApiClient\Models\Collection;

class Posts extends Endpoint
{
    /**
     * Get pages for the provided website.
     *
     * @param  array $params
     *
     * @return Collection
     * @throws \BaseApiClient\Exceptions\ApiException
     */
    public function index(array $params = [])
    {
        $response = $this->request->get('posts', $params);

        return new Collection($response, 'Post');
    }

    /**
     * Create a new post.
     *
     * @param  array $params
     *
     * @return Post
     * @throws \BaseApiClient\Exceptions\ApiException
     */
    public function create(array $params)
    {
        $response = $this->request->post('posts', $params);

        return new Post($response);
    }

    /**
     * Delete the provided post.
     *
     * @param  string $id
     *
     * @return bool
     * @throws \BaseApiClient\Exceptions\ApiException
     */
    public function delete($id)
    {
        $response = $this->request->delete(sprintf('posts/%s', $id));

        return $response->getResponseCode() === 200;
    }
}

模型

<?php

namespace App\BlogApi\Models;

use BaseApiClient\Models\Model;

class Post extends Model
{
    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = [
        'publish_at',
    ];
}

注册我们的客户端

<?php

namespace App\Providers;

use App\BlogApi\Client;
use App\AnotherApi\Client;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->registerBlogService();
        $this->registerAnotherService();
    }

    /**
     * Register blog manager services.
     *
     * @return void
     */
    public function registerBlogService()
    {
        $this->app->bind(Client::class, function () {
            return new Client([
                'domain' => 'http://some.fancy.ip/',
                'secret' => env('BLOG_MANAGER_API_SECRET'),
            ]);
        });
    }

    /**
     * Register blog manager services.
     *
     * @return void
     */
    public function registerAnotherService()
    {
        $this->app->bind(Client::class, function () {
            return new Client([
                'domain' => 'http://some.fancy.ip/',
                'secret' => env('ANOTHER_API_SECRET'),
            ]);
        });
    }
}

调用端点

以下是在控制器中使用我们的 \App\BlogApi\Client 的示例。

<?php

namespace App\Http\Controllers;

use App\BlogApi\Client;
use Illuminate\Http\Request;

class BlogController extends Controller
{
    /**
     * Blog manager client instance.
     *
     * @var \App\BlogApi\Client
     */
    protected $client;

    /**
     * Initializer constructor.
     *
     * @param Client $client
     */
    public function __construct(Client $client)
    {
        parent::__construct();

        $this->client = $client;
    }

    /**
     * Display the specified resource.
     *
     * @param  \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $posts = $this->client->posts->index($request->only('page'));

        return view('posts.index')->with([
            'posts' => $posts->paginate()
        ]);
    }

    /**
     * Display the specified resource.
     *
     * @param  Request $request
     * @param  string  $slug
     *
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request, $slug)
    {
        $post = $this->client->posts->find($slug);

        return view('posts.show')->with([
            'post' => $post
        ]);
    }
}

变更日志

0.2.0

  • 添加对Laravel 5.4的支持

0.1.4

  • 对于空值返回null

0.1.3

  • 添加对Laravel 5.3的支持

0.1.2

  • 删除尾部斜杠

0.1.1

  • 错误修复

0.1.0

  • 首次发布