1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace OpenSearch;
6:
7: use OpenSearch\Aws\SigningClientFactory;
8: use OpenSearch\HttpClient\GuzzleHttpClientFactory;
9: use Psr\Log\LoggerInterface;
10: use GuzzleHttp\Psr7\HttpFactory;
11: use OpenSearch\Serializers\SmartSerializer;
12:
13: /**
14: * Creates an OpenSearch client using Guzzle.
15: */
16: class GuzzleClientFactory implements ClientFactoryInterface
17: {
18: public function __construct(
19: protected int $maxRetries = 0,
20: protected ?LoggerInterface $logger = null,
21: protected ?SigningClientFactory $awsSigningHttpClientFactory = null,
22: ) {
23: }
24:
25: /**
26: * @param array<string,mixed> $options
27: * The Guzzle client options.
28: */
29: public function create(array $options): Client
30: {
31: // Clean up the options array for the Guzzle HTTP Client.
32: if (isset($options['auth_aws'])) {
33: $awsAuth = $options['auth_aws'];
34: unset($options['auth_aws']);
35: }
36:
37: $httpClient = (new GuzzleHttpClientFactory($this->maxRetries, $this->logger))->create($options);
38:
39: if (isset($awsAuth)) {
40: if (!isset($awsAuth['host'])) {
41: // Get the host from the base URI.
42: $awsAuth['host'] = parse_url($options['base_uri'], PHP_URL_HOST);
43: }
44: $httpClient = $this->getSigningClientFactory()->create($httpClient, $awsAuth);
45: }
46:
47: $httpFactory = new HttpFactory();
48:
49: $serializer = new SmartSerializer();
50:
51: $requestFactory = new RequestFactory(
52: $httpFactory,
53: $httpFactory,
54: $httpFactory,
55: $serializer,
56: );
57:
58: $transport = (new TransportFactory())
59: ->setHttpClient($httpClient)
60: ->setRequestFactory($requestFactory)
61: ->create();
62:
63: return new Client($transport, new EndpointFactory($serializer), []);
64: }
65:
66: /**
67: * Gets the AWS signing client factory.
68: */
69: protected function getSigningClientFactory(): SigningClientFactory
70: {
71: if ($this->awsSigningHttpClientFactory === null) {
72: $this->awsSigningHttpClientFactory = new SigningClientFactory();
73: }
74: return $this->awsSigningHttpClientFactory;
75: }
76:
77: }
78: