diff --git a/Tests/ClientTest.php b/Tests/ClientTest.php index 66d5b94..a024b8d 100644 --- a/Tests/ClientTest.php +++ b/Tests/ClientTest.php @@ -246,4 +246,38 @@ class ClientTest extends ApiTestCase ) ); } + + /** + * @test + * @return void + * @throws \PHPUnit\Framework\ExpectationFailedException + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException + * @covers \D3\LinkmobilityClient\Client::getLoggerMiddleware + */ + public function testGetLoggerMiddleware() + { + $this->assertIsCallable( + $this->callMethod( + $this->api, + 'getLoggerMiddleware' + ) + ); + } + + /** + * @test + * @return void + * @throws \PHPUnit\Framework\ExpectationFailedException + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException + * @covers \D3\LinkmobilityClient\Client::getRetryMiddleware + */ + public function testGetRetryMiddleware() + { + $this->assertIsCallable( + $this->callMethod( + $this->api, + 'getRetryMiddleware' + ) + ); + } } diff --git a/composer.json b/composer.json index 96ea920..e1bbe1f 100644 --- a/composer.json +++ b/composer.json @@ -27,6 +27,7 @@ "psr/http-message": "~1.0", "phlib/sms-length": "^2.0", "giggsey/libphonenumber-for-php": "^8.12.50", + "caseyamcl/guzzle_retry_middleware": "^2.8", "ext-json": "*" }, "require-dev": { diff --git a/src/Client.php b/src/Client.php index 73a8831..b458018 100644 --- a/src/Client.php +++ b/src/Client.php @@ -24,6 +24,7 @@ use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\HandlerStack; use GuzzleHttp\MessageFormatter; use GuzzleHttp\Middleware; +use GuzzleRetry\GuzzleRetryMiddleware; use InvalidArgumentException; use Psr\Http\Message\ResponseInterface; @@ -45,13 +46,9 @@ class Client */ protected function getDefaultClient(): GuzzleClient { - $logger = Middleware::log( - $this->getLoggerHandler()->getLogger(), - new MessageFormatter(MessageFormatter::DEBUG), - 'debug' - ); $handlerStack = HandlerStack::create(); - $handlerStack->push($logger); + $handlerStack->push($this->getLoggerMiddleware()); + $handlerStack->push($this->getRetryMiddleware()); return new GuzzleClient( [ 'base_uri' => $this->apiUrl->getBaseUri(), @@ -100,4 +97,28 @@ class Client { return LoggerHandler::getInstance(); } + + /** + * @param string $loglevel + * + * @return callable + */ + protected function getLoggerMiddleware(string $loglevel = 'debug'): callable + { + return Middleware::log( + $this->getLoggerHandler()->getLogger(), + new MessageFormatter(MessageFormatter::DEBUG), + $loglevel + ); + } + + /** + * @return callable + */ + protected function getRetryMiddleware(): callable + { + return GuzzleRetryMiddleware::factory([ + 'max_retry_attempts' => 3 + ]); + } }