Skip to content

Commit 1c97591

Browse files
Fix Add user agent to Curl (#235)
* feat:Add user agent to Curl * fix: unit test headers
1 parent 288af6a commit 1c97591

4 files changed

Lines changed: 17 additions & 4 deletions

File tree

src/Client/Application/ClientConfiguration.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
class ClientConfiguration
3232
{
33+
const VERSION = '3.0.0';
3334
private string $apiKey;
3435
private array $userAgentComponents = [];
3536
private array $config = [];
@@ -60,6 +61,7 @@ public function __construct(string $apiKey, ?Environment $environment = null, ar
6061
} catch (InvalidArgumentException $e) {
6162
$this->addError("Invalid configuration: " . $e->getMessage());
6263
}
64+
$this->initUserAgent();
6365
}
6466

6567
/**
@@ -216,4 +218,11 @@ public function hasError(): bool {
216218
public function getErrors(): array {
217219
return array_values($this->errors);
218220
}
221+
222+
private function initUserAgent()
223+
{
224+
$phpVersion = rtrim(str_replace(PHP_EXTRA_VERSION, '', PHP_VERSION), '-');
225+
$this->addUserAgentComponent('PHP', $phpVersion);
226+
$this->addUserAgentComponent('Alma for PHP', self::VERSION);
227+
}
219228
}

src/Client/Application/CurlClient.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535

3636
class CurlClient implements ClientInterface
3737
{
38-
const VERSION = '3.0.0';
3938

4039
private ClientConfiguration $config;
4140

src/Client/Application/Endpoint/AbstractEndpoint.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ public function __construct(ClientInterface $client)
3333
* @return Request The Request object
3434
*/
3535
private function createRequest(string $method, string $uri, array $body = []): Request {
36+
3637
$headers = [
38+
'User-Agent: ' . $this->client->getConfig()->getUserAgentString(),
3739
'Authorization' => ['Alma-Auth ' . $this->client->getConfig()->getApiKey()]
3840
];
3941
return new Request($method, $uri, $headers, json_encode($body));

tests/Client/Unit/Application/ClientConfigurationTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ClientConfigurationTest extends TestCase
1010
{
1111
/** @var ClientConfiguration */
1212
private ClientConfiguration $clientConfig;
13-
13+
1414
public function setUp(): void
1515
{
1616
parent::setUp();
@@ -169,6 +169,7 @@ public static function timeoutConfigDataProvider(): array
169169
]
170170
];
171171
}
172+
172173
/**
173174
* @dataProvider timeoutConfigDataProvider
174175
* @return void
@@ -196,8 +197,10 @@ public function testValidateVerify()
196197

197198
public function testAddUserAgentComponent()
198199
{
199-
$this->clientConfig->addUserAgentComponent('alma-php-sdk', '1.0.0');
200-
$this->assertEquals('alma-php-sdk/1.0.0', $this->clientConfig->getUserAgentString());
200+
$phpVersion = rtrim(str_replace(PHP_EXTRA_VERSION, '', PHP_VERSION), '-');
201+
202+
$this->clientConfig->addUserAgentComponent('wc-version', '1.0.0');
203+
$this->assertEquals(sprintf('wc-version/1.0.0; Alma for PHP/%s; PHP/%s', ClientConfiguration::VERSION, $phpVersion), $this->clientConfig->getUserAgentString());
201204
}
202205

203206
/**

0 commit comments

Comments
 (0)