Skip to content

Commit f5f977c

Browse files
authored
Merge pull request #36 from aternosorg/align-constructor
⚠️ !Breaking Change! Reorder constructor parameters, remove $userAgent
2 parents b9978f4 + ae1c116 commit f5f977c

4 files changed

Lines changed: 47 additions & 10 deletions

File tree

‎README.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Most endpoints are public. Some endpoints (e.g. the daily stats of projects and
4040
require an API key, which you can create in your [account settings](https://hangar.papermc.io/auth/settings/api-keys).
4141
If an API key is set, it is used for all requests. The client exchanges it for a JWT automatically.
4242
```php
43+
$hangarClient = new HangarAPIClient("your-api-key");
44+
// or
4345
$hangarClient->setApiKey("your-api-key");
4446
```
4547

@@ -67,13 +69,15 @@ try {
6769

6870
### Custom HTTP Client or Configuration
6971
You can pass your own [Guzzle](https://docs.guzzlephp.org/) client, e.g. to set timeouts or add middlewares,
70-
and your own `Aternos\HangarApi\Configuration`:
72+
and your own `Aternos\HangarApi\Configuration`.
73+
Each client creates its own configuration if none is passed, so changes to `Configuration::getDefaultConfiguration()`
74+
do not affect the client. A configuration passed to the client is used as is, so set your user agent on it.
7175
```php
7276
use Aternos\HangarApi\Configuration;
7377
use GuzzleHttp\Client;
7478

7579
$hangarClient = new HangarAPIClient(
76-
configuration: new Configuration(),
80+
configuration: new Configuration()->setUserAgent("my-app/1.0.0 (contact@example.com)"),
7781
httpClient: new Client(["timeout" => 10]),
7882
);
7983
// or

‎src/Client/HangarAPIClient.php‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,22 @@ class HangarAPIClient
7070

7171
protected PagesApi $pages;
7272

73+
/**
74+
* @param string|null $apiKey API key used for authentication, see {@see setApiKey()}
75+
* @param Configuration|null $configuration Configuration used for all requests. If null, a new configuration
76+
* with the default user agent of this library is created, see {@see getDefaultUserAgent()}.
77+
* @param ClientInterface|null $httpClient HTTP client used for all requests, see {@see setHttpClient()}
78+
*/
7379
public function __construct(
74-
?Configuration $configuration = null,
7580
?string $apiKey = null,
81+
?Configuration $configuration = null,
7682
?ClientInterface $httpClient = null,
77-
?string $userAgent = null,
7883
)
7984
{
8085
$this->apiKey = $apiKey;
8186
$this->httpClient = $httpClient;
8287
$this->setConfiguration($configuration ?? (new Configuration())
83-
->setUserAgent($userAgent ?? static::getDefaultUserAgent()));
88+
->setUserAgent(static::getDefaultUserAgent()));
8489
}
8590

8691
/**

‎tests/Unit/Client/ClientTestCase.php‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Aternos\HangarApi\Tests\Unit\Client;
44

55
use Aternos\HangarApi\Client\HangarAPIClient;
6+
use Aternos\HangarApi\Configuration;
67
use Aternos\HangarApi\Model\Pagination;
78
use GuzzleHttp\Client;
89
use GuzzleHttp\Handler\MockHandler;
@@ -35,14 +36,19 @@ public function setUp(): void
3536
* Create a client that answers every request with one of the given responses, in order,
3637
* and records all requests in {@see $requests}
3738
* @param Response[] $responses
38-
* @param string|null $userAgent user agent passed to the constructor of the client
39+
* @param string|null $apiKey API key passed to the constructor of the client
40+
* @param Configuration|null $configuration configuration passed to the constructor of the client
3941
* @return HangarAPIClient
4042
*/
41-
protected function createClient(array $responses = [], ?string $userAgent = null): HangarAPIClient
43+
protected function createClient(
44+
array $responses = [],
45+
?string $apiKey = null,
46+
?Configuration $configuration = null,
47+
): HangarAPIClient
4248
{
4349
$stack = HandlerStack::create(new MockHandler($responses));
4450
$stack->push(Middleware::history($this->requests));
45-
return new HangarAPIClient(httpClient: new Client(['handler' => $stack]), userAgent: $userAgent);
51+
return new HangarAPIClient($apiKey, $configuration, new Client(['handler' => $stack]));
4652
}
4753

4854
/**

‎tests/Unit/Client/HangarAPIClientTest.php‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,37 @@ public function testDefaultUserAgentIsSentWithRequests(): void
6262
/**
6363
* @throws ApiException
6464
*/
65-
public function testUserAgentFromConstructorReplacesDefaultUserAgent(): void
65+
public function testConfigurationFromConstructorIsUsed(): void
6666
{
67-
$client = $this->createClient([new Response(200, [], "1.0.0")], "test-agent/1.0");
67+
$configuration = new Configuration()
68+
->setHost("https://hangar.example.com")
69+
->setUserAgent("test-agent/1.0");
70+
$client = $this->createClient([new Response(200, [], "1.0.0")], configuration: $configuration);
6871

6972
$client->getLatestReleaseVersion("mclogs");
7073

74+
$this->assertEquals("hangar.example.com", $this->getRequest()->getUri()->getHost());
7175
$this->assertEquals("test-agent/1.0", $this->getRequest()->getHeaderLine("User-Agent"));
7276
}
7377

78+
/**
79+
* @throws ApiException
80+
*/
81+
public function testApiKeyFromConstructorIsUsedForAuthentication(): void
82+
{
83+
$client = $this->createClient([
84+
new Response(200, [], json_encode(["token" => "test-jwt", "expiresIn" => 3600])),
85+
new Response(200, [], "1.0.0"),
86+
], "test-api-key");
87+
88+
$client->getLatestReleaseVersion("mclogs");
89+
90+
$this->assertEquals("POST", $this->getRequest(0)->getMethod());
91+
$this->assertEquals("/api/v1/authenticate", $this->getRequest(0)->getUri()->getPath());
92+
$this->assertEquals(["apiKey" => "test-api-key"], $this->getQuery(0));
93+
$this->assertEquals("Bearer test-jwt", $this->getRequest(1)->getHeaderLine("Authorization"));
94+
}
95+
7496
/**
7597
* @throws ApiException
7698
*/

0 commit comments

Comments
 (0)