vasildakov / speedy
Speedy API Client
Requires
- php: >=8.1
- ext-curl: *
- ext-json: *
- ext-xsl: *
- beberlei/assert: ^3.3
- doctrine/cache: ^1.13
- doctrine/collections: ^2.1
- fig/http-message-util: ^1.1
- guzzlehttp/guzzle: ^7.7
- guzzlehttp/psr7: ^2.5
- jms/serializer: ^3.25
- laminas/laminas-diactoros: ^3.0
- laminas/laminas-hydrator: ^4.14
- laminas/laminas-serializer: ^2.14
- php-http/curl-client: ^2.3
- php-http/discovery: ^1.14
- php-http/message: ^1.13
- vlucas/phpdotenv: ^5.5
Requires (Dev)
- friendsofphp/php-cs-fixer: dev-master
- php-coveralls/php-coveralls: dev-master
- phpmd/phpmd: dev-master
- phpunit/phpunit: ^9.5
- psalm/plugin-phpunit: dev-master
- squizlabs/php_codesniffer: 4.0.x-dev
- symfony/var-dumper: ^6.4
- theseer/phpdox: dev-master
- vimeo/psalm: 4.x-dev
- dev-main
- 1.0.0-alpha.13
- 1.0.0-alpha.12
- 1.0.0-alpha.11
- 1.0.0-alpha.10
- 1.0.0-alpha.9
- 1.0.0-alpha.8
- 1.0.0-alpha.7
- 1.0.0-alpha.6
- 1.0.0-alpha.5
- 1.0.0-alpha.4
- 1.0.0-alpha.3
- 1.0.0-alpha.2
- 1.0.0-alpha.1
- dev-30-calculation-request
- dev-develop
- dev-28-track-request
- dev-26-find-complex-request
- dev-24-find-office-request
- dev-22-find-street-request
- dev-feature/18-find-state-request
- dev-circleci-project-setup
This package is auto-updated.
Last update: 2024-12-16 22:08:21 UTC
README
An easy to use PHP client for Speedy REST API
Speedy client is a PSR-7 and PSR-18 compliant HTTP client that implements Speedy communication protocol. It has clean and consistent API, is fully unit tested and even comes with an example application to get you started.
Features
This library is compliant with PSR-7: HTTP message interfaces, PSR-17: HTTP Factories and PSR-18: HTTP Client
Installation
Using Composer:
$ composer require vasildakov/speedy
Usage
The Configuration
Let's presume that you are using PHP dotenv to load environment variables
from a file named .env
. In this case, you need to add the following variables:
SPEEDY_USERNAME="username"
SPEEDY_PASSWORD="password"
SPEEDY_LANGUAGE="EN"
The next step is to create a new Configuration instance like in the example bellow:
<?php // configuration $configuration = new Configuration( username: $_ENV['SPEEDY_USERNAME'], password: $_ENV['SPEEDY_PASSWORD'], language: $_ENV['SPEEDY_LANGUAGE'] );
Configuring Speedy Client
The final step is to configure the Speedy client.
The client can be configured with any PSR-18 HTTP Client
and PSR-17 HTTP Factory
:
Example with Guzzle and Laminas Diactoros
<?php use GuzzleHttp\Client; use Laminas\Diactoros\RequestFactory; $client = new Client(); // PSR-18 HTTP Client $factory = new RequestFactory(); // PSR-17 HTTP Factory $speedy = new Speedy($configuration, $client, $factory);
Example with Symfony HTTP Client and Nyholm HTTP Factory
<?php use Nyholm\Psr7\Factory\Psr17Factory; use Symfony\Component\HttpClient\Psr18Client; $client = new Psr18Client(); // PSR-18 HTTP Client $factory = new Psr17Factory(); // PSR-17 HTTP Factory $speedy = new Speedy($configuration, $client, $factory);
Making a Request
Once you have configured the client, you can proceed to make your first request. By default, each method returns the data in JSON, which can then be utilized as a simple PHP array or deserialized into the PHP model.
<?php // use an array $request = new GetContractClientsRequest(clientSystemId: "1234567"); $json = $speedy->getContractClient($request); $array = json_decode($json, true);
Processing the Response
The client API always returns the raw JSON response received from the endpoint. The JSON can be used as it is, decoded into a PHP associative array, or deserialized into a model object.
Deserialization can be achieved in two different ways: 1) by using the serializer, or 2) by decorating the original Speedy client with the SpeedyModelDecorator.
Using serializer:
<?php $json = $speedy->getContractClient($request); # json $serializer = (new SerializerFactory())(); # JMS\Serializer\SerializerInterface $response = $serializer->deserialize( data: $json, type: GetContractClientsResponse::class, format: 'json' ); # GetContractClientsResponse
Instead of calling the serializer every time, you can enhance the original Speedy client by decorating it with the SpeedyModelDecorator. This enhancement makes the responses more convenient, predictable and easy to use.
<?php $decorator = new SpeedyModelDecorator( new Speedy($configuration, $client, $factory) ); /** @var GetContractClientsResponse $response */ $response = $decorator->getContractClient(new GetContractClientsRequest());
Using the model
<?php // @var ArrayCollection $collection $collection = $response->getClients(); foreach ($collection as $client) { dump($client); # Model\Client dump($client->getClientName()); dump($client->getAddress()); # Model\Address dump($client->getAddress()->getSiteName()); # string dump($client->getAddress()->getPostcode()); # string }
Documentation
TBC
License
Code released under the MIT license
Speedy REST API examples
Speedy Web API Integration