Skip to content

Commit b867df0

Browse files
Added proxy and Retry functionality
1 parent f363312 commit b867df0

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

src/Stack/Stack.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public function __construct(
7373
unset($this->header['environment']);
7474
$livePreview = array('enable' => false, 'host' => $previewHost);
7575
$this->live_preview = $config['live_preview'] ? array_merge($livePreview, $config['live_preview']) : $livePreview;
76+
$this->proxy = array_***_exists("proxy",$config) ? $config['proxy'] : array('proxy'=>array());
77+
$this->timeout = array_***_exists("timeout",$config) ? $config['timeout'] : '3000';
78+
$this->retryDelay = array_***_exists("retryDelay",$config) ? $config['retryDelay'] : '3000';
79+
$this->retryLimit = array_***_exists("retryLimit",$config) ? $config['retryLimit'] : '5';
80+
$this->errorRetry = array_***_exists("errorRetry",$config) ? $config['errorRetry'] : array('errorRetry'=>array(408, 429));
7681
return $this;
7782
}
7883

src/Support/Utility.php

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,10 @@ public static function wrapResult($result = '', $queryObject = '')
375375
*
376376
* @return Result
377377
* */
378-
public static function contentstackRequest($queryObject = '', $type = '')
378+
public static function send_request($queryObject = '', $type = '')
379379
{
380380
$server_output = '';
381-
381+
382382
if ($queryObject) {
383383
if (Utility::isLivePreview($queryObject)) {
384384
$queryObject->_query['live_preview'] = ($queryObject->contentType->stack->live_preview['live_preview'] ?? 'init');
@@ -399,30 +399,81 @@ public static function contentstackRequest($queryObject = '', $type = '')
399399
if ($Headers["branch"] !== '' && $Headers["branch"] !== "undefined") {
400400
$request_headers[] = 'branch: '.$Headers["branch"];
401401
}
402+
403+
$proxy_details = $queryObject->contentType->stack->proxy;
404+
$timeout = $queryObject->contentType->stack->timeout;
405+
402406
curl_setopt($http, CURLOPT_HTTPHEADER, $request_headers);
403407

404408
curl_setopt($http, CURLOPT_HEADER, false);
405409
// setting the GET request
406410
curl_setopt($http, CURLOPT_CUSTOMREQUEST, "GET");
407411
// receive server response ...
408412
curl_setopt($http, CURLOPT_RETURNTRANSFER, true);
409-
$response = curl_exec($http);
413+
// set the cURL time out
414+
curl_setopt($http, CURLOPT_TIMEOUT_MS, $timeout);
415+
416+
if(array_***_exists("url",$proxy_details) && array_***_exists("port",$proxy_details)){
417+
if($proxy_details['url'] != '' && $proxy_details['port'] != '') {
418+
419+
// Set the proxy IP
420+
curl_setopt($http, CURLOPT_PROXY, $proxy_details['url']);
421+
// Set the port
422+
curl_setopt($http, CURLOPT_PROXYPORT, $proxy_details['port']);
423+
424+
}
425+
}
426+
if(array_***_exists("username",$proxy_details) && array_***_exists("password",$proxy_details)){
427+
if($proxy_details['username'] != '' && $proxy_details['password'] != '') {
428+
429+
$proxyauth = $proxy_details['username'].":".$proxy_details['password'];
430+
// Set the username and password
431+
curl_setopt($http, CURLOPT_PROXYUSERPWD, $proxyauth);
432+
433+
}
434+
}
410435

436+
$response = curl_exec($http);
411437
// status code extraction
412438
$httpcode = curl_getinfo($http, CURLINFO_HTTP_CODE);
413-
439+
414440
// close the curl
415441
curl_close($http);
416442
if ($httpcode > 199 && $httpcode < 300) {
417443
// wrapper the server result
418-
$response = Utility::wrapResult($response, $queryObject);
419-
} else {
420-
throw new CSException($response, $httpcode);
444+
$response = Utility::wrapResult($response, $queryObject);
445+
446+
}elseif($httpcode == 0){
447+
$response = array( 'error_message' => 'cURL couldn’t hook up with the proxy IP address and port used', 'error_code' => '407');
448+
}
449+
else{
450+
$response = Utility::wrapResult($response, $httpcode);
451+
}
452+
}
453+
$response += ['httpcode' => $httpcode];
454+
return $response;
455+
}
456+
457+
public static function contentstackRequest($queryObject = '', $type = '', $count = 0)
458+
{
459+
$retryDelay = $queryObject->contentType->stack->retryDelay;
460+
$retryLimit = $queryObject->contentType->stack->retryLimit;
461+
$errorRetry = $queryObject->contentType->stack->errorRetry;
462+
$response = Utility::send_request($queryObject, $type);
463+
464+
if(in_array( $response['httpcode'] ,$errorRetry)){
465+
if($count < $retryLimit){
466+
$retryDelay = round($retryDelay/1000); //converting retry_delay from milliseconds into seconds
467+
sleep($retryDelay); //sleep method requires time in seconds
468+
$count += 1;
469+
$response = Utility::contentstackRequest($queryObject, $type, $count);
421470
}
422471
}
472+
unset($response['httpcode']);
423473
return $response;
424474
}
425475

476+
426477
/**
427478
* Validate the *** is set or not
428479
*

0 commit comments

Comments
 (0)