From b2b1a334b2dd79945f22f3895aa907777783506a Mon Sep 17 00:00:00 2001 From: Art4 Date: Wed, 6 Mar 2024 14:59:29 +0100 Subject: [PATCH 1/2] Add method Client::authenticate() --- src/Client.php | 21 ++++++--- tests/Unit/Client/AuthenticateTest.php | 60 ++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 tests/Unit/Client/AuthenticateTest.php diff --git a/src/Client.php b/src/Client.php index 9447835..47e8ff5 100644 --- a/src/Client.php +++ b/src/Client.php @@ -21,6 +21,18 @@ public static function create( private string $apiUrl = 'https://www.weg.li'; + private string $apiKey = ''; + + private function __construct( + private ClientInterface $httpClient, + private RequestFactoryInterface $requestFactory, + ) {} + + public function authenticate(string $apiKey): void + { + $this->apiKey = $apiKey; + } + /** * List all districts using the endpoint `GET /districts.json` * @@ -78,11 +90,6 @@ public function listCharges(): array return $this->parseJsonResponseToArray($response); } - private function __construct( - private ClientInterface $httpClient, - private RequestFactoryInterface $requestFactory, - ) {} - /** * @throws \Psr\Http\Client\ClientExceptionInterface If an error happens while processing the request. */ @@ -93,6 +100,10 @@ private function sendJsonRequest( $request = $this->requestFactory->createRequest($method, $this->apiUrl . $path); $request = $request->withHeader('Accept', 'application/json'); + if ($this->apiKey !== '') { + $request = $request->withHeader('X-API-KEY', $this->apiKey); + } + return $this->httpClient->sendRequest($request); } diff --git a/tests/Unit/Client/AuthenticateTest.php b/tests/Unit/Client/AuthenticateTest.php new file mode 100644 index 0000000..977febd --- /dev/null +++ b/tests/Unit/Client/AuthenticateTest.php @@ -0,0 +1,60 @@ +createMock(RequestInterface::class); + $request->expects($this->exactly(2))->method('withHeader')->willReturnMap([ + ['Accept', 'application/json', $request], + ['X-API-KEY', $apiKey, $request], + ]); + + $requestFactory = $this->createMock(RequestFactoryInterface::class); + $requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/districts.json')->willReturn($request); + + $stream = $this->createConfiguredMock( + StreamInterface::class, + [ + '__toString' => json_encode($expected), + ], + ); + + $response = $this->createConfiguredMock( + ResponseInterface::class, + [ + 'getStatusCode' => 200, + 'getHeaderLine' => 'application/json', + 'getBody' => $stream, + ] + ); + + $httpClient = $this->createMock(ClientInterface::class); + $httpClient->expects($this->exactly(1))->method('sendRequest')->willReturn($response); + + $client = Client::create( + $httpClient, + $requestFactory, + ); + $client->authenticate($apiKey); + + $response = $client->listDistricts(); + } +} From 2952006da3d9a8cf5623103d384a3cbc1978d8d7 Mon Sep 17 00:00:00 2001 From: Art4 Date: Wed, 6 Mar 2024 15:28:31 +0100 Subject: [PATCH 2/2] Update README.md and CHANGELOG.md --- CHANGELOG.md | 1 + README.md | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89705a9..5efbe54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- New method `Art4\Wegliphant\Client::authenticate()` to set your API key for authorized API requests. - New class `Art4\Wegliphant\Exception\UnexpectedResponseException` that will be thrown if an error happens while processing the response. ### Changed diff --git a/README.md b/README.md index 022686b..909bb1c 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,14 @@ $client = \Art4\Wegliphant\Client::create( ); ``` +Optionally, you can use `authenticate()` to set an API key. Some areas of the weg.li API require an API key. +Without the API key, all requests are sent without authorization. +You can find your API key [here](https://www.weg.li/user/edit). + +```php +$client->authenticate($apiKey); +``` + ### List all districts ```php