Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #150 from Net-Logic/feat/guzzle_retry
Browse files Browse the repository at this point in the history
allow guzzle to retry if api fails and add a user agent
  • Loading branch information
kkhelifa-opendsi authored Oct 4, 2024
2 parents a343cdd + 3acbd84 commit 877ca73
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 6 deletions.
46 changes: 42 additions & 4 deletions class/client/eCommerceClientWooCommerceApi.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
dol_include_once('/ecommerceng/includes/oauth-subscriber-woocommerce/src/Oauth1.php');
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Subscriber\Oauth\Oauth1;

/**
Expand Down Expand Up @@ -71,30 +73,66 @@ public function connection($site)
$this->api_url_prefix = '/wp-json/wc/' . $this->api_version;

try {
$userAgent = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:34.0) Gecko/20100101 Firefox/34.0';
if (!empty($conf->global->ECOMMERCE_USER_AGENT)) {
$userAgent = $conf->global->ECOMMERCE_USER_AGENT;
}
$options = [
// Base URI is used with relative requests
'base_uri' => $this->api_url,
// You can set any number of default request options.
'timeout' => $timeout,
// 'cookies' => true,
'headers' => array('User-Agent' => $userAgent),
];
if (!empty($conf->global->ECOMMERCENG_WOOCOMMERCE_NO_VERIFY_SSL)) $options['verify'] = false;

$stack = HandlerStack::create();
if (in_array($this->authentication_type, [ 'oauth1_header', 'oauth1_query' ])) {
$stack = HandlerStack::create();

$middleware = new Oauth1([
'consumer_key' => $this->authentication_login,
'consumer_secret' => $this->authentication_password,
'request_method' => $this->authentication_type == 'oauth1_header' ? Oauth1::REQUEST_METHOD_HEADER : Oauth1::REQUEST_METHOD_QUERY,
'signature_method' => Oauth1::SIGNATURE_METHOD_HMACSHA256,
'api_version' => $this->api_version,
]);
$stack->push($middleware);

$options['handler'] = $stack;
$stack->push($middleware);
$options['auth'] = 'oauth';
}

// Define the retry middleware
$retryMiddleware = Middleware::retry(
function ($retries, $request, $response, $exception) {
// Limit the number of retries to 5
if ($retries >= 5) {
return false;
}

// Retry on server errors (5xx HTTP status codes)
if ($response && $response->getStatusCode() >= 500) {
dol_syslog('ecommerceClientWooCommerceApi::connection is retrying on status code '.$response->getStatusCode(), LOG_WARNING);
return true;
}

// Retry on connection exceptions
if ($exception instanceof RequestException && $exception->getCode() === 0) {
return true;
}

return false;
},
function ($retries) {
// Define a delay function (e.g., exponential backoff)
return (int) pow(2, $retries) * 1000; // Delay in milliseconds
}
);

// Add the retry middleware to the handler stack
$stack->push($retryMiddleware);

$options['handler'] = $stack;

$this->client = new Client($options);
} catch (Exception $e) {
$this->errors[] = $langs->trans('ECommerceErrorConnectAPI', $site->name);
Expand Down
46 changes: 44 additions & 2 deletions class/client/eCommerceClientWordpressApi.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
*/

dol_include_once('/ecommerceng/class/client/eCommerceClientApi.class.php');

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Exception\RequestException;
/**
* Class to manage Client Wordpress API
*/
Expand Down Expand Up @@ -64,15 +67,54 @@ public function connection($site)
$timeout = $site->wordpress_timeout > 0 ? $site->wordpress_timeout : 30;

try {
$stack = HandlerStack::create();

// Define the retry middleware
$retryMiddleware = Middleware::retry(
function ($retries, $request, $response, $exception) {
// Limit the number of retries to 5
if ($retries >= 5) {
return false;
}

// Retry on server errors (5xx HTTP status codes)
if ($response && $response->getStatusCode() >= 500) {
dol_syslog('ecommerceClientWordpressApi::connection is retrying on status code '.$response->getStatusCode(), LOG_WARNING);
return true;
}

// Retry on connection exceptions
if ($exception instanceof RequestException && $exception->getCode() === 0) {
return true;
}

return false;
},
function ($retries) {
// Define a delay function (e.g., exponential backoff)
return (int) pow(2, $retries) * 1000; // Delay in milliseconds
}
);

// Add the retry middleware to the handler stack
$stack->push($retryMiddleware);

$userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
if (!empty($conf->global->ECOMMERCE_USER_AGENT)) {
$userAgent = $conf->global->ECOMMERCE_USER_AGENT;
}
$options = [
// Base URI is used with relative requests
'base_uri' => $this->api_url,
// You can set any number of default request options.
'timeout' => $timeout,
// 'cookies' => true,
'headers' => array('User-Agent' => $userAgent),
'handler' => $stack,
];
if (!empty($conf->global->ECOMMERCENG_WORDPRESS_NO_VERIFY_SSL)) $options['verify'] = false;

$this->client = new GuzzleHttp\Client($options);
$this->client = new Client($options);
} catch (Exception $e) {
$this->errors[] = $langs->trans('ECommerceErrorConnectAPI', $site->name);
$this->errors[] = $e->getMessage();
Expand Down

0 comments on commit 877ca73

Please sign in to comment.