1
0
mirror of synced 2024-11-22 05:16:07 +03:00

psr2 & rollback for timeout handle

This commit is contained in:
Alex Lushpai 2015-05-08 18:03:05 +03:00
parent 7ec35c2ad8
commit de6dbdd87f
2 changed files with 40 additions and 38 deletions

View File

@ -221,7 +221,7 @@ class ApiClient
'orders' => json_encode($ids),
));
}
/**
* Get orders assembly history
*
@ -288,11 +288,14 @@ class ApiClient
return $this->client->makeRequest(
"/customers/" . $customer[$by] . "/edit",
Client::METHOD_POST,
$this->fillSite($site, array(
'customer' => json_encode($customer),
'by' => $by,
$this->fillSite(
$site,
array(
'customer' => json_encode($customer),
'by' => $by
)
)
));
);
}
/**
@ -375,12 +378,13 @@ class ApiClient
/**
* Get purchace prices & stock balance
*
* @param array $filter (default: array())
* @param int $page (default: null)
* @param int $limit (default: null)
* @param array $filter (default: array())
* @param int $page (default: null)
* @param int $limit (default: null)
* @param string $site (default: null)
* @return ApiResponse
*/
public function storeInventories(array $filter = array(), $page = null, $limit = null)
public function storeInventories(array $filter = array(), $page = null, $limit = null, $site = null)
{
$parameters = array();
@ -394,7 +398,7 @@ class ApiClient
$parameters['limit'] = (int) $limit;
}
return $this->client->makeRequest('/store/inventories', Client::METHOD_GET, $parameters);
return $this->client->makeRequest('/store/inventories', Client::METHOD_GET, $this->fillSite($site, $parameters));
}
/**
@ -410,9 +414,11 @@ class ApiClient
throw new \InvalidArgumentException('Parameter `offers` must contains array of the customers');
}
return $this->client->makeRequest("/store/inventories/upload", Client::METHOD_POST, $this->fillSite($site, array(
'offers' => json_encode($offers),
)));
return $this->client->makeRequest(
"/store/inventories/upload",
Client::METHOD_POST,
$this->fillSite($site, array('offers' => json_encode($offers)))
);
}
/**

View File

@ -39,8 +39,14 @@ class Client
* @param bool $debug
* @return ApiResponse
*/
public function makeRequest($path, $method, array $parameters = array(), $timeout = 30, $verify = false, $debug = false)
{
public function makeRequest(
$path,
$method,
array $parameters = array(),
$timeout = 30,
$verify = false,
$debug = false
) {
$allowedMethods = array(self::METHOD_GET, self::METHOD_POST);
if (!in_array($method, $allowedMethods)) {
throw new \InvalidArgumentException(sprintf(
@ -53,6 +59,7 @@ class Client
$parameters = array_merge($this->defaultParameters, $parameters);
$path = $this->url . $path;
if (self::METHOD_GET === $method && sizeof($parameters)) {
$path .= '?' . http_build_query($parameters);
}
@ -72,10 +79,21 @@ class Client
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
}
$responseBody = $this->curlExec($ch, $debug);
$responseBody = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$errno = curl_errno($ch);
$error = curl_error($ch);
/*if ($errno && in_array($errno, array(6, 7, 28, 34, 35)) && $this->retry < 3) {
$this->retry += 1;
if ($debug) {
error_log('CURL RETRY #' . $this->retry . PHP_EOL, 4);
}
$this->makeRequest($path, $method, $parameters, $timeout, $verify, $debug);
}*/
curl_close($ch);
if ($errno) {
@ -84,26 +102,4 @@ class Client
return new ApiResponse($statusCode, $responseBody);
}
/**
* @param resource $ch
* @param boolean $debug
* @return mixed
*/
private function curlExec($ch, $debug) {
$exec = curl_exec($ch);
if (curl_errno($ch) && in_array(curl_errno($ch), array(6, 7, 28, 34, 35)) && $this->retry < 3) {
$this->retry += 1;
if ($debug) {
error_log('CURL RETRY #' . $this->retry . PHP_EOL, 4);
}
$this->curlExec($ch, $debug);
}
return $exec;
}
}