1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace OpenSearch\HttpClient;
6:
7: use OpenSearch\Client;
8: use Psr\Log\LoggerInterface;
9: use Symfony\Component\HttpClient\HttpClient;
10: use Symfony\Component\HttpClient\Psr18Client;
11: use Symfony\Component\HttpClient\RetryableHttpClient;
12:
13: /**
14: * Builds an OpenSearch client using Symfony.
15: */
16: class SymfonyHttpClientFactory implements HttpClientFactoryInterface
17: {
18: public function __construct(
19: protected int $maxRetries = 0,
20: protected ?LoggerInterface $logger = null,
21: ) {
22: }
23:
24: /**
25: * {@inheritdoc}
26: */
27: public function create(array $options): Psr18Client
28: {
29: if (!isset($options['base_uri'])) {
30: throw new \InvalidArgumentException('The base_uri option is required.');
31: }
32: // Set default configuration.
33: $defaults = [
34: 'headers' => [
35: 'Accept' => 'application/json',
36: 'Content-Type' => 'application/json',
37: 'User-Agent' => sprintf('opensearch-php/%s (%s; PHP %s)', Client::VERSION, PHP_OS, PHP_VERSION),
38: ],
39: ];
40: $options = array_merge_recursive($defaults, $options);
41:
42: $symfonyClient = HttpClient::create()->withOptions($options);
43:
44: if ($this->maxRetries > 0) {
45: $symfonyClient = new RetryableHttpClient($symfonyClient, null, $this->maxRetries, $this->logger);
46: }
47:
48: return new Psr18Client($symfonyClient);
49: }
50:
51: }
52: