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

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/Client/DefaultRegistrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ public function __construct(
private readonly ClientInterface $httpClient,
private readonly RequestFactoryInterface $requestFactory,
private readonly UnleashConfiguration $configuration,
private ?string $sdkName = null,
private ?string $sdkVersion = null,
/**
* @deprecated use configuration sdkVersion property
*/
private ?string $sdkName = '',
/**
* @deprecated use configuration sdkVersion property
*/
private ?string $sdkVersion = '',
) {
$this->sdkName ??= 'unleash-client-php';
$this->sdkVersion ??= Unleash::SDK_VERSION;
}

/**
Expand All @@ -47,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->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(),
Expand Down
17 changes: 16 additions & 1 deletion src/Configuration/UnleashConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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-<language-or-framework>:<semver>`.
*/
private string $sdkVersion = Unleash::SDK_NAME . ':' . Unleash::SDK_VERSION,
) {
$this->contextProvider ??= new DefaultUnleashContextProvider();
}
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -298,4 +308,9 @@ public function setMetricsBucketSerializer(MetricsBucketSerializer $metricsBucke

return $this;
}

public function getSdkVersion(): string
{
return $this->sdkVersion;
}
}
4 changes: 3 additions & 1 deletion src/Repository/DefaultUnleashRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ 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);
}

/**
Expand Down Expand Up @@ -510,6 +511,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 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.1';

public const string SPECIFICATION_VERSION = '5.0.2';
Expand Down
24 changes: 24 additions & 0 deletions tests/Client/DefaultRegistrationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,28 @@ 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',
])->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'));
}
}
Loading