|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Macpaw\SchemaContextBundle\HttpClient; |
| 6 | + |
| 7 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 8 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
| 9 | +use Macpaw\SchemaContextBundle\Service\SchemaResolver; |
| 10 | +use Symfony\Contracts\HttpClient\ResponseStreamInterface; |
| 11 | + |
| 12 | +class SchemaAwareHttpClient implements HttpClientInterface |
| 13 | +{ |
| 14 | + public function __construct( |
| 15 | + private HttpClientInterface $inner, |
| 16 | + private SchemaResolver $schemaResolver, |
| 17 | + private string $schemaRequestHeader |
| 18 | + ) { |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * @param array<string, mixed> $options |
| 23 | + */ |
| 24 | + public function request(string $method, string $url, array $options = []): ResponseInterface |
| 25 | + { |
| 26 | + $schema = $this->schemaResolver->getSchema(); |
| 27 | + $baggageHeader = $this->schemaRequestHeader . '=' . $schema; |
| 28 | + $headers = isset($options['headers']) && is_array($options['headers']) |
| 29 | + ? $options['headers'] |
| 30 | + : []; |
| 31 | + |
| 32 | + if (isset($headers['baggage'])) { |
| 33 | + $headers['baggage'] .= ',' . $baggageHeader; |
| 34 | + } else { |
| 35 | + $headers['baggage'] = $baggageHeader; |
| 36 | + } |
| 37 | + |
| 38 | + $options['headers'] = $headers; |
| 39 | + |
| 40 | + return $this->inner->request($method, $url, $options); |
| 41 | + } |
| 42 | + |
| 43 | + public function stream(iterable|ResponseInterface $responses, ?float $timeout = null): ResponseStreamInterface |
| 44 | + { |
| 45 | + return $this->inner->stream($responses, $timeout); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @param array<string, mixed> $options |
| 50 | + */ |
| 51 | + public function withOptions(array $options): static |
| 52 | + { |
| 53 | + $wrapped = $this->inner->withOptions($options); |
| 54 | + |
| 55 | + /** @phpstan-ignore-next-line */ |
| 56 | + return new self($wrapped, $this->schemaResolver, $this->schemaRequestHeader); |
| 57 | + } |
| 58 | +} |
0 commit comments