From 458a4c39b97c3f58c4ee270c1092ed0e9e4f19f2 Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Tue, 21 Jan 2025 13:48:10 +0100 Subject: [PATCH 01/18] feat: client identification headers --- src/Client/DefaultRegistrationService.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Client/DefaultRegistrationService.php b/src/Client/DefaultRegistrationService.php index 69602c10..a683a935 100755 --- a/src/Client/DefaultRegistrationService.php +++ b/src/Client/DefaultRegistrationService.php @@ -13,11 +13,14 @@ use Unleash\Client\Enum\CacheKey; use Unleash\Client\Helper\StringStream; use Unleash\Client\Helper\Url; +use Unleash\Client\Helper\Uuid; use Unleash\Client\Strategy\StrategyHandler; use Unleash\Client\Unleash; final class DefaultRegistrationService implements RegistrationService { + private string $connectionId; + public function __construct( private readonly ClientInterface $httpClient, private readonly RequestFactoryInterface $requestFactory, @@ -25,8 +28,9 @@ public function __construct( private ?string $sdkName = null, private ?string $sdkVersion = null, ) { - $this->sdkName ??= 'unleash-client-php'; + $this->sdkName ??= 'unleash-php'; $this->sdkVersion ??= Unleash::SDK_VERSION; + $this->connectionId = Uuid::v4(); } /** @@ -51,9 +55,15 @@ public function register(iterable $strategyHandlers): bool ->createRequest('POST', (string) Url::appendPath($this->configuration->getUrl(), 'client/register')) ->withHeader('Content-Type', 'application/json') ->withBody(new StringStream(json_encode([ + // TODO: delete non-standard redundant headers 'appName' => $this->configuration->getAppName(), 'instanceId' => $this->configuration->getInstanceId(), - 'sdkVersion' => $this->sdkName . ':' . $this->sdkVersion, + 'sdkVersion' => 'unleash-client-php:' . $this->sdkVersion, + + 'x-unleash-appname' => $this->configuration->getAppName(), + 'x-unleash-sdk' => $this->sdkName . '@' . $this->sdkVersion, + 'x-unleash-connection-id' => $this->connectionId, + 'strategies' => array_map(fn (StrategyHandler $strategyHandler): string => $strategyHandler->getStrategyName(), $strategyHandlers), 'started' => (new DateTimeImmutable())->format('c'), 'interval' => $this->configuration->getMetricsInterval(), From 24dc32326d37138fb70189563e0431ebcbaa164c Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Tue, 21 Jan 2025 14:49:23 +0100 Subject: [PATCH 02/18] test: connection headers --- .../Client/DefaultRegistrationServiceTest.php | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/Client/DefaultRegistrationServiceTest.php b/tests/Client/DefaultRegistrationServiceTest.php index edcd0d69..c5d1ff7e 100755 --- a/tests/Client/DefaultRegistrationServiceTest.php +++ b/tests/Client/DefaultRegistrationServiceTest.php @@ -131,4 +131,27 @@ public function testUrl() self::assertCount(1, $this->requestHistory); self::assertSame('https://localhost/api/client/register?namePrefix=somePrefix', (string) $this->requestHistory[0]['request']->getUri()); } + + public function testRequestHeaders() + { + $configuration = (new UnleashConfiguration('', '', '')) + ->setCache($this->getCache()) + ->setHeaders([ + 'Some-Header' => 'some-value', + 'x-unleash-connection-id' => 'override', + ])->setCache($this->getCache()); + + $instance = new DefaultRegistrationService( + $this->httpClient, + new HttpFactory(), + $configuration + ); + $this->pushResponse([]); + + $instance->register([]); + self::assertCount(1, $this->requestHistory); + self::assertSame('some-value', $this->requestHistory[0]['request']->getHeaderLine('Some-Header')); + // should not override the connection ID + self::assertNotEquals('override', $this->requestHistory[0]['request']->getHeaderLine('x-unleash-connection-id')); + } } From 3de64c1ac8e71f1cb49824840a4c0e75123586a3 Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Tue, 21 Jan 2025 16:14:29 +0100 Subject: [PATCH 03/18] revert sdk name convention --- src/Client/DefaultRegistrationService.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Client/DefaultRegistrationService.php b/src/Client/DefaultRegistrationService.php index a683a935..576a3264 100755 --- a/src/Client/DefaultRegistrationService.php +++ b/src/Client/DefaultRegistrationService.php @@ -28,7 +28,7 @@ public function __construct( private ?string $sdkName = null, private ?string $sdkVersion = null, ) { - $this->sdkName ??= 'unleash-php'; + $this->sdkName ??= 'unleash-client-php'; $this->sdkVersion ??= Unleash::SDK_VERSION; $this->connectionId = Uuid::v4(); } @@ -58,11 +58,10 @@ public function register(iterable $strategyHandlers): bool // TODO: delete non-standard redundant headers 'appName' => $this->configuration->getAppName(), 'instanceId' => $this->configuration->getInstanceId(), - 'sdkVersion' => 'unleash-client-php:' . $this->sdkVersion, + 'sdkVersion' => $this->sdkName . ':' . $this->sdkVersion, 'x-unleash-appname' => $this->configuration->getAppName(), - 'x-unleash-sdk' => $this->sdkName . '@' . $this->sdkVersion, - 'x-unleash-connection-id' => $this->connectionId, + 'x-unleash-sdk' => $this->sdkName . ':' . $this->sdkVersion, 'strategies' => array_map(fn (StrategyHandler $strategyHandler): string => $strategyHandler->getStrategyName(), $strategyHandlers), 'started' => (new DateTimeImmutable())->format('c'), @@ -76,6 +75,8 @@ public function register(iterable $strategyHandlers): bool $request = $request->withHeader($name, $value); } + $request = $request->withHeader('x-unleash-connection-id', $this->connectionId); + try { $response = $this->httpClient->sendRequest($request); $result = $response->getStatusCode() >= 200 && $response->getStatusCode() < 300; From 0517a21a62968ce4dc933a8c4e4d7ece530c2728 Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Tue, 21 Jan 2025 16:14:48 +0100 Subject: [PATCH 04/18] test sdk headers - fix connection id --- tests/Client/DefaultRegistrationServiceTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/Client/DefaultRegistrationServiceTest.php b/tests/Client/DefaultRegistrationServiceTest.php index c5d1ff7e..f6ea5b6e 100755 --- a/tests/Client/DefaultRegistrationServiceTest.php +++ b/tests/Client/DefaultRegistrationServiceTest.php @@ -138,7 +138,7 @@ public function testRequestHeaders() ->setCache($this->getCache()) ->setHeaders([ 'Some-Header' => 'some-value', - 'x-unleash-connection-id' => 'override', + 'x-unleash-connection-id' => 'should not override', ])->setCache($this->getCache()); $instance = new DefaultRegistrationService( @@ -151,7 +151,6 @@ public function testRequestHeaders() $instance->register([]); self::assertCount(1, $this->requestHistory); self::assertSame('some-value', $this->requestHistory[0]['request']->getHeaderLine('Some-Header')); - // should not override the connection ID - self::assertNotEquals('override', $this->requestHistory[0]['request']->getHeaderLine('x-unleash-connection-id')); + self::assertMatchesRegularExpression('/[0-9a-f-]{36}/', $this->requestHistory[0]['request']->getHeaderLine('x-unleash-connection-id')); } } From 370a15ec7e63f552e376666f42c96dbe69792c34 Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Tue, 21 Jan 2025 16:18:59 +0100 Subject: [PATCH 05/18] match uuid --- tests/Client/DefaultRegistrationServiceTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Client/DefaultRegistrationServiceTest.php b/tests/Client/DefaultRegistrationServiceTest.php index f6ea5b6e..ee2b3bd3 100755 --- a/tests/Client/DefaultRegistrationServiceTest.php +++ b/tests/Client/DefaultRegistrationServiceTest.php @@ -151,6 +151,6 @@ public function testRequestHeaders() $instance->register([]); self::assertCount(1, $this->requestHistory); self::assertSame('some-value', $this->requestHistory[0]['request']->getHeaderLine('Some-Header')); - self::assertMatchesRegularExpression('/[0-9a-f-]{36}/', $this->requestHistory[0]['request']->getHeaderLine('x-unleash-connection-id')); + self::assertStringMatchesFormat('%s-%s-%s-%s-%s', $this->requestHistory[0]['request']->getHeaderLine('x-unleash-connection-id')); } } From 46a32ff6652ba37b08b439dddd20e7585ec1f526 Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Wed, 22 Jan 2025 17:21:15 +0100 Subject: [PATCH 06/18] add headers to fetchFeatures --- src/Client/DefaultRegistrationService.php | 17 +++++++-------- src/Repository/DefaultUnleashRepository.php | 10 +++++++-- src/Unleash.php | 2 ++ src/UnleashBuilder.php | 21 +++++++++++++++++-- .../Client/DefaultRegistrationServiceTest.php | 10 ++++++--- 5 files changed, 43 insertions(+), 17 deletions(-) diff --git a/src/Client/DefaultRegistrationService.php b/src/Client/DefaultRegistrationService.php index 576a3264..731d4d35 100755 --- a/src/Client/DefaultRegistrationService.php +++ b/src/Client/DefaultRegistrationService.php @@ -19,18 +19,17 @@ final class DefaultRegistrationService implements RegistrationService { - private string $connectionId; - public function __construct( private readonly ClientInterface $httpClient, private readonly RequestFactoryInterface $requestFactory, private readonly UnleashConfiguration $configuration, private ?string $sdkName = null, private ?string $sdkVersion = null, + private ?string $connectionId = null, ) { $this->sdkName ??= 'unleash-client-php'; $this->sdkVersion ??= Unleash::SDK_VERSION; - $this->connectionId = Uuid::v4(); + $this->connectionId = $connectionId ?? Uuid::v4(); } /** @@ -55,15 +54,10 @@ public function register(iterable $strategyHandlers): bool ->createRequest('POST', (string) Url::appendPath($this->configuration->getUrl(), 'client/register')) ->withHeader('Content-Type', 'application/json') ->withBody(new StringStream(json_encode([ - // TODO: delete non-standard redundant headers 'appName' => $this->configuration->getAppName(), 'instanceId' => $this->configuration->getInstanceId(), 'sdkVersion' => $this->sdkName . ':' . $this->sdkVersion, - - 'x-unleash-appname' => $this->configuration->getAppName(), - 'x-unleash-sdk' => $this->sdkName . ':' . $this->sdkVersion, - - 'strategies' => array_map(fn (StrategyHandler $strategyHandler): string => $strategyHandler->getStrategyName(), $strategyHandlers), + 'strategies' => array_map(fn(StrategyHandler $strategyHandler): string => $strategyHandler->getStrategyName(), $strategyHandlers), 'started' => (new DateTimeImmutable())->format('c'), 'interval' => $this->configuration->getMetricsInterval(), 'platformName' => PHP_SAPI, @@ -75,7 +69,10 @@ public function register(iterable $strategyHandlers): bool $request = $request->withHeader($name, $value); } - $request = $request->withHeader('x-unleash-connection-id', $this->connectionId); + $request = $request + ->withHeader('x-unleash-appname', $this->configuration->getAppName() ?? $this->configuration->getInstanceId()) + ->withHeader('x-unleash-sdk', $this->sdkName . ':' . $this->sdkVersion) + ->withHeader('x-unleash-connection-id', $this->connectionId); try { $response = $this->httpClient->sendRequest($request); diff --git a/src/Repository/DefaultUnleashRepository.php b/src/Repository/DefaultUnleashRepository.php index 6035889c..4ac065c8 100755 --- a/src/Repository/DefaultUnleashRepository.php +++ b/src/Repository/DefaultUnleashRepository.php @@ -88,8 +88,10 @@ public function __construct( private ClientInterface $httpClient, private RequestFactoryInterface $requestFactory, private UnleashConfiguration $configuration, - ) { - } + private string $sdkName = null, + private string $sdkVersion = null, + private string $connectionId = null, + ) {} /** * @throws ClientExceptionInterface @@ -510,6 +512,10 @@ private function fetchFeatures(): array } else { $request = $this->requestFactory ->createRequest('GET', (string) Url::appendPath($this->configuration->getUrl(), 'client/features')) + ->withHeader('x-unleash-appname', $this->configuration->getAppName() ?? $this->configuration->getInstanceId()) + ->withHeader('x-unleash-sdk', $this->sdkName . ':' . $this->sdkVersion) + ->withHeader('x-unleash-connection-id', $this->connectionId) + // TODO: remove non-standard headers ->withHeader('UNLEASH-APPNAME', $this->configuration->getAppName()) ->withHeader('UNLEASH-INSTANCEID', $this->configuration->getInstanceId()) ->withHeader('Unleash-Client-Spec', Unleash::SPECIFICATION_VERSION); diff --git a/src/Unleash.php b/src/Unleash.php index 0d921270..7ee5abef 100755 --- a/src/Unleash.php +++ b/src/Unleash.php @@ -7,6 +7,8 @@ interface Unleash { + public const string SDK_NAME = 'unleash-client-php'; + public const string SDK_VERSION = '2.6.0'; public const string SPECIFICATION_VERSION = '5.0.2'; diff --git a/src/UnleashBuilder.php b/src/UnleashBuilder.php index 25713833..76a13a96 100755 --- a/src/UnleashBuilder.php +++ b/src/UnleashBuilder.php @@ -36,6 +36,7 @@ use Unleash\Client\Helper\Builder\StickinessCalculatorAware; use Unleash\Client\Helper\DefaultImplementationLocator; use Unleash\Client\Helper\UnleashBuilderContainer; +use Unleash\Client\Helper\Uuid; use Unleash\Client\Metrics\DefaultMetricsBucketSerializer; use Unleash\Client\Metrics\DefaultMetricsHandler; use Unleash\Client\Metrics\DefaultMetricsSender; @@ -69,6 +70,8 @@ final class UnleashBuilder private ?string $appName = null; + private string $connectionId = Uuid::v4(); + private ?ClientInterface $httpClient = null; private ?RequestFactoryInterface $requestFactory = null; @@ -406,7 +409,14 @@ public function build(): Unleash ); assert($dependencyContainer->getConfiguration() !== null); - $registrationService = $this->registrationService ?? new DefaultRegistrationService($dependencyContainer->getHttpClient(), $dependencyContainer->getRequestFactory(), $dependencyContainer->getConfiguration()); + $registrationService = $this->registrationService ?? new DefaultRegistrationService( + $dependencyContainer->getHttpClient(), + $dependencyContainer->getRequestFactory(), + $dependencyContainer->getConfiguration(), + Unleash::SDK_NAME, + Unleash::SDK_VERSION, + $this->connectionId + ); $metricsHandler = $this->metricsHandler ?? new DefaultMetricsHandler($metricsSender, $dependencyContainer->getConfiguration()); $variantHandler = $this->variantHandler ?? new DefaultVariantHandler(new MurmurHashCalculator()); @@ -671,6 +681,13 @@ private function createRepository(UnleashBuilderContainer $dependencyContainer): { assert($dependencyContainer->getConfiguration() !== null); - return new DefaultUnleashRepository($dependencyContainer->getHttpClient(), $dependencyContainer->getRequestFactory(), $dependencyContainer->getConfiguration()); + return new DefaultUnleashRepository( + $dependencyContainer->getHttpClient(), + $dependencyContainer->getRequestFactory(), + $dependencyContainer->getConfiguration(), + Unleash::SDK_NAME, + Unleash::SDK_VERSION, + $this->connectionId + ); } } diff --git a/tests/Client/DefaultRegistrationServiceTest.php b/tests/Client/DefaultRegistrationServiceTest.php index ee2b3bd3..b6760512 100755 --- a/tests/Client/DefaultRegistrationServiceTest.php +++ b/tests/Client/DefaultRegistrationServiceTest.php @@ -134,7 +134,7 @@ public function testUrl() public function testRequestHeaders() { - $configuration = (new UnleashConfiguration('', '', '')) + $configuration = (new UnleashConfiguration('', 'customAppName', '')) ->setCache($this->getCache()) ->setHeaders([ 'Some-Header' => 'some-value', @@ -150,7 +150,11 @@ public function testRequestHeaders() $instance->register([]); self::assertCount(1, $this->requestHistory); - self::assertSame('some-value', $this->requestHistory[0]['request']->getHeaderLine('Some-Header')); - self::assertStringMatchesFormat('%s-%s-%s-%s-%s', $this->requestHistory[0]['request']->getHeaderLine('x-unleash-connection-id')); + + $request = $this->requestHistory[0]['request']; + self::assertSame('some-value', $request->getHeaderLine('Some-Header')); + self::assertEquals('customAppName', $request->getHeaderLine('x-unleash-appname')); + self::assertStringStartsWith('unleash-client-php:', $request->getHeaderLine('x-unleash-sdk')); + self::assertStringMatchesFormat('%s-%s-%s-%s-%s', $request->getHeaderLine('x-unleash-connection-id')); } } From 68e07e54f6a670e9b463e3931829e06585ac3c3d Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Wed, 22 Jan 2025 18:55:05 +0100 Subject: [PATCH 07/18] update sdk headers definitons --- src/Client/DefaultRegistrationService.php | 4 ++-- src/Repository/DefaultUnleashRepository.php | 15 +++++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Client/DefaultRegistrationService.php b/src/Client/DefaultRegistrationService.php index 731d4d35..323793c4 100755 --- a/src/Client/DefaultRegistrationService.php +++ b/src/Client/DefaultRegistrationService.php @@ -27,7 +27,7 @@ public function __construct( private ?string $sdkVersion = null, private ?string $connectionId = null, ) { - $this->sdkName ??= 'unleash-client-php'; + $this->sdkName ??= Unleash::SDK_NAME; $this->sdkVersion ??= Unleash::SDK_VERSION; $this->connectionId = $connectionId ?? Uuid::v4(); } @@ -56,7 +56,7 @@ public function register(iterable $strategyHandlers): bool ->withBody(new StringStream(json_encode([ 'appName' => $this->configuration->getAppName(), 'instanceId' => $this->configuration->getInstanceId(), - 'sdkVersion' => $this->sdkName . ':' . $this->sdkVersion, + 'sdkVersion' => $this->sdkName . ':' . $this->sdkVersion, 'strategies' => array_map(fn(StrategyHandler $strategyHandler): string => $strategyHandler->getStrategyName(), $strategyHandlers), 'started' => (new DateTimeImmutable())->format('c'), 'interval' => $this->configuration->getMetricsInterval(), diff --git a/src/Repository/DefaultUnleashRepository.php b/src/Repository/DefaultUnleashRepository.php index 4ac065c8..5bedffd8 100755 --- a/src/Repository/DefaultUnleashRepository.php +++ b/src/Repository/DefaultUnleashRepository.php @@ -34,6 +34,7 @@ use Unleash\Client\Exception\HttpResponseException; use Unleash\Client\Exception\InvalidValueException; use Unleash\Client\Helper\Url; +use Unleash\Client\Helper\Uuid; use Unleash\Client\Unleash; /** @@ -88,9 +89,9 @@ public function __construct( private ClientInterface $httpClient, private RequestFactoryInterface $requestFactory, private UnleashConfiguration $configuration, - private string $sdkName = null, - private string $sdkVersion = null, - private string $connectionId = null, + private string $sdkName = Unleash::SDK_NAME, + private string $sdkVersion = Unleash::SDK_VERSION, + private string $connectionId = Uuid::v4(), ) {} /** @@ -512,9 +513,6 @@ private function fetchFeatures(): array } else { $request = $this->requestFactory ->createRequest('GET', (string) Url::appendPath($this->configuration->getUrl(), 'client/features')) - ->withHeader('x-unleash-appname', $this->configuration->getAppName() ?? $this->configuration->getInstanceId()) - ->withHeader('x-unleash-sdk', $this->sdkName . ':' . $this->sdkVersion) - ->withHeader('x-unleash-connection-id', $this->connectionId) // TODO: remove non-standard headers ->withHeader('UNLEASH-APPNAME', $this->configuration->getAppName()) ->withHeader('UNLEASH-INSTANCEID', $this->configuration->getInstanceId()) @@ -524,6 +522,11 @@ private function fetchFeatures(): array $request = $request->withHeader($name, $value); } + $request = $request + ->withHeader('x-unleash-appname', $this->configuration->getAppName()) + ->withHeader('x-unleash-sdk', $this->sdkName . ':' . $this->sdkVersion) + ->withHeader('x-unleash-connection-id', $this->connectionId); + try { $response = $this->httpClient->sendRequest($request); if ($response->getStatusCode() === 200) { From ed8736329189d9cd00032328733094f6e66cffe9 Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Thu, 23 Jan 2025 14:01:20 +0100 Subject: [PATCH 08/18] fix registration service constructor --- src/Client/DefaultRegistrationService.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Client/DefaultRegistrationService.php b/src/Client/DefaultRegistrationService.php index 323793c4..08594a9a 100755 --- a/src/Client/DefaultRegistrationService.php +++ b/src/Client/DefaultRegistrationService.php @@ -19,16 +19,20 @@ final class DefaultRegistrationService implements RegistrationService { + private string $sdkName; + private string $sdkVersion; + private string $connectionId; + public function __construct( private readonly ClientInterface $httpClient, private readonly RequestFactoryInterface $requestFactory, private readonly UnleashConfiguration $configuration, - private ?string $sdkName = null, - private ?string $sdkVersion = null, - private ?string $connectionId = null, + ?string $sdkName = null, + ?string $sdkVersion = null, + ?string $connectionId = null, ) { - $this->sdkName ??= Unleash::SDK_NAME; - $this->sdkVersion ??= Unleash::SDK_VERSION; + $this->sdkName = $sdkName ?? Unleash::SDK_NAME; + $this->sdkVersion = $sdkVersion ?? Unleash::SDK_VERSION; $this->connectionId = $connectionId ?? Uuid::v4(); } From f1b9d1251899583b073d38026efb3b3004a06d68 Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Thu, 23 Jan 2025 14:05:27 +0100 Subject: [PATCH 09/18] fix: appname not-nullable static check --- src/Client/DefaultRegistrationService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client/DefaultRegistrationService.php b/src/Client/DefaultRegistrationService.php index 08594a9a..2a067efb 100755 --- a/src/Client/DefaultRegistrationService.php +++ b/src/Client/DefaultRegistrationService.php @@ -74,7 +74,7 @@ public function register(iterable $strategyHandlers): bool } $request = $request - ->withHeader('x-unleash-appname', $this->configuration->getAppName() ?? $this->configuration->getInstanceId()) + ->withHeader('x-unleash-appname', $this->configuration->getAppName()) ->withHeader('x-unleash-sdk', $this->sdkName . ':' . $this->sdkVersion) ->withHeader('x-unleash-connection-id', $this->connectionId); From 206b548f19d5673a084e0dac29cd047fe5510152 Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Thu, 23 Jan 2025 14:08:31 +0100 Subject: [PATCH 10/18] fix: linter style --- src/Client/DefaultRegistrationService.php | 4 +++- src/Repository/DefaultUnleashRepository.php | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Client/DefaultRegistrationService.php b/src/Client/DefaultRegistrationService.php index 2a067efb..092ecd48 100755 --- a/src/Client/DefaultRegistrationService.php +++ b/src/Client/DefaultRegistrationService.php @@ -20,7 +20,9 @@ final class DefaultRegistrationService implements RegistrationService { private string $sdkName; + private string $sdkVersion; + private string $connectionId; public function __construct( @@ -61,7 +63,7 @@ public function register(iterable $strategyHandlers): bool 'appName' => $this->configuration->getAppName(), 'instanceId' => $this->configuration->getInstanceId(), 'sdkVersion' => $this->sdkName . ':' . $this->sdkVersion, - 'strategies' => array_map(fn(StrategyHandler $strategyHandler): string => $strategyHandler->getStrategyName(), $strategyHandlers), + 'strategies' => array_map(fn (StrategyHandler $strategyHandler): string => $strategyHandler->getStrategyName(), $strategyHandlers), 'started' => (new DateTimeImmutable())->format('c'), 'interval' => $this->configuration->getMetricsInterval(), 'platformName' => PHP_SAPI, diff --git a/src/Repository/DefaultUnleashRepository.php b/src/Repository/DefaultUnleashRepository.php index 5bedffd8..84bd336e 100755 --- a/src/Repository/DefaultUnleashRepository.php +++ b/src/Repository/DefaultUnleashRepository.php @@ -92,7 +92,8 @@ public function __construct( private string $sdkName = Unleash::SDK_NAME, private string $sdkVersion = Unleash::SDK_VERSION, private string $connectionId = Uuid::v4(), - ) {} + ) { + } /** * @throws ClientExceptionInterface From 50c52609df59d2d313e894936dd6f78b0a107443 Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Thu, 23 Jan 2025 14:22:10 +0100 Subject: [PATCH 11/18] connection id initialization --- src/Repository/DefaultUnleashRepository.php | 15 ++++++++++++--- src/UnleashBuilder.php | 4 +++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Repository/DefaultUnleashRepository.php b/src/Repository/DefaultUnleashRepository.php index 84bd336e..01e3bd88 100755 --- a/src/Repository/DefaultUnleashRepository.php +++ b/src/Repository/DefaultUnleashRepository.php @@ -85,14 +85,23 @@ */ final readonly class DefaultUnleashRepository implements UnleashRepository { + private string $sdkName; + + private string $sdkVersion; + + private string $connectionId; + public function __construct( private ClientInterface $httpClient, private RequestFactoryInterface $requestFactory, private UnleashConfiguration $configuration, - private string $sdkName = Unleash::SDK_NAME, - private string $sdkVersion = Unleash::SDK_VERSION, - private string $connectionId = Uuid::v4(), + ?string $sdkName = null, + ?string $sdkVersion = null, + ?string $connectionId = null, ) { + $this->sdkName = $sdkName ?? Unleash::SDK_NAME; + $this->sdkVersion = $sdkVersion ?? Unleash::SDK_VERSION; + $this->connectionId = $connectionId ?? Uuid::v4(); } /** diff --git a/src/UnleashBuilder.php b/src/UnleashBuilder.php index 76a13a96..ac5624b0 100755 --- a/src/UnleashBuilder.php +++ b/src/UnleashBuilder.php @@ -70,7 +70,7 @@ final class UnleashBuilder private ?string $appName = null; - private string $connectionId = Uuid::v4(); + private string $connectionId; private ?ClientInterface $httpClient = null; @@ -134,6 +134,8 @@ public function __construct() $this->eventDispatcher = new EventDispatcher(); } + $this->connectionId = Uuid::v4(); + $rolloutStrategyHandler = new GradualRolloutStrategyHandler(new MurmurHashCalculator()); $this->strategies = [ new DefaultStrategyHandler(), From 0550c86038e1b4b258c2d35a2e347455847adc29 Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Thu, 23 Jan 2025 16:37:07 +0100 Subject: [PATCH 12/18] move connectionId to shared configuration --- src/Client/DefaultRegistrationService.php | 17 +++-------------- src/Configuration/UnleashConfiguration.php | 17 +++++++++++++++++ src/Enum/CacheKey.php | 2 ++ src/Repository/DefaultUnleashRepository.php | 21 ++++++--------------- src/UnleashBuilder.php | 6 ------ 5 files changed, 28 insertions(+), 35 deletions(-) diff --git a/src/Client/DefaultRegistrationService.php b/src/Client/DefaultRegistrationService.php index 092ecd48..5f4131b1 100755 --- a/src/Client/DefaultRegistrationService.php +++ b/src/Client/DefaultRegistrationService.php @@ -13,29 +13,18 @@ use Unleash\Client\Enum\CacheKey; use Unleash\Client\Helper\StringStream; use Unleash\Client\Helper\Url; -use Unleash\Client\Helper\Uuid; use Unleash\Client\Strategy\StrategyHandler; use Unleash\Client\Unleash; final class DefaultRegistrationService implements RegistrationService { - private string $sdkName; - - private string $sdkVersion; - - private string $connectionId; - public function __construct( private readonly ClientInterface $httpClient, private readonly RequestFactoryInterface $requestFactory, private readonly UnleashConfiguration $configuration, - ?string $sdkName = null, - ?string $sdkVersion = null, - ?string $connectionId = null, + private ?string $sdkName = Unleash::SDK_NAME, + private ?string $sdkVersion = Unleash::SDK_VERSION, ) { - $this->sdkName = $sdkName ?? Unleash::SDK_NAME; - $this->sdkVersion = $sdkVersion ?? Unleash::SDK_VERSION; - $this->connectionId = $connectionId ?? Uuid::v4(); } /** @@ -78,7 +67,7 @@ public function register(iterable $strategyHandlers): bool $request = $request ->withHeader('x-unleash-appname', $this->configuration->getAppName()) ->withHeader('x-unleash-sdk', $this->sdkName . ':' . $this->sdkVersion) - ->withHeader('x-unleash-connection-id', $this->connectionId); + ->withHeader('x-unleash-connection-id', $this->configuration->getConnectionId()); try { $response = $this->httpClient->sendRequest($request); diff --git a/src/Configuration/UnleashConfiguration.php b/src/Configuration/UnleashConfiguration.php index b30faf69..8fec45ce 100755 --- a/src/Configuration/UnleashConfiguration.php +++ b/src/Configuration/UnleashConfiguration.php @@ -14,12 +14,16 @@ use Unleash\Client\Bootstrap\EmptyBootstrapProvider; use Unleash\Client\ContextProvider\DefaultUnleashContextProvider; use Unleash\Client\ContextProvider\UnleashContextProvider; +use Unleash\Client\Enum\CacheKey; use Unleash\Client\Helper\Url; +use Unleash\Client\Helper\Uuid; use Unleash\Client\Metrics\DefaultMetricsBucketSerializer; use Unleash\Client\Metrics\MetricsBucketSerializer; final class UnleashConfiguration { + private string $connectionId; + /** * @param array $headers */ @@ -45,6 +49,7 @@ public function __construct( private MetricsBucketSerializer $metricsBucketSerializer = new DefaultMetricsBucketSerializer(), ) { $this->contextProvider ??= new DefaultUnleashContextProvider(); + $this->connectionId = Uuid::v4(); } public function getCache(): CacheInterface @@ -298,4 +303,16 @@ public function setMetricsBucketSerializer(MetricsBucketSerializer $metricsBucke return $this; } + + public function getConnectionId(): string + { + $cachedConnectionId = $this->getCache()->get(CacheKey::CONNECTION_ID); + + return $cachedConnectionId ?? $this->connectionId; + } + + public function updateCachedConnectionId(): void + { + $this->getCache()->set(CacheKey::CONNECTION_ID, $this->connectionId); + } } diff --git a/src/Enum/CacheKey.php b/src/Enum/CacheKey.php index 411933ae..6623d695 100755 --- a/src/Enum/CacheKey.php +++ b/src/Enum/CacheKey.php @@ -14,4 +14,6 @@ final class CacheKey public const string REGISTRATION = 'unleash.client.metrics.registration'; public const string FEATURES_RESPONSE = 'unleash.client.feature.response'; + + public const string CONNECTION_ID = 'unleash.client.connection.id'; } diff --git a/src/Repository/DefaultUnleashRepository.php b/src/Repository/DefaultUnleashRepository.php index 01e3bd88..efa3435b 100755 --- a/src/Repository/DefaultUnleashRepository.php +++ b/src/Repository/DefaultUnleashRepository.php @@ -34,7 +34,6 @@ use Unleash\Client\Exception\HttpResponseException; use Unleash\Client\Exception\InvalidValueException; use Unleash\Client\Helper\Url; -use Unleash\Client\Helper\Uuid; use Unleash\Client\Unleash; /** @@ -85,23 +84,13 @@ */ final readonly class DefaultUnleashRepository implements UnleashRepository { - private string $sdkName; - - private string $sdkVersion; - - private string $connectionId; - public function __construct( private ClientInterface $httpClient, private RequestFactoryInterface $requestFactory, private UnleashConfiguration $configuration, - ?string $sdkName = null, - ?string $sdkVersion = null, - ?string $connectionId = null, + private string $sdkName = Unleash::SDK_NAME, + private string $sdkVersion = Unleash::SDK_VERSION, ) { - $this->sdkName = $sdkName ?? Unleash::SDK_NAME; - $this->sdkVersion = $sdkVersion ?? Unleash::SDK_VERSION; - $this->connectionId = $connectionId ?? Uuid::v4(); } /** @@ -171,7 +160,9 @@ private function getCachedFeatures(): ?array private function setCache(array $features): void { $cache = $this->configuration->getCache(); - $cache->set(CacheKey::FEATURES, $features, $this->configuration->getTtl()); + $ttl = $this->configuration->getTtl(); + $cache->set(CacheKey::FEATURES, $features, $ttl); + $this->configuration->updateCachedConnectionId(); } /** @@ -535,7 +526,7 @@ private function fetchFeatures(): array $request = $request ->withHeader('x-unleash-appname', $this->configuration->getAppName()) ->withHeader('x-unleash-sdk', $this->sdkName . ':' . $this->sdkVersion) - ->withHeader('x-unleash-connection-id', $this->connectionId); + ->withHeader('x-unleash-connection-id', $this->configuration->getConnectionId()); try { $response = $this->httpClient->sendRequest($request); diff --git a/src/UnleashBuilder.php b/src/UnleashBuilder.php index ac5624b0..7b17edbe 100755 --- a/src/UnleashBuilder.php +++ b/src/UnleashBuilder.php @@ -415,9 +415,6 @@ public function build(): Unleash $dependencyContainer->getHttpClient(), $dependencyContainer->getRequestFactory(), $dependencyContainer->getConfiguration(), - Unleash::SDK_NAME, - Unleash::SDK_VERSION, - $this->connectionId ); $metricsHandler = $this->metricsHandler ?? new DefaultMetricsHandler($metricsSender, $dependencyContainer->getConfiguration()); $variantHandler = $this->variantHandler ?? new DefaultVariantHandler(new MurmurHashCalculator()); @@ -687,9 +684,6 @@ private function createRepository(UnleashBuilderContainer $dependencyContainer): $dependencyContainer->getHttpClient(), $dependencyContainer->getRequestFactory(), $dependencyContainer->getConfiguration(), - Unleash::SDK_NAME, - Unleash::SDK_VERSION, - $this->connectionId ); } } From 78b8103f1399a78f2ecf930a1425f251cf4c3d96 Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Fri, 24 Jan 2025 10:28:23 +0100 Subject: [PATCH 13/18] update connection id string casting --- src/Configuration/UnleashConfiguration.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Configuration/UnleashConfiguration.php b/src/Configuration/UnleashConfiguration.php index 8fec45ce..54b53dfd 100755 --- a/src/Configuration/UnleashConfiguration.php +++ b/src/Configuration/UnleashConfiguration.php @@ -306,9 +306,8 @@ public function setMetricsBucketSerializer(MetricsBucketSerializer $metricsBucke public function getConnectionId(): string { - $cachedConnectionId = $this->getCache()->get(CacheKey::CONNECTION_ID); - - return $cachedConnectionId ?? $this->connectionId; + $connectionId = $this->getCache()->get(CacheKey::CONNECTION_ID, $this->connectionId); + return is_string($connectionId) ? $connectionId : $this->connectionId; } public function updateCachedConnectionId(): void From f02f7faa74e499d134c6614b5c3604bb91a260b6 Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Fri, 24 Jan 2025 10:32:27 +0100 Subject: [PATCH 14/18] remove connection id for now --- src/Client/DefaultRegistrationService.php | 3 +-- src/Configuration/UnleashConfiguration.php | 16 ---------------- src/Enum/CacheKey.php | 2 -- src/Repository/DefaultUnleashRepository.php | 4 +--- src/UnleashBuilder.php | 5 ----- tests/Client/DefaultRegistrationServiceTest.php | 2 -- 6 files changed, 2 insertions(+), 30 deletions(-) diff --git a/src/Client/DefaultRegistrationService.php b/src/Client/DefaultRegistrationService.php index 5f4131b1..500f71cc 100755 --- a/src/Client/DefaultRegistrationService.php +++ b/src/Client/DefaultRegistrationService.php @@ -66,8 +66,7 @@ public function register(iterable $strategyHandlers): bool $request = $request ->withHeader('x-unleash-appname', $this->configuration->getAppName()) - ->withHeader('x-unleash-sdk', $this->sdkName . ':' . $this->sdkVersion) - ->withHeader('x-unleash-connection-id', $this->configuration->getConnectionId()); + ->withHeader('x-unleash-sdk', $this->sdkName . ':' . $this->sdkVersion); try { $response = $this->httpClient->sendRequest($request); diff --git a/src/Configuration/UnleashConfiguration.php b/src/Configuration/UnleashConfiguration.php index 54b53dfd..b30faf69 100755 --- a/src/Configuration/UnleashConfiguration.php +++ b/src/Configuration/UnleashConfiguration.php @@ -14,16 +14,12 @@ use Unleash\Client\Bootstrap\EmptyBootstrapProvider; use Unleash\Client\ContextProvider\DefaultUnleashContextProvider; use Unleash\Client\ContextProvider\UnleashContextProvider; -use Unleash\Client\Enum\CacheKey; use Unleash\Client\Helper\Url; -use Unleash\Client\Helper\Uuid; use Unleash\Client\Metrics\DefaultMetricsBucketSerializer; use Unleash\Client\Metrics\MetricsBucketSerializer; final class UnleashConfiguration { - private string $connectionId; - /** * @param array $headers */ @@ -49,7 +45,6 @@ public function __construct( private MetricsBucketSerializer $metricsBucketSerializer = new DefaultMetricsBucketSerializer(), ) { $this->contextProvider ??= new DefaultUnleashContextProvider(); - $this->connectionId = Uuid::v4(); } public function getCache(): CacheInterface @@ -303,15 +298,4 @@ public function setMetricsBucketSerializer(MetricsBucketSerializer $metricsBucke return $this; } - - public function getConnectionId(): string - { - $connectionId = $this->getCache()->get(CacheKey::CONNECTION_ID, $this->connectionId); - return is_string($connectionId) ? $connectionId : $this->connectionId; - } - - public function updateCachedConnectionId(): void - { - $this->getCache()->set(CacheKey::CONNECTION_ID, $this->connectionId); - } } diff --git a/src/Enum/CacheKey.php b/src/Enum/CacheKey.php index 6623d695..411933ae 100755 --- a/src/Enum/CacheKey.php +++ b/src/Enum/CacheKey.php @@ -14,6 +14,4 @@ final class CacheKey public const string REGISTRATION = 'unleash.client.metrics.registration'; public const string FEATURES_RESPONSE = 'unleash.client.feature.response'; - - public const string CONNECTION_ID = 'unleash.client.connection.id'; } diff --git a/src/Repository/DefaultUnleashRepository.php b/src/Repository/DefaultUnleashRepository.php index efa3435b..e4d1b3a0 100755 --- a/src/Repository/DefaultUnleashRepository.php +++ b/src/Repository/DefaultUnleashRepository.php @@ -162,7 +162,6 @@ private function setCache(array $features): void $cache = $this->configuration->getCache(); $ttl = $this->configuration->getTtl(); $cache->set(CacheKey::FEATURES, $features, $ttl); - $this->configuration->updateCachedConnectionId(); } /** @@ -525,8 +524,7 @@ private function fetchFeatures(): array $request = $request ->withHeader('x-unleash-appname', $this->configuration->getAppName()) - ->withHeader('x-unleash-sdk', $this->sdkName . ':' . $this->sdkVersion) - ->withHeader('x-unleash-connection-id', $this->configuration->getConnectionId()); + ->withHeader('x-unleash-sdk', $this->sdkName . ':' . $this->sdkVersion); try { $response = $this->httpClient->sendRequest($request); diff --git a/src/UnleashBuilder.php b/src/UnleashBuilder.php index 7b17edbe..d17d052e 100755 --- a/src/UnleashBuilder.php +++ b/src/UnleashBuilder.php @@ -36,7 +36,6 @@ use Unleash\Client\Helper\Builder\StickinessCalculatorAware; use Unleash\Client\Helper\DefaultImplementationLocator; use Unleash\Client\Helper\UnleashBuilderContainer; -use Unleash\Client\Helper\Uuid; use Unleash\Client\Metrics\DefaultMetricsBucketSerializer; use Unleash\Client\Metrics\DefaultMetricsHandler; use Unleash\Client\Metrics\DefaultMetricsSender; @@ -70,8 +69,6 @@ final class UnleashBuilder private ?string $appName = null; - private string $connectionId; - private ?ClientInterface $httpClient = null; private ?RequestFactoryInterface $requestFactory = null; @@ -134,8 +131,6 @@ public function __construct() $this->eventDispatcher = new EventDispatcher(); } - $this->connectionId = Uuid::v4(); - $rolloutStrategyHandler = new GradualRolloutStrategyHandler(new MurmurHashCalculator()); $this->strategies = [ new DefaultStrategyHandler(), diff --git a/tests/Client/DefaultRegistrationServiceTest.php b/tests/Client/DefaultRegistrationServiceTest.php index b6760512..dffa74f5 100755 --- a/tests/Client/DefaultRegistrationServiceTest.php +++ b/tests/Client/DefaultRegistrationServiceTest.php @@ -138,7 +138,6 @@ public function testRequestHeaders() ->setCache($this->getCache()) ->setHeaders([ 'Some-Header' => 'some-value', - 'x-unleash-connection-id' => 'should not override', ])->setCache($this->getCache()); $instance = new DefaultRegistrationService( @@ -155,6 +154,5 @@ public function testRequestHeaders() self::assertSame('some-value', $request->getHeaderLine('Some-Header')); self::assertEquals('customAppName', $request->getHeaderLine('x-unleash-appname')); self::assertStringStartsWith('unleash-client-php:', $request->getHeaderLine('x-unleash-sdk')); - self::assertStringMatchesFormat('%s-%s-%s-%s-%s', $request->getHeaderLine('x-unleash-connection-id')); } } From feec93d0ab4d2ae09dec08aa495d2a6df5c6bf9d Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Fri, 24 Jan 2025 10:48:18 +0100 Subject: [PATCH 15/18] revert formatting changes --- src/UnleashBuilder.php | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/UnleashBuilder.php b/src/UnleashBuilder.php index d17d052e..25713833 100755 --- a/src/UnleashBuilder.php +++ b/src/UnleashBuilder.php @@ -406,11 +406,7 @@ public function build(): Unleash ); assert($dependencyContainer->getConfiguration() !== null); - $registrationService = $this->registrationService ?? new DefaultRegistrationService( - $dependencyContainer->getHttpClient(), - $dependencyContainer->getRequestFactory(), - $dependencyContainer->getConfiguration(), - ); + $registrationService = $this->registrationService ?? new DefaultRegistrationService($dependencyContainer->getHttpClient(), $dependencyContainer->getRequestFactory(), $dependencyContainer->getConfiguration()); $metricsHandler = $this->metricsHandler ?? new DefaultMetricsHandler($metricsSender, $dependencyContainer->getConfiguration()); $variantHandler = $this->variantHandler ?? new DefaultVariantHandler(new MurmurHashCalculator()); @@ -675,10 +671,6 @@ private function createRepository(UnleashBuilderContainer $dependencyContainer): { assert($dependencyContainer->getConfiguration() !== null); - return new DefaultUnleashRepository( - $dependencyContainer->getHttpClient(), - $dependencyContainer->getRequestFactory(), - $dependencyContainer->getConfiguration(), - ); + return new DefaultUnleashRepository($dependencyContainer->getHttpClient(), $dependencyContainer->getRequestFactory(), $dependencyContainer->getConfiguration()); } } From 2770b7746a37557d7a545cbb382db46a860a4759 Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Fri, 24 Jan 2025 11:01:58 +0100 Subject: [PATCH 16/18] refactor: move sdk identification to config --- src/Client/DefaultRegistrationService.php | 12 +++++++----- src/Configuration/UnleashConfiguration.php | 17 ++++++++++++++++- src/Repository/DefaultUnleashRepository.php | 6 ------ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/Client/DefaultRegistrationService.php b/src/Client/DefaultRegistrationService.php index 500f71cc..7d260f61 100755 --- a/src/Client/DefaultRegistrationService.php +++ b/src/Client/DefaultRegistrationService.php @@ -22,7 +22,13 @@ public function __construct( private readonly ClientInterface $httpClient, private readonly RequestFactoryInterface $requestFactory, private readonly UnleashConfiguration $configuration, + /** + * @deprecated use configuration sdkVersion property + */ private ?string $sdkName = Unleash::SDK_NAME, + /** + * @deprecated use configuration sdkVersion property + */ private ?string $sdkVersion = Unleash::SDK_VERSION, ) { } @@ -51,7 +57,7 @@ public function register(iterable $strategyHandlers): bool ->withBody(new StringStream(json_encode([ 'appName' => $this->configuration->getAppName(), 'instanceId' => $this->configuration->getInstanceId(), - 'sdkVersion' => $this->sdkName . ':' . $this->sdkVersion, + 'sdkVersion' => $this->configuration->getSdkVersion() || $this->sdkName . ':' . $this->sdkVersion, 'strategies' => array_map(fn (StrategyHandler $strategyHandler): string => $strategyHandler->getStrategyName(), $strategyHandlers), 'started' => (new DateTimeImmutable())->format('c'), 'interval' => $this->configuration->getMetricsInterval(), @@ -64,10 +70,6 @@ public function register(iterable $strategyHandlers): bool $request = $request->withHeader($name, $value); } - $request = $request - ->withHeader('x-unleash-appname', $this->configuration->getAppName()) - ->withHeader('x-unleash-sdk', $this->sdkName . ':' . $this->sdkVersion); - try { $response = $this->httpClient->sendRequest($request); $result = $response->getStatusCode() >= 200 && $response->getStatusCode() < 300; diff --git a/src/Configuration/UnleashConfiguration.php b/src/Configuration/UnleashConfiguration.php index b30faf69..afa41977 100755 --- a/src/Configuration/UnleashConfiguration.php +++ b/src/Configuration/UnleashConfiguration.php @@ -17,6 +17,7 @@ use Unleash\Client\Helper\Url; use Unleash\Client\Metrics\DefaultMetricsBucketSerializer; use Unleash\Client\Metrics\MetricsBucketSerializer; +use Unleash\Client\Unleash; final class UnleashConfiguration { @@ -43,6 +44,10 @@ public function __construct( private ?string $proxyKey = null, private ?CacheInterface $metricsCache = null, private MetricsBucketSerializer $metricsBucketSerializer = new DefaultMetricsBucketSerializer(), + /** + * SDK identifier in a format of `unleash-client-:`. + */ + private string $sdkVersion = Unleash::SDK_NAME . ':' . Unleash::SDK_VERSION, ) { $this->contextProvider ??= new DefaultUnleashContextProvider(); } @@ -183,7 +188,12 @@ public function setMetricsEnabled(bool $metricsEnabled): self */ public function getHeaders(): array { - return $this->headers; + $identificationHeaders = [ + 'x-unleash-appname' => $this->getAppName(), + 'x-unleash-sdk' => $this->getSdkVersion(), + ]; + + return array_merge($this->headers, $identificationHeaders); } /** @@ -298,4 +308,9 @@ public function setMetricsBucketSerializer(MetricsBucketSerializer $metricsBucke return $this; } + + public function getSdkVersion(): string + { + return $this->sdkVersion; + } } diff --git a/src/Repository/DefaultUnleashRepository.php b/src/Repository/DefaultUnleashRepository.php index e4d1b3a0..f838a05a 100755 --- a/src/Repository/DefaultUnleashRepository.php +++ b/src/Repository/DefaultUnleashRepository.php @@ -88,8 +88,6 @@ public function __construct( private ClientInterface $httpClient, private RequestFactoryInterface $requestFactory, private UnleashConfiguration $configuration, - private string $sdkName = Unleash::SDK_NAME, - private string $sdkVersion = Unleash::SDK_VERSION, ) { } @@ -522,10 +520,6 @@ private function fetchFeatures(): array $request = $request->withHeader($name, $value); } - $request = $request - ->withHeader('x-unleash-appname', $this->configuration->getAppName()) - ->withHeader('x-unleash-sdk', $this->sdkName . ':' . $this->sdkVersion); - try { $response = $this->httpClient->sendRequest($request); if ($response->getStatusCode() === 200) { From 832c9e77b1ebef282348799a9ea7802a457de24d Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Fri, 24 Jan 2025 11:13:02 +0100 Subject: [PATCH 17/18] improve legacy format for version resolution in registration service --- src/Client/DefaultRegistrationService.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Client/DefaultRegistrationService.php b/src/Client/DefaultRegistrationService.php index 7d260f61..0defb12d 100755 --- a/src/Client/DefaultRegistrationService.php +++ b/src/Client/DefaultRegistrationService.php @@ -25,11 +25,11 @@ public function __construct( /** * @deprecated use configuration sdkVersion property */ - private ?string $sdkName = Unleash::SDK_NAME, + private ?string $sdkName = '', /** * @deprecated use configuration sdkVersion property */ - private ?string $sdkVersion = Unleash::SDK_VERSION, + private ?string $sdkVersion = '', ) { } @@ -51,13 +51,15 @@ public function register(iterable $strategyHandlers): bool if (!is_array($strategyHandlers)) { $strategyHandlers = iterator_to_array($strategyHandlers); } + $legacySdkVersion = $this->sdkName . ':' . $this->sdkVersion; + $request = $this->requestFactory ->createRequest('POST', (string) Url::appendPath($this->configuration->getUrl(), 'client/register')) ->withHeader('Content-Type', 'application/json') ->withBody(new StringStream(json_encode([ 'appName' => $this->configuration->getAppName(), 'instanceId' => $this->configuration->getInstanceId(), - 'sdkVersion' => $this->configuration->getSdkVersion() || $this->sdkName . ':' . $this->sdkVersion, + 'sdkVersion' => ($legacySdkVersion !== ':') ? $legacySdkVersion : $this->configuration->getSdkVersion(), 'strategies' => array_map(fn (StrategyHandler $strategyHandler): string => $strategyHandler->getStrategyName(), $strategyHandlers), 'started' => (new DateTimeImmutable())->format('c'), 'interval' => $this->configuration->getMetricsInterval(), From 8fb5eeaa08631aee71e56281d61c21cdf07fae93 Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Tue, 4 Feb 2025 16:16:31 +0100 Subject: [PATCH 18/18] drop deprecated x-header convention --- src/Configuration/UnleashConfiguration.php | 4 ++-- tests/Client/DefaultRegistrationServiceTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Configuration/UnleashConfiguration.php b/src/Configuration/UnleashConfiguration.php index afa41977..91f9c17c 100755 --- a/src/Configuration/UnleashConfiguration.php +++ b/src/Configuration/UnleashConfiguration.php @@ -189,8 +189,8 @@ public function setMetricsEnabled(bool $metricsEnabled): self public function getHeaders(): array { $identificationHeaders = [ - 'x-unleash-appname' => $this->getAppName(), - 'x-unleash-sdk' => $this->getSdkVersion(), + 'unleash-appname' => $this->getAppName(), + 'unleash-sdk' => $this->getSdkVersion(), ]; return array_merge($this->headers, $identificationHeaders); diff --git a/tests/Client/DefaultRegistrationServiceTest.php b/tests/Client/DefaultRegistrationServiceTest.php index dffa74f5..6f85661a 100755 --- a/tests/Client/DefaultRegistrationServiceTest.php +++ b/tests/Client/DefaultRegistrationServiceTest.php @@ -152,7 +152,7 @@ public function testRequestHeaders() $request = $this->requestHistory[0]['request']; self::assertSame('some-value', $request->getHeaderLine('Some-Header')); - self::assertEquals('customAppName', $request->getHeaderLine('x-unleash-appname')); - self::assertStringStartsWith('unleash-client-php:', $request->getHeaderLine('x-unleash-sdk')); + self::assertEquals('customAppName', $request->getHeaderLine('unleash-appname')); + self::assertStringStartsWith('unleash-client-php:', $request->getHeaderLine('unleash-sdk')); } }