Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: client identification headers #232

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
12 changes: 10 additions & 2 deletions src/Client/DefaultRegistrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;

Expand All @@ -24,9 +25,11 @@
private readonly UnleashConfiguration $configuration,
private ?string $sdkName = null,
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();
}

/**
Expand Down Expand Up @@ -54,7 +57,7 @@
'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),
Tymek marked this conversation as resolved.
Show resolved Hide resolved
'started' => (new DateTimeImmutable())->format('c'),
'interval' => $this->configuration->getMetricsInterval(),
'platformName' => PHP_SAPI,
Expand All @@ -66,6 +69,11 @@
$request = $request->withHeader($name, $value);
}

$request = $request
->withHeader('x-unleash-appname', $this->configuration->getAppName() ?? $this->configuration->getInstanceId())

Check failure on line 73 in src/Client/DefaultRegistrationService.php

View workflow job for this annotation

GitHub Actions / Static analysis (8.3)

Expression on left side of ?? is not nullable.
->withHeader('x-unleash-sdk', $this->sdkName . ':' . $this->sdkVersion)
->withHeader('x-unleash-connection-id', $this->connectionId);

Check failure on line 75 in src/Client/DefaultRegistrationService.php

View workflow job for this annotation

GitHub Actions / Static analysis (8.3)

Parameter #2 $value of method Psr\Http\Message\MessageInterface::withHeader() expects array<string>|string, string|null given.

try {
$response = $this->httpClient->sendRequest($request);
$result = $response->getStatusCode() >= 200 && $response->getStatusCode() < 300;
Expand Down
13 changes: 11 additions & 2 deletions src/Repository/DefaultUnleashRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -88,8 +89,10 @@ 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(),
) {}

/**
* @throws ClientExceptionInterface
Expand Down Expand Up @@ -510,6 +513,7 @@ private function fetchFeatures(): array
} else {
$request = $this->requestFactory
->createRequest('GET', (string) Url::appendPath($this->configuration->getUrl(), 'client/features'))
// TODO: remove non-standard headers
Copy link
Member Author

@Tymek Tymek Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe after some time, for example in a next major version

->withHeader('UNLEASH-APPNAME', $this->configuration->getAppName())
->withHeader('UNLEASH-INSTANCEID', $this->configuration->getInstanceId())
->withHeader('Unleash-Client-Spec', Unleash::SPECIFICATION_VERSION);
Expand All @@ -518,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) {
Expand Down
2 changes: 2 additions & 0 deletions src/Unleash.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
21 changes: 19 additions & 2 deletions src/UnleashBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,6 +70,8 @@ final class UnleashBuilder

private ?string $appName = null;

private string $connectionId = Uuid::v4();
Tymek marked this conversation as resolved.
Show resolved Hide resolved

private ?ClientInterface $httpClient = null;

private ?RequestFactoryInterface $requestFactory = null;
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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
);
}
}
26 changes: 26 additions & 0 deletions tests/Client/DefaultRegistrationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,30 @@ 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('', 'customAppName', ''))
->setCache($this->getCache())
->setHeaders([
'Some-Header' => 'some-value',
'x-unleash-connection-id' => 'should not override',
])->setCache($this->getCache());

$instance = new DefaultRegistrationService(
$this->httpClient,
new HttpFactory(),
$configuration
);
$this->pushResponse([]);

$instance->register([]);
self::assertCount(1, $this->requestHistory);

$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'));
}
}
Loading