-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSchemaAwareHttpClient.php
More file actions
58 lines (47 loc) · 1.71 KB
/
SchemaAwareHttpClient.php
File metadata and controls
58 lines (47 loc) · 1.71 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
<?php
declare(strict_types=1);
namespace Macpaw\SchemaContextBundle\HttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Macpaw\SchemaContextBundle\Service\SchemaResolver;
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
class SchemaAwareHttpClient implements HttpClientInterface
{
public function __construct(
private HttpClientInterface $inner,
private SchemaResolver $schemaResolver,
private string $schemaRequestHeader
) {
}
/**
* @param array<string, mixed> $options
*/
public function request(string $method, string $url, array $options = []): ResponseInterface
{
$schema = $this->schemaResolver->getSchema();
$baggageHeader = $this->schemaRequestHeader . '=' . $schema;
$headers = isset($options['headers']) && is_array($options['headers'])
? $options['headers']
: [];
if (isset($headers['baggage'])) {
$headers['baggage'] .= ',' . $baggageHeader;
} else {
$headers['baggage'] = $baggageHeader;
}
$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->schemaResolver, $this->schemaRequestHeader);
}
}