From b100792a3474f7939982b67d79f6426785588ad1 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Mon, 15 Jul 2024 15:11:34 +0200 Subject: [PATCH] Fix request to France Travail Api to accomodate for new request limit A new limit was set to allow a maximum amount of request in a certain timeframe. We need to wait 1 sec to make next request. --- .../ApiHelper/PartenaireRomeAppellation.php | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/Bundle/ChillFranceTravailApiBundle/src/ApiHelper/PartenaireRomeAppellation.php b/src/Bundle/ChillFranceTravailApiBundle/src/ApiHelper/PartenaireRomeAppellation.php index a548894c9..605bbf668 100644 --- a/src/Bundle/ChillFranceTravailApiBundle/src/ApiHelper/PartenaireRomeAppellation.php +++ b/src/Bundle/ChillFranceTravailApiBundle/src/ApiHelper/PartenaireRomeAppellation.php @@ -87,20 +87,27 @@ class PartenaireRomeAppellation { $bearer = $this->getBearer(); - try { - $response = $this->httpClient->request('GET', sprintf(self::BASE.'appellation/%s', $code), [ - 'headers' => [ - 'Authorization' => 'Bearer '.$bearer, - 'Accept' => 'application/json', - ], - 'query' => [ - 'champs' => 'code,libelle,metier(code,libelle)', - ], - ]); + while (true) { + try { + $response = $this->httpClient->request('GET', sprintf(self::BASE.'appellation/%s', $code), [ + 'headers' => [ + 'Authorization' => 'Bearer ' . $bearer, + 'Accept' => 'application/json', + ], + 'query' => [ + 'champs' => 'code,libelle,metier(code,libelle)', + ], + ]); - return $response->toArray(); - } catch (HttpExceptionInterface $exception) { - throw $exception; + return $response->toArray(); + } catch (HttpExceptionInterface $exception) { + if ($exception->getResponse()->getStatusCode() === 429) { + $retryAfter = $exception->getResponse()->getHeaders(false)['retry-after'][0] ?? 1; + sleep((int)$retryAfter); + } else { + throw $exception; + } + } } } }