Skip to content

Commit 9ac460d

Browse files
committed
Revert "reorder constructor parameters, remove userAgent parameter"
This reverts commit eed2bd3.
1 parent eed2bd3 commit 9ac460d

4 files changed

Lines changed: 10 additions & 48 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ 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
4543
$hangarClient->setApiKey("your-api-key");
4644
```
4745

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

7068
### Custom HTTP Client or Configuration
7169
You can pass your own [Guzzle](https://docs.guzzlephp.org/) client, e.g. to set timeouts or add middlewares,
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.
70+
and your own `Aternos\HangarApi\Configuration`:
7571
```php
7672
use Aternos\HangarApi\Configuration;
7773
use GuzzleHttp\Client;
7874

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

‎src/Client/HangarAPIClient.php‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,17 @@ 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-
*/
7973
public function __construct(
80-
?string $apiKey = null,
8174
?Configuration $configuration = null,
75+
?string $apiKey = null,
8276
?ClientInterface $httpClient = null,
77+
?string $userAgent = null,
8378
)
8479
{
8580
$this->apiKey = $apiKey;
8681
$this->httpClient = $httpClient;
8782
$this->setConfiguration($configuration ?? (new Configuration())
88-
->setUserAgent(static::getDefaultUserAgent()));
83+
->setUserAgent($userAgent ?? static::getDefaultUserAgent()));
8984
}
9085

9186
/**

‎tests/Unit/Client/ClientTestCase.php‎

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

55
use Aternos\HangarApi\Client\HangarAPIClient;
6-
use Aternos\HangarApi\Configuration;
76
use Aternos\HangarApi\Model\Pagination;
87
use GuzzleHttp\Client;
98
use GuzzleHttp\Handler\MockHandler;
@@ -36,19 +35,14 @@ public function setUp(): void
3635
* Create a client that answers every request with one of the given responses, in order,
3736
* and records all requests in {@see $requests}
3837
* @param Response[] $responses
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
38+
* @param string|null $userAgent user agent passed to the constructor of the client
4139
* @return HangarAPIClient
4240
*/
43-
protected function createClient(
44-
array $responses = [],
45-
?string $apiKey = null,
46-
?Configuration $configuration = null,
47-
): HangarAPIClient
41+
protected function createClient(array $responses = [], ?string $userAgent = null): HangarAPIClient
4842
{
4943
$stack = HandlerStack::create(new MockHandler($responses));
5044
$stack->push(Middleware::history($this->requests));
51-
return new HangarAPIClient($apiKey, $configuration, new Client(['handler' => $stack]));
45+
return new HangarAPIClient(httpClient: new Client(['handler' => $stack]), userAgent: $userAgent);
5246
}
5347

5448
/**

‎tests/Unit/Client/HangarAPIClientTest.php‎

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Aternos\HangarApi\Client\Project;
1717
use Aternos\HangarApi\Client\User;
1818
use Aternos\HangarApi\Client\Version;
19-
use Aternos\HangarApi\Configuration;
2019
use Aternos\HangarApi\Model\Category;
2120
use Aternos\HangarApi\Model\Platform;
2221
use Aternos\HangarApi\Model\ProjectMember;
@@ -58,37 +57,15 @@ public function testDefaultUserAgentIsSentWithRequests(): void
5857
/**
5958
* @throws ApiException
6059
*/
61-
public function testConfigurationFromConstructorIsUsed(): void
60+
public function testUserAgentFromConstructorReplacesDefaultUserAgent(): void
6261
{
63-
$configuration = new Configuration()
64-
->setHost("https://hangar.example.com")
65-
->setUserAgent("test-agent/1.0");
66-
$client = $this->createClient([new Response(200, [], "1.0.0")], configuration: $configuration);
62+
$client = $this->createClient([new Response(200, [], "1.0.0")], "test-agent/1.0");
6763

6864
$client->getLatestReleaseVersion("mclogs");
6965

70-
$this->assertEquals("hangar.example.com", $this->getRequest()->getUri()->getHost());
7166
$this->assertEquals("test-agent/1.0", $this->getRequest()->getHeaderLine("User-Agent"));
7267
}
7368

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

0 commit comments

Comments
 (0)