From 727170057e448058fde0543b3b471f9dbb6fa2ec Mon Sep 17 00:00:00 2001 From: "John.R" Date: Fri, 13 Dec 2024 16:17:58 +0100 Subject: [PATCH] fix: client http in v9 --- src/Api/SyncApiClient.php | 55 ++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/src/Api/SyncApiClient.php b/src/Api/SyncApiClient.php index 4b64bfe1..6f18851e 100644 --- a/src/Api/SyncApiClient.php +++ b/src/Api/SyncApiClient.php @@ -31,6 +31,7 @@ use PrestaShop\Module\PsEventbus\Service\PsAccountsAdapterService; use Prestashop\ModuleLibGuzzleAdapter\ClientFactory; use Prestashop\ModuleLibGuzzleAdapter\Interfaces\HttpClientInterface; +use Symfony\Component\HttpClient\HttpClient; if (!defined('_PS_VERSION_')) { exit; @@ -67,23 +68,6 @@ public function __construct($syncApiUrl, \Ps_eventbus $module, PsAccountsAdapter $this->syncApiUrl = $syncApiUrl; } - /** - * @see https://docs.guzzlephp.org/en/stable/quickstart.html- - * - * @param int $timeout - * - * @return HttpClientInterface - */ - private function getClient($timeout = Config::SYNC_API_MAX_TIMEOUT) - { - return (new ClientFactory())->getClient([ - 'allow_redirects' => true, - 'connect_timeout' => 10, - 'http_errors' => false, - 'timeout' => $timeout, - ]); - } - /** * @param string $jobId * @@ -91,17 +75,40 @@ private function getClient($timeout = Config::SYNC_API_MAX_TIMEOUT) */ public function validateJobId($jobId) { - $response = $this->getClient()->sendRequest( - new Request( + if (defined('_PS_VERSION_') && version_compare(_PS_VERSION_, '9', '>=')) { + $client = HttpClient::create(); + + $response = $client->request( 'GET', $this->syncApiUrl . '/job/' . $jobId, [ - 'Accept' => 'application/json', - 'Authorization' => 'Bearer ' . $this->jwt, - 'User-Agent' => 'ps-eventbus/' . $this->module->version, + 'headers' => [ + 'Accept' => 'application/json', + 'Authorization' => 'Bearer ' . $this->jwt, + 'User-Agent' => 'ps-eventbus/' . $this->module->version, + ] ] - ) - ); + ); + } else { + $client = (new ClientFactory())->getClient([ + 'allow_redirects' => true, + 'connect_timeout' => 10, + 'http_errors' => false, + 'timeout' => Config::SYNC_API_MAX_TIMEOUT, + ]); + + $response = $client->sendRequest( + new Request( + 'GET', + $this->syncApiUrl . '/job/' . $jobId, + [ + 'Accept' => 'application/json', + 'Authorization' => 'Bearer ' . $this->jwt, + 'User-Agent' => 'ps-eventbus/' . $this->module->version, + ] + ) + ); + } return [ 'status' => substr((string) $response->getStatusCode(), 0, 1) === '2',