44
55use Akeneo \Pim \ApiClient \Api \AuthenticationApiInterface ;
66use Akeneo \Pim \ApiClient \Exception \UnauthorizedHttpException ;
7+ use Akeneo \Pim \ApiClient \Exception \UnprocessableEntityHttpException ;
78use Akeneo \Pim \ApiClient \Security \Authentication ;
89use Psr \Http \Message \ResponseInterface ;
910
@@ -62,11 +63,7 @@ public function sendRequest(string $httpMethod, $uri, array $headers = [], $body
6263 $ headers ['Authorization ' ] = sprintf ('Bearer %s ' , $ this ->authentication ->getAccessToken ());
6364 $ response = $ this ->basicHttpClient ->sendRequest ($ httpMethod , $ uri , $ headers , $ body );
6465 } catch (UnauthorizedHttpException $ e ) {
65- $ tokens = $ this ->authenticationApi ->authenticateByRefreshToken (
66- $ this ->authentication ->getClientId (),
67- $ this ->authentication ->getSecret (),
68- $ this ->authentication ->getRefreshToken ()
69- );
66+ $ tokens = $ this ->renewTokens ($ e );
7067
7168 $ this ->authentication
7269 ->setAccessToken ($ tokens ['access_token ' ])
@@ -78,4 +75,17 @@ public function sendRequest(string $httpMethod, $uri, array $headers = [], $body
7875
7976 return $ response ;
8077 }
78+
79+ private function renewTokens (UnauthorizedHttpException $ unauthorizedHttpException ): array
80+ {
81+ try {
82+ return $ this ->authenticationApi ->authenticateByRefreshToken (
83+ $ this ->authentication ->getClientId (),
84+ $ this ->authentication ->getSecret (),
85+ $ this ->authentication ->getRefreshToken ()
86+ );
87+ } catch (UnprocessableEntityHttpException $ e ) {
88+ throw $ unauthorizedHttpException ;
89+ }
90+ }
8191}
0 commit comments