yusefauto1/service-api-client-bundle

PHP项目使用的Auto1服务API客户端

v1.0.6 2019-11-18 08:53 UTC

This package is not auto-updated.

Last update: 2024-10-01 07:02:36 UTC


README

该捆绑包使用php-http/httplug客户端抽象。因此,您需要在项目中安装一些psr7兼容的客户端,以便使用此捆绑包。更多详情:[php-http/httplug客户端和适配器](http://docs.php-http.org/en/latest/clients.html)。

config.yml

auto1_service_api_client:
    request_visitors:
        - '@visitor1'
        - '@visitor2'

请求访问者(RequestVisitorInterface)旨在修改您的请求,例如添加自定义头。您还可以使用'auto1.api.request_visitor'标签来使服务成为访问者。警告!通过设置此配置,您将覆盖默认值!

EP定义示例(yaml)

postUnicorn:
    method:        'POST'
    baseUrl:       'http://google.com'
    path:          '/v1/unicorn'
    requestFormat: 'url'
    requestClass:  'Auto1\ServiceDTOCollection\Unicorns\Request\PostUnicorn'
    responseClass: 'Auto1\ServiceDTOCollection\Unicorns\Response\Unicorn'

listUnicorns:
    method:        'GET'
    baseUrl:       'http://google.com'
    path:          '/v1/unicorns'
    requestFormat: 'json'
    requestClass:  'Auto1\ServiceDTOCollection\Unicorns\Request\SearchUnicorns'
    responseClass: 'Auto1\ServiceDTOCollection\Unicorns\Response\Unicorn[]'

ServiceRequest实现示例

class PostUnicorn implements ServiceRequestInterface
{
    private $horn;

    public function setHorn(string $horn): self
    {
        $this->horn = $horn;

        return $this;
    }

    public function getHorn()
    {
        return $this->horn;
    }
}

Repository实现示例

class UnicornRepository
{
    public function __construct(APIClientInterface $apiClient,)
    {
        $this->apiClient = $apiClient;
    }

    /**
     * @param string $horn
     *
     * @return Unicorn[]
     */
    public function getListByHorn(string $horn): array
    {
        $serviceRequest = (new GetUnicornsByHornRequest())->setHorn($horn);

        return $this->apiClient->send($serviceRequest);
    }
}

更多信息 - 请查看[service-api-components-bundle使用](https://github.com/auto1-oss/service-api-components-bundle)。