Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for api token #18

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 16 additions & 5 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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`
*
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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);
}

Expand Down
60 changes: 60 additions & 0 deletions tests/Unit/Client/AuthenticateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Tests\Art4\Wegliphant\Client;

use Art4\Wegliphant\Client;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

#[CoversClass(Client::class)]
final class AuthenticateTest extends TestCase
{
public function testAuthenticateSetsCorrectHeader(): void
{
$expected = [];
$apiKey = 'c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2';

$request = $this->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();
}
}
Loading