lendable / dvla-vehicle-enquiry-api-client
DVLA车辆查询服务API的客户端
Requires
- php: >=7.4.0
- ext-json: *
- beberlei/assert: ^3.2
- nyholm/psr7: ^1.2
- paragonie/hidden-string: ^1.0 || ^2.0
- psr/http-client: ^1.0.1
- psr/http-message: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- guzzlehttp/guzzle: ^6.0|^7.0
- php-parallel-lint/php-parallel-lint: ^1.2
- phpstan/phpstan: ^1.0
- phpstan/phpstan-beberlei-assert: ^1.0
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpstan/phpstan-strict-rules: ^1.0
- phpunit/phpunit: ^9.5
- rector/rector: ^0.13
This package is auto-updated.
Last update: 2024-09-23 07:57:30 UTC
README
PHP客户端实现,用于DVLA车辆查询API v1。此包提供
- 支持PSR-18 HTTP客户端的API客户端
- 基于令牌的身份验证
- 用于请求构建和响应的值对象
安装
您可以通过Composer安装此库。
composer require lendable/dvla-vehicle-enquiry-api-client
使用方法
Client类实现了DVLA的REST API,并可以返回车辆范围,可用于请求车辆详细信息。
为了实例化Client类,我们需要注入装饰器,这将为API密钥身份验证添加PSR-18兼容层。我们还需要定义API的基础URI,以便轻松在UAT和实时服务之间切换。
基础URI
API的规范包含UAT和实时URL。给定的URI不应该以斜杠(/
)结尾,并且应包含/v1
路径。
例如:https://uat.driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1
客户端接受任何PSR-7 UriInterface实现的URI。
身份验证
ApiKeyAuthHttpClientDecorator将为请求添加所需的API令牌身份验证头。用于存储令牌的ApiKey值对象可以避免意外泄露。
HTTP客户端
使用Psr18ClientDecorator,您可以使用任何支持PSR-18标准的HTTP客户端来执行预构建的HTTP请求。
如果您想使用不支持PSR-18标准的HTTP客户端,您可以使用简单的装饰器,通过PSR-18 RequestInterface格式请求数据调用HTTP客户端,并将HTTP客户端的响应转换为PSR-18 ResponseInterface格式的响应。
例如,在我们的集成测试中,我们使用GuzzleClient与装饰器一起使用,该装饰器使用此PSR-18转换。
示例请求
<?php declare(strict_types=1); use Lendable\Dvla\VehicleEnquiry\Auth\ApiKeyAuthHttpClientDecorator; use Lendable\Dvla\VehicleEnquiry\Auth\ValueObject\ApiKey; use Lendable\Dvla\VehicleEnquiry\Client; use Lendable\Dvla\VehicleEnquiry\Psr18ClientDecorator; use Lendable\Dvla\VehicleEnquiry\Scope\VehiclesScope\Request\EnquiryRequest; use Lendable\Dvla\VehicleEnquiry\Scope\VehiclesScope\ValueObject\RegistrationNumber; use Nyholm\Psr7\Uri; $client = new Client( new ApiKeyAuthHttpClientDecorator( new Psr18ClientDecorator( new YourPsr18HttpClient() ), ApiKey::fromString('YOUR-AUTHENTICATION-TOKEN') ), new Uri('https://uat.driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1') ); $vehicleDetails = $client->vehicles()->enquireDetails( EnquiryRequest::with(RegistrationNumber::fromString('AA19PPP')) );
这会使用AA19PPP
注册号发出API请求,而变量$vehicleDetails
将包含一个EnquiryResponse对象,其中包含所有返回的API响应数据作为值对象。
本示例使用UAT API URL和测试登记号。测试登记号在DVLA车辆查询服务API文档中可用于不同测试用例的模拟响应。