category -> product association implemented
This commit is contained in:
1 parent
0b8c5de8b0
commit
c8bcfe6bd6
41 files changed
+2222
-703
No files matched your search
@@ -1,107 +1,152 @@
|
||||
<?php
|
||||
if (!defined("_PS_VERSION_")) {
|
||||
exit();
|
||||
}
|
||||
|
||||
class EcomZoneClient
|
||||
{
|
||||
private $apiToken;
|
||||
private $apiUrl;
|
||||
private $maxRetries = 3;
|
||||
private $timeout = 30;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
// Hardcoded API token for testing
|
||||
$this->apiToken = 'klRyAdrXaxL0s6PEUp7LDlH6T8aPSCtBY8NiEHsHiWpc6646K2TZPi5KMxUg';
|
||||
$this->apiUrl = Configuration::get('ECOMZONE_API_URL');
|
||||
|
||||
if (empty($this->apiUrl)) {
|
||||
throw new Exception('API URL not configured');
|
||||
$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?page=' . $page . '&per_page=' . $perPage;
|
||||
|
||||
EcomZoneLogger::log("Making API request", 'INFO', ['url' => $url]);
|
||||
return $this->makeRequest('GET', $url);
|
||||
$url = $this->apiUrl . "/catalog";
|
||||
$params = [
|
||||
"page" => $page,
|
||||
"per_page" => $perPage,
|
||||
];
|
||||
|
||||
return $this->makeRequest("GET", $url, $params);
|
||||
}
|
||||
|
||||
public function getProduct($sku)
|
||||
{
|
||||
return $this->makeRequest('GET', $this->apiUrl . '/product/' . $sku);
|
||||
$url = $this->apiUrl . "/product/" . urlencode($sku);
|
||||
return $this->makeRequest("GET", $url);
|
||||
}
|
||||
|
||||
public function createOrder($orderData)
|
||||
{
|
||||
return $this->makeRequest('POST', $this->apiUrl . '/ordering', $orderData);
|
||||
}
|
||||
|
||||
public function getOrder($orderId)
|
||||
{
|
||||
return $this->makeRequest('GET', $this->apiUrl . '/order/' . $orderId);
|
||||
}
|
||||
|
||||
private function makeRequest($method, $url, $data = null)
|
||||
private function makeRequest($method, $url, $params = [], $data = null)
|
||||
{
|
||||
$retryCount = 0;
|
||||
$maxRetries = 5;
|
||||
$backoff = 1; // initial backoff time in seconds
|
||||
do {
|
||||
$curl = curl_init();
|
||||
$headers = [
|
||||
'Authorization: Bearer ' . $this->apiToken,
|
||||
'Accept: application/json',
|
||||
'Content-Type: application/json',
|
||||
];
|
||||
|
||||
$options = [
|
||||
CURLOPT_URL => $url,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_ENCODING => '',
|
||||
CURLOPT_MAXREDIRS => 10,
|
||||
CURLOPT_TIMEOUT => 30,
|
||||
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
||||
CURLOPT_CUSTOMREQUEST => $method,
|
||||
CURLOPT_HTTPHEADER => $headers,
|
||||
CURLOPT_SSL_VERIFYPEER => false, // Temporarily disable SSL verification for testing
|
||||
];
|
||||
|
||||
if ($data !== null) {
|
||||
$options[CURLOPT_POSTFIELDS] = json_encode($data);
|
||||
}
|
||||
|
||||
curl_setopt_array($curl, $options);
|
||||
|
||||
$response = curl_exec($curl);
|
||||
$err = curl_error($curl);
|
||||
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
||||
|
||||
EcomZoneLogger::log("API Response", 'INFO', [
|
||||
'url' => $url,
|
||||
'method' => $method,
|
||||
'http_code' => $httpCode,
|
||||
'curl_error' => $err,
|
||||
'response' => $response
|
||||
]);
|
||||
|
||||
curl_close($curl);
|
||||
|
||||
if ($err) {
|
||||
if (strpos($err, 'Connection timed out') !== false && $retryCount < $maxRetries) {
|
||||
EcomZoneLogger::log("Connection timed out. Retrying in $backoff seconds...", 'WARNING');
|
||||
sleep($backoff);
|
||||
$backoff *= 2; // exponential backoff
|
||||
$retryCount++;
|
||||
} else {
|
||||
throw new Exception('cURL Error: ' . $err);
|
||||
}
|
||||
} else if ($httpCode >= 400) {
|
||||
throw new Exception('API Error: HTTP ' . $httpCode . ' - ' . $response);
|
||||
} else {
|
||||
$decodedResponse = json_decode($response, true);
|
||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||
throw new Exception('Invalid JSON response: ' . json_last_error_msg());
|
||||
}
|
||||
return $decodedResponse;
|
||||
}
|
||||
} while ($retryCount < $maxRetries);
|
||||
}
|
||||
$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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user