forked from opensearch-project/opensearch-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuzzleHttpClientFactory.php
57 lines (46 loc) · 1.5 KB
/
GuzzleHttpClientFactory.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
52
53
54
55
56
57
<?php
declare(strict_types=1);
namespace OpenSearch\HttpClient;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use OpenSearch\Client;
use Psr\Log\LoggerInterface;
/**
* Builds an OpenSearch client using Guzzle.
*/
class GuzzleHttpClientFactory implements HttpClientFactoryInterface
{
public function __construct(
protected int $maxRetries = 0,
protected ?LoggerInterface $logger = null,
) {
}
/**
* {@inheritdoc}
*/
public function create(array $options): GuzzleClient
{
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),
],
];
// Merge the default options with the provided options.
$config = array_merge_recursive($defaults, $options);
$stack = HandlerStack::create();
// Handle retries if max_retries is set.
if ($this->maxRetries > 0) {
$decider = new GuzzleRetryDecider($this->maxRetries, $this->logger);
$stack->push(Middleware::retry($decider(...)));
}
$config['handler'] = $stack;
return new GuzzleClient($config);
}
}