-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBaggageAwareHttpClient.php
More file actions
61 lines (49 loc) · 1.81 KB
/
Copy pathBaggageAwareHttpClient.php
File metadata and controls
61 lines (49 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
declare(strict_types=1);
namespace Macpaw\SchemaContextBundle\HttpClient;
use Macpaw\SchemaContextBundle\Service\BaggageCodec;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Macpaw\SchemaContextBundle\Service\BaggageSchemaResolver;
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
class BaggageAwareHttpClient implements HttpClientInterface
{
public function __construct(
private HttpClientInterface $inner,
private BaggageSchemaResolver $baggageSchemaResolver,
private BaggageCodec $baggageCodec,
) {
}
/**
* @param array<string, mixed> $options
*/
public function request(string $method, string $url, array $options = []): ResponseInterface
{
$headers = isset($options['headers']) && is_array($options['headers'])
? $options['headers']
: [];
$baggage = isset($headers['baggage'])
? $this->baggageCodec->decode($headers['baggage'])
: [];
$baggage = [
...$baggage,
...($this->baggageSchemaResolver->getBaggage() ?? [])
];
$headers['baggage'] = $this->baggageCodec->encode($baggage);
$options['headers'] = $headers;
return $this->inner->request($method, $url, $options);
}
public function stream(iterable|ResponseInterface $responses, ?float $timeout = null): ResponseStreamInterface
{
return $this->inner->stream($responses, $timeout);
}
/**
* @param array<string, mixed> $options
*/
public function withOptions(array $options): static
{
$wrapped = $this->inner->withOptions($options);
/** @phpstan-ignore-next-line */
return new self($wrapped, $this->baggageSchemaResolver, $this->baggageCodec);
}
}