workingCopy/ecomzone/classes/EcomZoneClient.php

153 lines
4.6 KiB
PHP

<?php
if (!defined("_PS_VERSION_")) {
exit();
}
class EcomZoneClient
{
private $apiToken;
private $apiUrl;
private $maxRetries = 3;
private $timeout = 30;
public function __construct()
{
$this->apiToken = Configuration::get("ECOMZONE_API_TOKEN");
$this->apiUrl = Configuration::get("ECOMZONE_API_URL");
if (empty($this->apiToken)) {
throw new EcomZoneException(
"API token not configured",
EcomZoneException::API_ERROR
);
}
if (empty($this->apiUrl)) {
throw new EcomZoneException(
"API URL not configured",
EcomZoneException::API_ERROR
);
}
EcomZoneLogger::log("API Client initialized", "INFO", [
"url" => $this->apiUrl,
]);
}
public function getCatalog($page = 1, $perPage = 100)
{
$url = $this->apiUrl . "/catalog";
$params = [
"page" => $page,
"per_page" => $perPage,
];
return $this->makeRequest("GET", $url, $params);
}
public function getProduct($sku)
{
$url = $this->apiUrl . "/product/" . urlencode($sku);
return $this->makeRequest("GET", $url);
}
private function makeRequest($method, $url, $params = [], $data = null)
{
$retryCount = 0;
$lastError = null;
do {
try {
EcomZoneLogger::log("Making API request", "INFO", [
"method" => $method,
"url" => $url,
"params" => $params,
"retry" => $retryCount,
]);
// Add query parameters
if (!empty($params)) {
$url .=
(strpos($url, "?") === false ? "?" : "&") .
http_build_query($params);
}
$curl = curl_init();
$options = [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => $this->timeout,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer " . $this->apiToken,
"Accept: application/json",
"Content-Type: application/json",
],
];
if ($data !== null) {
$options[CURLOPT_POSTFIELDS] = json_encode($data);
}
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$error = curl_error($curl);
curl_close($curl);
if ($error) {
throw new Exception("cURL Error: " . $error);
}
if ($httpCode >= 400) {
throw new Exception(
"HTTP Error: " . $httpCode . " Response: " . $response
);
}
$decodedResponse = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception("Invalid JSON response");
}
EcomZoneLogger::log("API request successful", "INFO", [
"http_code" => $httpCode,
]);
return $decodedResponse;
} catch (Exception $e) {
$lastError = $e;
$retryCount++;
EcomZoneLogger::log("API request failed", "ERROR", [
"error" => $e->getMessage(),
"retry" => $retryCount,
]);
if ($retryCount < $this->maxRetries) {
sleep(pow(2, $retryCount));
continue;
}
throw new EcomZoneException(
"API request failed after {$this->maxRetries} retries: " .
$e->getMessage(),
EcomZoneException::API_ERROR,
$e
);
}
} while ($retryCount < $this->maxRetries);
throw new EcomZoneException(
"Max retries reached: " . $lastError->getMessage(),
EcomZoneException::API_ERROR,
$lastError
);
}
}