auto1-oss/service-api-client-bundle

适用于PHP项目的Auto1 Service API客户端

v2.7.1 2024-08-19 12:54 UTC

README

此捆绑包使用 php-http/httplug 客户端抽象。因此,您需要将一些psr7兼容的客户端安装到您的项目中,以便使用此捆绑包。更多详细信息: php-http/httplug 客户端和适配器

config.yml

auto1_service_api_client:
    request_visitors:
        - '@visitor1'
        - '@visitor2'
    strict_mode: false
  • request_visitors - 请求访问者(RequestVisitorInterface)旨在修改您的请求,例如添加自定义头。您还可以使用 'auto1.api.request_visitor' 标记服务以使其成为访问者。注意!通过设置此配置,您将覆盖默认值!
  • strict_mode - 布尔值,默认为 false。如果为 true,则请求工厂会忽略GET、HEAD、OPTIONS和TRACE HTTP方法的任何请求体。换句话说,客户端将始终发送不带主体的此类请求。

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 的使用