Skip to content

Commit

Permalink
GuzzleSenderFactory: Guzzle Request Options (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtemYurov authored Dec 8, 2024
1 parent 66b288c commit 388e460
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ $ms = new MoySklad(
Библиотека содержит встроенную фабрику `GuzzleSenderFactory()`, принимающую следующие аргументы:
* `retries` - количество повторных попыток отправки запроса в случае неудачи. По умолчанию 0 (одна отправка, без повторных попыток). Задержка между повторами экспоненциальна.
* `exceptionTruncateAt` - максимальный размер сообщения об ошибке. Значение Guzzle по умолчанию - 120 символов, чего во многих ситуациях недостаточно.
* `requestOptions` - массив c [Guzzle Request Options](https://docs.guzzlephp.org/en/stable/request-options.html), можно задать, например, `connect_timeout` и `timeout`

| - | [Оглавление](/docs/index.md) | [Взаимодействие с API >>](/docs/api_interaction.md) |
|:--|:----------------------------:|----------------------------------------------------:|
4 changes: 2 additions & 2 deletions src/Http/GuzzleSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class GuzzleSender implements RequestSenderInterface
{
public function __construct(private readonly Client $client)
public function __construct(private readonly Client $client, private readonly array $requestOptions = [])
{
}

Expand All @@ -20,6 +20,6 @@ public function __construct(private readonly Client $client)
*/
public function send(RequestInterface $request): ResponseInterface
{
return $this->client->send($request);
return $this->client->send($request, $this->requestOptions);
}
}
6 changes: 4 additions & 2 deletions src/Http/GuzzleSenderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ class GuzzleSenderFactory implements RequestSenderFactoryInterface
*
* @param int $retries количество повторных попыток отправки запроса в случае неудачи
* @param int $exceptionTruncateAt максимальный размер сообщения об ошибке
* @param array $requestOptions https://docs.guzzlephp.org/en/stable/request-options.html
*/
public function __construct(
private readonly int $retries = 0,
private readonly int $exceptionTruncateAt = 4000
private readonly int $exceptionTruncateAt = 4000,
private readonly array $requestOptions = []
) {
}

Expand All @@ -38,7 +40,7 @@ protected function makeFromHandler(?callable $handler): GuzzleSender
$handlerStack = HandlerStack::create($handler);
$client = $this->createClient($handlerStack);

return new GuzzleSender($client);
return new GuzzleSender($client, $this->requestOptions);
}

protected function createClient(HandlerStack $handlerStack): Client
Expand Down

0 comments on commit 388e460

Please sign in to comment.