Skip to content

Commit

Permalink
Merge pull request #49 from dominikb/main
Browse files Browse the repository at this point in the history
Add Retry plugin to http client
  • Loading branch information
amcintosh authored Oct 6, 2022
2 parents 6192c73 + 24108e9 commit e34bae5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/FreshBooksClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/FreshBooksClientConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class FreshBooksClientConfig
public ?DateTimeImmutable $tokenExpiresAt;
public ?string $userAgent;
public bool $autoRetry;
public int $retries;
public int $timeout;
public string $version;

Expand All @@ -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;
Expand All @@ -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();
}
Expand Down

0 comments on commit e34bae5

Please sign in to comment.