germania-kg / downloadsapi-client
API Client for Germania's Downloads API
Requires
- php: ^7.4|^8.0
- germania-kg/response-decoder: ^1.0
- psr/cache: ^1.0|^2.0|^3.0
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/log: ^1.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- guzzlehttp/guzzle: ^7.0
- laminas/laminas-log: ^2.13
- nyholm/psr7: ^1.3
- php-coveralls/php-coveralls: ^2.0
- phpspec/prophecy: ^1.16
- phpspec/prophecy-phpunit: ^2.0
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9.0
- spatie/phpunit-watcher: ^1.0
- symfony/cache: ^5.0
Suggests
- guzzlehttp/guzzle: PSR-18 client (as of v7)
- nyholm/psr7: PSR-17 factories
- dev-master
- 5.1.0
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 4.1.2
- 4.1.1
- 4.1.0
- 4.0.1
- 4.0.0
- 3.0.10
- 3.0.9
- 3.0.8
- 3.0.7
- 3.0.6
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.1.1
- 2.1.0
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.0.2
- 1.0.1
- 1.0.0
- dev-dependabot/composer/nyholm/psr7-1.6.1
- dev-dependabot/composer/guzzlehttp/psr7-2.5.0
- dev-develop
- dev-dependabot/composer/guzzlehttp/guzzle-7.4.5
This package is auto-updated.
Last update: 2024-11-21 23:52:10 UTC
README
DownloadsApi Client
Server-side PHP client for retrieving a list of available downloads from Germania's Downloads API.
Installation
The v5 release is a complete rewrite, so there is no “upgrading” procedure.
$ composer require germania-kg/downloadsapi-client $ composer require germania-kg/downloadsapi-client:^5.0
This package requires a PSR-18 HTTP client implementation and a PSR-17 HTT factory implementation. Suggestions are Guzzle 7 via guzzlehttp/guzzle and Nyholm's nyholm/psr7 which (despite its name) provides the PSR-17 factories as well:
$ composer require nyholm/psr7 $ composer require guzzlehttp/guzzle
Basics
Interface and abstract class
Interface DownloadsApiInterface provides public methods for retrieving documents, all and latest and request, with the latter for internal use. All of these return an iterable. There are also interceptors for the authentication key:
// Provided by interface // Germania\DownloadsApi\DownloadsApiInterface public function all() : iterable; public function latest() : iterable; public function request( string $path ) : iterable ; public function setAuthentication( ?string $key ) : DownloadsApiInterface; public function getAuthentication( ) : string;
Abstract class DownloadsApiAbstract prepares the all and latest methods to delegate directly to the request method. It also utilizes various useful traits such as Psr\Log\LoggerAwareTrait
, LoglevelTrait
and AuthenticationTrait
. – So any class extending this abstract will thus provide:
// Inherited from abstract class // Germania\DownloadsApi\DownloadsApiAbstract $api->setLogger( \Monolog\Logger( ... ) ); $api->setErrorLoglevel( \LogLevel::ERROR ); $api->setSuccessLoglevel( \LogLevel::INFO ); $api->setAuthentication( "secrete" ); $api->getAuthentication(); // "secret"
PSR-6 Cache Support
Class CacheDownloadsApiDecorator wraps an existing DownloadsApi instance and adds support for PSR-6 Caches. It extends DownloadsApiDecorator which itself extends DownloadsApiAbstract, so the class also implements DownloadsApiInterface.
The constructor requires a DownloadsApi (or DownloadsApiInterface) instance and a PSR-6 Cache Item Pool. You may optionally pass a cache lifetime in seconds which defaults to 14400 (4 hours).
<?php use Germania\DownloadsApi\DownloadsApi; use Germania\DownloadsApi\CacheDownloadsApiDecorator; use Symfony\Component\Cache\Adapter\FilesystemAdapter; $api = new DownloadsApi( ... ); $psr6 = new FilesystemAdapter( ... ); $cached_api = new CacheDownloadsApiDecorator( $api, $psr6); $cached_api = new CacheDownloadsApiDecorator( $api, $psr6, 14400);
The API client
The DownloadsApi API client extends DownloadsApiAbstract und thus implements DownloadsApiInterface. The constructor requires a PSR-18 HTTP Client, a PSR-17 Request factory and an API key. – To obtain an API key, ask the web developers over at Germania KG.
<?php use Germania\DownloadsApi\DownloadsApi; use GuzzleHttp\Client; use Nyholm\Psr7\Factory\Psr17Factory; $psr18 = new Client; $psr17 = new Psr17Factory; $key = "secret" $api = new DownloadsApi($psr18, $psr17, $key);
Retrieve documents
Public methods all and latest return an Array iterable with the documents provided by Germania's Documents API.
$downloads = $api->latest(); $downloads = $api->all(); foreach( $downloads as $document): print_r( $document ); endforeach;
Example record
The above print_r( $document )
will reveal something like this:
Array (
[company] =>
[brand] => mybrand
[title] => Brand<sup>®</sup> Window Styling
[subtitle] =>
[subtitle2] =>
[description] =>
[picture] => Array (
[src] =>
[fallback] =>
)
[language] => de
[download] => https://download.example.com/document.pdf
[categories] => Array(
[0] => montageanleitung
)
[keywords] => Array ()
[products] => Array (
[0] => dachflaechen
[1] => plissee
[2] => duette
[3] => jalousie
[4] => rollo
)
[fileSize] => 2298631
[lastModified] => Array (
[date] => 2018-02-19 10:21:14.000000
[timezone_type] => 2
[timezone] => GMT
)
[daysAgo] => 420
)
Filtering results
To narrow down the results, both the all and latest methods accept an array with filter values. The fiter values may contain multiple values, separated with comma.
Think of the filter array items as WHERE … AND…
clauses, and comma-separated values as 'a' OR 'b'
$filters = array( 'company' => "ACME", 'brand' => "mybrand", 'category' => "brochure", 'language' => "en", 'keyword' => "customers,retailers", 'product' => "cars,bikes" ); $downloads = $api->latest($filters); $downloads = $api->all($filters);
Errors and Exceptions
It should be plenty to watch out for \Germania\DownloadsApi\Exceptions\DownloadsApiExceptionInterface
since all concrete exception classes implement this interface.
<?php use Germania\DownloadsApi\DownloadsApiExceptionInterface; use Germania\DownloadsApi\DownloadsApiRuntimeException; use Germania\DownloadsApi\DownloadsApiResponseException; use Germania\DownloadsApi\DownloadsApiUnexpectedValueException; try { $client->latest(); } catch (\Germania\DownloadsApi\DownloadsApiExceptionInterface $e) { echo $e->getMessage(); }
Exceptions during request:
Whenever a PSR-18 client request fails, a DownloadsApiRuntimeException will be thrown. This class implements DownloadsApiExceptionInterface
and extends \RuntimeException
.
HTTP Error responses:
When a API call returns an HTTP error response, a DownloadsApiResponseException will be thrown. This class implements DownloadsApiExceptionInterface
and extends \RuntimeException
.
Unexpected values in response:
Whenever a response (even with status 200 OK) can't be decoded to an useful array, a DownloadsApiUnexpectedValueException will be thrown. This class implements DownloadsApiExceptionInterface
and extends \UnexpectedValueException
.
Unit tests and development
Copy phpunit.xml.dist
to phpunit.xml
and fill in the Authentication data you obtained from Germania.
AUTH_TOKEN
when you've got an auth token for the Downloads API at hand.- Otherwise, fill in
AUTH_API
,AUTH_USER
, andAUTH_PASS
. The auth token will then be fetched from Germania's Auth API before each test.
In order to run unit tests, run PhpUnit like this:
$ composer test # or $ vendor/bin/phpunit