3 declare(strict_types=1);
13 use GuzzleHttp\Client as HttpClient;
14 use GuzzleHttp\ClientInterface;
15 use GuzzleHttp\HandlerStack;
16 use Psr\Log\LoggerInterface;
27 if (!is_null($authConfig)) {
40 $middlewares = array_merge(
44 return $this->createGuzzleClientWithOptions($config->
getOptions(), $middlewares);
53 array $middlewares = []
55 return $this->createGuzzleClientWithOptions($config->getOptions(), $middlewares);
62 private function createGuzzleClientWithOptions(array $options, array $middlewares = []): ClientInterface
64 if (isset($options[
'handler']) && $options[
'handler'] instanceof HandlerStack) {
65 $stack = $options[
'handler'];
67 $stack = HandlerStack::create();
68 $options[
'handler'] = $stack;
71 $options = array_replace(
73 'allow_redirects' =>
false,
76 'connect_timeout' => 10,
82 if (!isset($options[
'headers'][
'accept-encoding']) && $this->acceptCompressedResponse()) {
83 $options[
'headers'][
'accept-encoding'] =
'gzip';
85 if (!isset($options[
'headers'][
'user-agent'])) {
86 $options[
'headers'][
'user-agent'] = (
new UserAgentProvider())->getUserAgent();
88 foreach ($middlewares as $key => $middleware) {
89 if (!is_callable($middleware)) {
90 throw new InvalidArgumentException(
'Middleware isn\'t callable');
93 $name = is_numeric($key) ?
'' : $key;
94 $stack->push($middleware, $name);
97 $client =
new HttpClient($options);
102 private function acceptCompressedResponse(): bool
104 if (function_exists(
'gzdecode')) {