lendable / dvla-vehicle-enquiry-api-client
API client for the DVLA's Vehicle Enquiry Service API
Installs: 38 399
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 39
Forks: 2
Open Issues: 2
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-11-18 23:32:27 UTC
README
PHP client implementation for the DVLA Vehicle Enquiry API v1. This package provides:
- API client that supports PSR-18 HTTP clients
- token-based authentication
- value objects for the request building and for the response
Installation
You can install the library via Composer.
composer require lendable/dvla-vehicle-enquiry-api-client
Usage
The Client class implements the DVLA's REST API and can return the vehicles scope which can be used to request the vehicle details.
For the instantiation of the Client class we need to inject the decorators which adds the API key authentication, and the PSR-18 compatibility layers. Also, we need to define the API's base URI to easily switch between UAT and live service.
Base URI
The API's specification contains the UAT and live URL.
The given URI should not end with slash (/
) and should contain the /v1
path too.
For example: https://uat.driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1
The client accepts the URI in any PSR-7 UriInterface implementation.
Authentication
The ApiKeyAuthHttpClientDecorator adds the required API token authentication headers to the requests. The ApiKey value object keeps the token secret, avoiding accidental exposure.
HTTP client
With the Psr18ClientDecorator you can use any HTTP client which supports the PSR-18 standard to perform the prebuilt HTTP request.
If you prefer to use an HTTP client that doesn't support the PSR-18 standard, you can alternatively make a simple decorator that calls the HTTP client using the PSR-18 RequestInterface format request data and convert the HTTP client's response to a PSR-18 ResponseInterface format response.
For example in our integration test we are using GuzzleClient with a decorator which using this PSR-18 conversion.
Example request
<?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')) );
This makes an API request with the AA19PPP
registration number and the $vehicleDetails
variable will contain an
EnquiryResponse object
which contains all the returned API response data in value objects.
This example is using the UAT API URL and a test registration number. Test registration numbers for mock responses of the different test cases are available in the DVLA Vehicle Enquiry Service API documentation.