up untill fetch 10 products

This commit is contained in:
dimitar committed 2025-01-27 23:08:03 +01:00
1 parent 9c2c174a43
commit 2eef682859
4 files changed
+148 -28

No files matched your search

+69 -22
View File
@@ -14,6 +14,14 @@ class EcomZoneAPI
$this->apiKey = Configuration::get('ECOMZONE_API_KEY');
$this->apiUrl = Configuration::get('ECOMZONE_API_URL');
$this->lastRequestTime = time();
// Debug log the configuration
PrestaShopLogger::addLog(
sprintf('EcomZone API initialized with URL: %s', $this->apiUrl),
1,
null,
'EcomZone'
);
}
private function checkRateLimit()
@@ -31,40 +39,35 @@ class EcomZoneAPI
$this->requestCount++;
}
public function getCatalog($perPage = 1000)
{
$this->checkRateLimit();
$url = $this->apiUrl . '/catalog';
return $this->makeRequest('GET', $url, ['per_page' => $perPage]);
}
public function getProduct($sku)
{
$this->checkRateLimit();
$url = $this->apiUrl . '/product/' . urlencode($sku);
return $this->makeRequest('GET', $url);
}
public function createOrder($orderData)
{
$this->checkRateLimit();
$url = $this->apiUrl . '/ordering';
return $this->makeRequest('POST', $url, $orderData);
}
private function makeRequest($method, $url, $params = [])
{
try {
// Debug log the request
PrestaShopLogger::addLog(
sprintf('Making request: %s %s', $method, $url),
1,
null,
'EcomZone'
);
$curl = curl_init();
// Add token as URL parameter AND header for maximum compatibility
$urlWithToken = $url . (strpos($url, '?') === false ? '?' : '&') . 'token=' . $this->apiKey;
if ($method === 'GET' && !empty($params)) {
$urlWithToken .= '&' . http_build_query($params);
}
$options = [
CURLOPT_URL => $url . ($method === 'GET' && !empty($params) ? '?' . http_build_query($params) : ''),
CURLOPT_URL => $urlWithToken,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_SSL_VERIFYPEER => false, // For testing only, remove in production
CURLOPT_SSL_VERIFYHOST => false, // For testing only, remove in production
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $this->apiKey,
'Accept: application/json',
@@ -78,10 +81,33 @@ class EcomZoneAPI
curl_setopt_array($curl, $options);
// Debug log the curl options
PrestaShopLogger::addLog(
sprintf('Curl options: %s', json_encode([
'url' => $urlWithToken,
'headers' => $options[CURLOPT_HTTPHEADER]
])),
1,
null,
'EcomZone'
);
$response = curl_exec($curl);
$err = curl_error($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Debug log the response
PrestaShopLogger::addLog(
sprintf('Response Code: %d, Error: %s, Response: %s',
$httpCode,
$err ?: 'none',
substr($response, 0, 1000)
),
1,
null,
'EcomZone'
);
curl_close($curl);
if ($err) {
@@ -103,4 +129,25 @@ class EcomZoneAPI
throw $e;
}
}
public function getCatalog($perPage = 10)
{
$this->checkRateLimit();
$url = $this->apiUrl . '/catalog';
return $this->makeRequest('GET', $url, ['per_page' => $perPage]);
}
public function getProduct($sku)
{
$this->checkRateLimit();
$url = $this->apiUrl . '/product/' . urlencode($sku);
return $this->makeRequest('GET', $url);
}
public function createOrder($orderData)
{
$this->checkRateLimit();
$url = $this->apiUrl . '/ordering';
return $this->makeRequest('POST', $url, $orderData);
}
}