forked from opensearch-project/opensearch-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymfonyHttpClientFactory.php
51 lines (42 loc) · 1.41 KB
/
SymfonyHttpClientFactory.php
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
<?php
declare(strict_types=1);
namespace OpenSearch\HttpClient;
use OpenSearch\Client;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\Psr18Client;
use Symfony\Component\HttpClient\RetryableHttpClient;
/**
* Builds an OpenSearch client using Symfony.
*/
class SymfonyHttpClientFactory implements HttpClientFactoryInterface
{
public function __construct(
protected int $maxRetries = 0,
protected ?LoggerInterface $logger = null,
) {
}
/**
* {@inheritdoc}
*/
public function create(array $options): Psr18Client
{
if (!isset($options['base_uri'])) {
throw new \InvalidArgumentException('The base_uri option is required.');
}
// Set default configuration.
$defaults = [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'User-Agent' => sprintf('opensearch-php/%s (%s; PHP %s)', Client::VERSION, PHP_OS, PHP_VERSION),
],
];
$options = array_merge_recursive($defaults, $options);
$symfonyClient = HttpClient::create()->withOptions($options);
if ($this->maxRetries > 0) {
$symfonyClient = new RetryableHttpClient($symfonyClient, null, $this->maxRetries, $this->logger);
}
return new Psr18Client($symfonyClient);
}
}