feat: retries of api requests in case of failure
This commit is contained in:
@@ -7,16 +7,23 @@ use App\Entity\DTO\QRCode\QRCode;
|
||||
use App\Service\CacheableQRCodeGeneratorInterface;
|
||||
use App\Service\Remote\Edge\QRCodeEntityConverter;
|
||||
use App\Service\Remote\Exception\UsetrenoQRCodeException;
|
||||
use App\Service\Remote\Exception\UsetrenoQRCodeRemoteServerErrorException;
|
||||
use Nubium\Exception\ThisShouldNeverHappenException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\HttpClient\Exception\TransportException;
|
||||
|
||||
class UsetrenoQRCodeProvider implements CacheableQRCodeGeneratorInterface
|
||||
{
|
||||
use RetryingFailClientTrait;
|
||||
|
||||
const QRCODE_API = 'https://topapi.top-test.cz/chameleon/api/v1/qr-code/create-for-bank-account-payment';
|
||||
const QRCODE_METHOD = 'POST';
|
||||
|
||||
public function __construct(protected readonly LoggerInterface $logger,
|
||||
protected readonly UsetrenoHttpClient $client,
|
||||
protected readonly QRCodeEntityConverter $codeEntityConverter) { }
|
||||
protected readonly QRCodeEntityConverter $codeEntityConverter,
|
||||
protected readonly float $retryWaitSeconds,
|
||||
protected readonly int $retryCount) { }
|
||||
|
||||
public function generateQRCodeFromEntity(QRCode $entity): string
|
||||
{
|
||||
@@ -27,27 +34,41 @@ class UsetrenoQRCodeProvider implements CacheableQRCodeGeneratorInterface
|
||||
"edgeEntity" => $edgeEntity
|
||||
]);
|
||||
|
||||
$response = $this->client->request(static::QRCODE_METHOD, static::QRCODE_API, [
|
||||
'json' => $edgeEntity,
|
||||
]);
|
||||
$responseData = $this->retryingFailRequest($this->retryCount, $this->retryWaitSeconds,
|
||||
[TransportException::class, UsetrenoQRCodeRemoteServerErrorException::class], $this->logger,
|
||||
function() use ($edgeEntity) {
|
||||
$response = $this->client->request(static::QRCODE_METHOD, static::QRCODE_API, [
|
||||
'json' => $edgeEntity,
|
||||
]);
|
||||
|
||||
$statusCode = $response->getStatusCode();
|
||||
$responseData = $response->getContent(false);
|
||||
$statusCode = $response->getStatusCode();
|
||||
$responseData = $response->getContent(false);
|
||||
|
||||
if ($statusCode != 200) {
|
||||
$this->logger->error("qrcode request status code is not ok", [
|
||||
"AUTHORIZE_API" => static::QRCODE_API,
|
||||
"statusCode" => $statusCode,
|
||||
"content" => $responseData,
|
||||
]);
|
||||
if ($statusCode != 200) {
|
||||
$this->logger->error("qrcode request status code is not ok", [
|
||||
"AUTHORIZE_API" => static::QRCODE_API,
|
||||
"statusCode" => $statusCode,
|
||||
"content" => $responseData,
|
||||
]);
|
||||
|
||||
throw new UsetrenoQRCodeException("Return code is not 200 OK (got: code: $statusCode)");
|
||||
if ($statusCode > 500) {
|
||||
throw new UsetrenoQRCodeRemoteServerErrorException("Return code is not 200 OK (got: code: $statusCode)");
|
||||
}
|
||||
|
||||
throw new UsetrenoQRCodeException("Return code is not 200 OK (got: code: $statusCode)");
|
||||
}
|
||||
|
||||
$this->logger->debug("QRCode generation success", [
|
||||
"responseContent" => $responseData,
|
||||
]);
|
||||
|
||||
return $responseData;
|
||||
});
|
||||
|
||||
if (!is_string($responseData)) {
|
||||
throw new ThisShouldNeverHappenException("responseData is not a string");
|
||||
}
|
||||
|
||||
$this->logger->debug("QRCode generation success", [
|
||||
"responseContent" => $responseData,
|
||||
]);
|
||||
|
||||
|
||||
return $this->parseBase64String($this->processQRCodeResponseEntity($responseData));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user