Skip to content

Commit

Permalink
Merge pull request #21 from Clearfacts/CLEARFACTS-10616
Browse files Browse the repository at this point in the history
[CLEARFACTS-10616] symfony http client decorator for correlation id
  • Loading branch information
ctrl-f5 authored Aug 28, 2024
2 parents 982847d + fd2661f commit 38e025b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
"require-dev": {
"phpunit/phpunit": "^9.1",
"clearfacts/cf-codestyle": "^3.1",
"symfony/messenger": "^5.4 || ^6"
"symfony/messenger": "^5.4 || ^6",
"symfony/http-client": "^5.4 || ^6"
},
"suggest": {
"symfony/security": "If you want to log user-id's",
"symfony/messenger": "If you want to use the message serializer for correlation id's"
"symfony/messenger": "If you want to use the message serializer for correlation id's",
"symfony/http-client": "If you want to use the http client decorator for correlation id's"
},
"scripts": {
"set-up": [
Expand Down
35 changes: 35 additions & 0 deletions src/Correlation/Http/CorrelationHttpClientDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Datalog\Correlation\Http;

use Datalog\Correlation\Correlation;
use Symfony\Component\HttpClient\DecoratorTrait;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

class CorrelationHttpClientDecorator implements HttpClientInterface
{
use DecoratorTrait;

private Correlation $correlation;

public function __construct(
Correlation $correlation,
?HttpClientInterface $client = null,
) {
$this->correlation = $correlation;
$this->client = $client ?? HttpClient::create();
}

public function request(string $method, string $url, array $options = []): ResponseInterface
{
if (!isset($options['headers']) || !array_key_exists('correlation_id', $options['headers'])) {
$options['headers']['correlation_id'] = $this->correlation->getId();
}

return $this->client->request($method, $url, $options);
}
}
28 changes: 28 additions & 0 deletions tests/Correlation/Http/CorrelationHttpClientDecoratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);

namespace Tests\Datalog\Correlation\Http;

use Datalog\Correlation\Correlation;
use Datalog\Correlation\Http\CorrelationHttpClientDecorator;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;

class CorrelationHttpClientDecoratorTest extends TestCase
{
public function testAddsHeader(): void
{
Correlation::setId('test-id');

$client = new MockHttpClient(function ($method, $url, $options) {
$this->assertSame(['correlation_id: test-id'], $options['normalized_headers']['correlation_id'] ?? null);

return new MockResponse();
});

$decorator = new CorrelationHttpClientDecorator(new Correlation(), $client);

$decorator->request('GET', 'http://example.com');
}
}

0 comments on commit 38e025b

Please sign in to comment.