Skip to content

Commit

Permalink
fix: client http in v9
Browse files Browse the repository at this point in the history
  • Loading branch information
fox-john committed Dec 13, 2024
1 parent c3cca7b commit 7271700
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions src/Api/SyncApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,41 +68,47 @@ 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
*
* @return array<mixed>
*/
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',
Expand Down

0 comments on commit 7271700

Please sign in to comment.