diff --git a/src/FreshBooksClient.php b/src/FreshBooksClient.php index d41e286..1f1dbc3 100644 --- a/src/FreshBooksClient.php +++ b/src/FreshBooksClient.php @@ -7,6 +7,7 @@ use Http\Client\Common\HttpMethodsClient; use Http\Client\Common\Plugin\BaseUriPlugin; use Http\Client\Common\Plugin\HeaderDefaultsPlugin; +use Http\Client\Common\Plugin\RetryPlugin; use Http\Client\Common\PluginClientFactory; use Http\Discovery\HttpClientDiscovery; use Http\Discovery\Psr17FactoryDiscovery; @@ -81,6 +82,10 @@ protected function createHttpClient(): HttpMethodsClient new HeaderDefaultsPlugin($this->getHeaders()), ); + if ($this->config->autoRetry) { + $plugins[] = new RetryPlugin(['retries' => $this->config->retries]); + } + $pluginClient = (new PluginClientFactory())->createClient( HttpClientDiscovery::find(), $plugins diff --git a/src/FreshBooksClientConfig.php b/src/FreshBooksClientConfig.php index 36f31b7..d0edff9 100644 --- a/src/FreshBooksClientConfig.php +++ b/src/FreshBooksClientConfig.php @@ -24,6 +24,7 @@ class FreshBooksClientConfig public ?DateTimeImmutable $tokenExpiresAt; public ?string $userAgent; public bool $autoRetry; + public int $retries; public int $timeout; public string $version; @@ -39,6 +40,7 @@ public function __construct( ?string $refreshToken = null, ?string $userAgent = null, bool $autoRetry = true, + int $retries = 3, int $timeout = self::DEFAULT_TIMEOUT ) { $this->apiBaseUrl = self::API_BASE_URL; @@ -51,6 +53,7 @@ public function __construct( $this->tokenExpiresAt = null; $this->userAgent = $userAgent; $this->autoRetry = $autoRetry; + $this->retries = $retries; $this->timeout = $timeout; $this->version = $this->getVersion(); }