Skip to content

Commit ad3d9af

Browse files
committed
feat: get-version-from-composer-and-pass-it-in-headers
1 parent 4cb3481 commit ad3d9af

6 files changed

Lines changed: 97 additions & 2 deletions

File tree

‎src/Flagsmith.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Flagsmith\Utils\AnalyticsProcessor;
2020
use Flagsmith\Utils\IdentitiesGenerator;
2121
use Flagsmith\Utils\Retry;
22+
use Flagsmith\Utils\UserAgent;
2223
use JsonException;
2324
use ValueError;
2425
use Psr\Http\Client\ClientInterface;
@@ -596,7 +597,8 @@ private function call(string $method, string $uri, array $body = [])
596597
->createRequest($method, rtrim($this->host, '/') . '/' . $uri)
597598
->withHeader('Accept', 'application/json')
598599
->withHeader('Content-Type', 'application/json')
599-
->withHeader('X-Environment-Key', $this->apiKey);
600+
->withHeader('X-Environment-Key', $this->apiKey)
601+
->withHeader('User-Agent', UserAgent::get());
600602

601603
if (!empty($this->customHeaders)) {
602604
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
@@ -7,6 +7,7 @@
77
use FlagsmithTest\Offline\FakeOfflineHandler;
88
use GuzzleHttp\Psr7\Response;
99
use PHPUnit\Framework\TestCase;
10+
use Psr\Http\Client\ClientInterface;
1011
use Psr\Http\Message\StreamFactoryInterface;
1112

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

‎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)