Skip to content

Commit ae35794

Browse files
authored
feat: get-version-from-composer-and-pass-it-in-headers (#112)
1 parent 66435a6 commit ae35794

File tree

6 files changed

+97
-2
lines changed

6 files changed

+97
-2
lines changed

src/Flagsmith.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Flagsmith\Utils\IdentitiesGenerator;
1919
use Flagsmith\Utils\Mappers;
2020
use Flagsmith\Utils\Retry;
21+
use Flagsmith\Utils\UserAgent;
2122
use JsonException;
2223
use ValueError;
2324
use Psr\Http\Client\ClientInterface;
@@ -621,7 +622,8 @@ private function call(string $method, string $uri, array $body = [])
621622
->createRequest($method, rtrim($this->host, '/') . '/' . $uri)
622623
->withHeader('Accept', 'application/json')
623624
->withHeader('Content-Type', 'application/json')
624-
->withHeader('X-Environment-Key', $this->apiKey);
625+
->withHeader('X-Environment-Key', $this->apiKey)
626+
->withHeader('User-Agent', UserAgent::get());
625627

626628
if (!empty($this->customHeaders)) {
627629
foreach ($this->customHeaders as $name => $value) {

src/Utils/AnalyticsProcessor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function flush()
6767
->withHeader('Accept', 'application/json')
6868
->withHeader('Content-Type', 'application/json')
6969
->withHeader('X-Environment-Key', $this->environment_key)
70+
->withHeader('User-Agent', UserAgent::get())
7071
->withBody($stream);
7172

7273
try {

src/Utils/UserAgent.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Flagsmith\Utils;
4+
5+
/**
6+
* UserAgent utility class for generating SDK user agent strings.
7+
*/
8+
class UserAgent
9+
{
10+
/**
11+
* Get the user agent string for the SDK.
12+
*
13+
* @return string The user agent string in the format "flagsmith-php-sdk/{version}"
14+
*/
15+
public static function get(): string
16+
{
17+
try {
18+
$composerPath = __DIR__ . '/../../composer.json';
19+
$content = file_get_contents($composerPath);
20+
$data = json_decode($content, true);
21+
$version = $data['version'] ?? null;
22+
23+
if ($version) {
24+
return "flagsmith-php-sdk/{$version}";
25+
}
26+
} catch (\Exception $e) {
27+
// Silently fall through to default
28+
}
29+
30+
return 'flagsmith-php-sdk/unknown';
31+
}
32+
}

tests/AnalyticsTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,27 @@ public function testAnalyticsProcessorFlushClearsAnalyticsData()
3030

3131
public function testAnalyticsProcessorFlushPostRequestDataMatchAnanlyticsData()
3232
{
33+
$capturedRequest = null;
3334
$client = $this->createMock(ClientInterface::class);
3435

3536
$analyticsProcessor = ClientFixtures::analyticsProcessor($client);
3637
$client->expects($this->once())
37-
->method('sendRequest');
38+
->method('sendRequest')
39+
->with($this->callback(function ($request) use (&$capturedRequest) {
40+
$capturedRequest = $request;
41+
return true;
42+
}));
3843

3944
$analyticsProcessor->trackFeature('my_feature');
4045
$analyticsProcessor->flush();
46+
47+
$this->assertNotNull($capturedRequest);
48+
$this->assertTrue($capturedRequest->hasHeader('User-Agent'));
49+
$userAgent = $capturedRequest->getHeaderLine('User-Agent');
50+
$composerData = json_decode(file_get_contents(__DIR__ . '/../composer.json'), true);
51+
$expectedVersion = $composerData['version'] ?? 'unknown';
52+
$expectedUserAgent = "flagsmith-php-sdk/{$expectedVersion}";
53+
$this->assertEquals($expectedUserAgent, $userAgent);
4154
}
4255

4356
public function testAnalyticsProcessorFlushEarlyExitIfAnalyticsDataIsEmpty()

tests/FlagsmithClientTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use FlagsmithTest\Offline\FakeOfflineHandler;
99
use GuzzleHttp\Psr7\Response;
1010
use PHPUnit\Framework\TestCase;
11+
use Psr\Http\Client\ClientInterface;
1112
use Psr\Http\Message\StreamFactoryInterface;
1213

1314
class FlagsmithClientTest extends TestCase
@@ -449,4 +450,32 @@ public function testOfflineHandlerUsedAsFallbackForLocalEvaluation()
449450
$this->assertEquals($identityFlags->getFlag('some_feature')->enabled, true);
450451
$this->assertEquals($identityFlags->getFlag('some_feature')->value, 'some-value');
451452
}
453+
454+
public function testApiRequestsIncludeUserAgentHeader()
455+
{
456+
$capturedRequest = null;
457+
$mockClient = $this->createMock(ClientInterface::class);
458+
$mockClient->expects($this->once())
459+
->method('sendRequest')
460+
->with($this->callback(function ($request) use (&$capturedRequest) {
461+
$capturedRequest = $request;
462+
return true;
463+
}))
464+
->willReturn(
465+
new Response(200, ['Content-Type' => 'application/json'], file_get_contents(__DIR__ . '/Data/flags.json'))
466+
);
467+
468+
$flagsmith = (new Flagsmith('api_key'))
469+
->withClient($mockClient);
470+
471+
$flagsmith->getEnvironmentFlags();
472+
473+
$this->assertNotNull($capturedRequest);
474+
$this->assertTrue($capturedRequest->hasHeader('User-Agent'));
475+
$userAgent = $capturedRequest->getHeaderLine('User-Agent');
476+
$composerData = json_decode(file_get_contents(__DIR__ . '/../composer.json'), true);
477+
$expectedVersion = $composerData['version'] ?? 'unknown';
478+
$expectedUserAgent = "flagsmith-php-sdk/{$expectedVersion}";
479+
$this->assertEquals($expectedUserAgent, $userAgent);
480+
}
452481
}

tests/UserAgentTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use Flagsmith\Utils\UserAgent;
5+
6+
class UserAgentTest extends TestCase
7+
{
8+
public function testGetUserAgentReturnsVersionFromComposerJson()
9+
{
10+
$userAgent = UserAgent::get();
11+
12+
$composerPath = __DIR__ . '/../composer.json';
13+
$composerData = json_decode(file_get_contents($composerPath), true);
14+
$expectedVersion = $composerData['version'] ?? 'unknown';
15+
16+
$this->assertEquals("flagsmith-php-sdk/{$expectedVersion}", $userAgent);
17+
}
18+
}

0 commit comments

Comments
 (0)