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
13 changes: 12 additions & 1 deletion src/Client/DefaultRegistrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -27,6 +30,7 @@ public function __construct(
) {
$this->sdkName ??= 'unleash-client-php';
$this->sdkVersion ??= Unleash::SDK_VERSION;
$this->connectionId = Uuid::v4();
}

/**
Expand All @@ -51,9 +55,14 @@ 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' => $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),
'started' => (new DateTimeImmutable())->format('c'),
'interval' => $this->configuration->getMetricsInterval(),
Expand All @@ -66,6 +75,8 @@ public function register(iterable $strategyHandlers): bool
$request = $request->withHeader($name, $value);
}

$request = $request->withHeader('x-unleash-connection-id', $this->connectionId);
Tymek marked this conversation as resolved.
Show resolved Hide resolved

try {
$response = $this->httpClient->sendRequest($request);
$result = $response->getStatusCode() >= 200 && $response->getStatusCode() < 300;
Expand Down
22 changes: 22 additions & 0 deletions tests/Client/DefaultRegistrationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,26 @@ 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' => 'should not 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'));
self::assertMatchesRegularExpression('/[0-9a-f-]{36}/', $this->requestHistory[0]['request']->getHeaderLine('x-unleash-connection-id'));
}
}
Loading