Skip to content

Commit a5ad378

Browse files
authored
Merge pull request #29 from contentstack/feature/PHP-proxy
Added proxy and Retry functionality
2 parents c1b81d1 + a1b8a36 commit a5ad378

File tree

8 files changed

+331
-28
lines changed

8 files changed

+331
-28
lines changed

src/Stack/Assets.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function Query()
8181
public function fetch()
8282
{
8383
$this->operation = __FUNCTION__;
84-
return Utility::contentstackRequest($this, 'asset');
84+
return Utility::contentstackRequest($this->stack, $this, 'asset');
8585
}
8686
}
8787

src/Stack/BaseQuery.php

+261-3
Large diffs are not rendered by default.

src/Stack/ContentType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function fetch($params = null)
7676
$myArray = json_decode($params, true);
7777
$this->_query = $myArray;
7878
}
79-
return Utility::contentstackRequest($this);
79+
return Utility::contentstackRequest($this->stack, $this);
8080
}
8181
/**
8282
* Query object to create the "Query" on the specified ContentType

src/Stack/ContentType/Entry.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ public function __construct($entryUid = '', $contentType = '')
6060
public function fetch()
6161
{
6262
$this->operation = __FUNCTION__;
63-
return Utility::contentstackRequest($this);
63+
return Utility::contentstackRequest($this->contentType->stack, $this);
6464
}
6565
}

src/Stack/ContentType/Query.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ public function find()
5656
{
5757
$this->operation = __FUNCTION__;
5858
if ($this->type == 'assets') {
59-
return Utility::contentstackRequest($this, 'assets');
59+
return Utility::contentstackRequest($this->assets->stack, $this, 'assets');
6060
} else if ($this->type == 'contentType') {
61-
return Utility::contentstackRequest($this);
61+
return Utility::contentstackRequest($this->contentType->stack, $this);
6262
}
6363
}
6464

@@ -75,9 +75,9 @@ public function findOne()
7575
$this->operation = __FUNCTION__;
7676
$this->_query['limit'] = 1;
7777
if ($this->type == 'assets') {
78-
return Utility::contentstackRequest($this, 'assets');
78+
return Utility::contentstackRequest($this->assets->stack, $this, 'assets');
7979
} elseif ($this->type == 'contentType') {
80-
return Utility::contentstackRequest($this);
80+
return Utility::contentstackRequest($this->contentType->stack, $this);
8181
}
8282
}
8383
}

src/Stack/Stack.php

+7-2
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

@@ -332,7 +337,7 @@ public function getContentTypes($params)
332337
$this->_query = $myArray;
333338
}
334339

335-
return Utility::contentstackRequest($this, "getcontentTypes");
340+
return Utility::contentstackRequest($this, $this, "getcontentTypes");
336341
}
337342

338343
/**
@@ -347,6 +352,6 @@ public function sync($params)
347352
if ($params && $params !== "undefined") {
348353
$this->_query = $params;
349354
}
350-
return Utility::contentstackRequest($this, "sync");
355+
return Utility::contentstackRequest($this, $this, "sync");
351356
}
352357
}

src/Support/Utility.php

+47-9
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,11 @@ public static function wrapResult($result = '', $queryObject = '')
375375
*
376376
* @return Result
377377
* */
378-
public static function contentstackRequest($queryObject = '', $type = '')
378+
public static function contentstackRequest($stack, $queryObject = '', $type = '', $count = 0)
379379
{
380-
$server_output = '';
381-
380+
$retryDelay = $stack->retryDelay;
381+
$retryLimit = $stack->retryLimit;
382+
$errorRetry = $stack->errorRetry;
382383
if ($queryObject) {
383384
if (Utility::isLivePreview($queryObject)) {
384385
$queryObject->_query['live_preview'] = ($queryObject->contentType->stack->live_preview['live_preview'] ?? 'init');
@@ -399,30 +400,67 @@ public static function contentstackRequest($queryObject = '', $type = '')
399400
if ($Headers["branch"] !== '' && $Headers["branch"] !== "undefined") {
400401
$request_headers[] = 'branch: '.$Headers["branch"];
401402
}
403+
404+
$proxy_details = $stack->proxy;
405+
$timeout = $stack->timeout;
406+
402407
curl_setopt($http, CURLOPT_HTTPHEADER, $request_headers);
403408

404409
curl_setopt($http, CURLOPT_HEADER, false);
405410
// setting the GET request
406411
curl_setopt($http, CURLOPT_CUSTOMREQUEST, "GET");
407412
// receive server response ...
408413
curl_setopt($http, CURLOPT_RETURNTRANSFER, true);
409-
$response = curl_exec($http);
414+
// set the cURL time out
415+
curl_setopt($http, CURLOPT_TIMEOUT_MS, $timeout);
416+
417+
if(array_***_exists("url",$proxy_details) && array_***_exists("port",$proxy_details)){
418+
if($proxy_details['url'] != '' && $proxy_details['port'] != '') {
410419

420+
// Set the proxy IP
421+
curl_setopt($http, CURLOPT_PROXY, $proxy_details['url']);
422+
// Set the port
423+
curl_setopt($http, CURLOPT_PROXYPORT, $proxy_details['port']);
424+
425+
if(array_***_exists("username",$proxy_details) && array_***_exists("password",$proxy_details)){
426+
if($proxy_details['username'] != '' && $proxy_details['password'] != '') {
427+
428+
$proxyauth = $proxy_details['username'].":".$proxy_details['password'];
429+
// Set the username and password
430+
curl_setopt($http, CURLOPT_PROXYUSERPWD, $proxyauth);
431+
432+
}
433+
}
434+
}
435+
}
436+
437+
$response = curl_exec($http);
411438
// status code extraction
412439
$httpcode = curl_getinfo($http, CURLINFO_HTTP_CODE);
413-
440+
414441
// close the curl
415442
curl_close($http);
416-
if ($httpcode > 199 && $httpcode < 300) {
417-
// wrapper the server result
418-
$response = Utility::wrapResult($response, $queryObject);
443+
if(in_array($httpcode,$errorRetry)){
444+
if($count < $retryLimit){
445+
$retryDelay = round($retryDelay/1000); //converting retry_delay from milliseconds into seconds
446+
sleep($retryDelay); //sleep method requires time in seconds
447+
$count += 1;
448+
return Utility::contentstackRequest($stack, $queryObject, $type, $count);
449+
}
419450
} else {
420-
throw new CSException($response, $httpcode);
451+
if ($httpcode > 199 && $httpcode < 300) {
452+
// wrapper the server result
453+
$response = Utility::wrapResult($response, $queryObject);
454+
}
455+
else{
456+
throw new CSException($response, $httpcode);
457+
}
421458
}
422459
}
423460
return $response;
424461
}
425462

463+
426464
/**
427465
* Validate the *** is set or not
428466
*

test/EntriesTest.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ public function testLivePreviewEntrywithQuery () {
6666
try {
6767
self::$LivePreviewStack->livePreviewQuery(array('content_type_uid' => CT_ContentType));
6868
$_entry = self::$LivePreviewStack->ContentType(CT_ContentType)->Entry(self::$_uid)->toJSON()->fetch();
69-
7069
} catch (Exception $e) {
7170
$this->assertTrue(true);
7271
}
@@ -178,9 +177,8 @@ public function testFindIncludeContentType() {
178177

179178
public function testFindIncludeEmbeddedItems() {
180179
$_entries = self::$Stack->ContentType(CT_ContentType)->Query()->toJSON()->includeEmbeddedItems()->find();
181-
182180
for($i = 0; $i < count($_entries[0]); $i++) {
183-
if ($_entries[0][$i]["rich_text_editor"]) {
181+
if (array_***_exists('rich_text_editor', $_entries[0][$i])) {
184182
$embedded = Contentstack::renderContent($_entries[0][$i]["rich_text_editor"], new Option($_entries[0][$i]));
185183
}
186184
}
@@ -340,12 +338,16 @@ public function testFindDescending() {
340338
public function testGetContentTypes() {
341339
$globalfield = '{"include_global_field_schema": "true"}';
342340
$content_type = self::$Stack->getContentTypes($globalfield);
343-
for($i = 0; $i < count($content_type['content_types'][1]['schema']); $i++) {
344-
if($content_type['content_types'][1]['schema'][$i]['data_type'] === 'global_field') {
345-
$flag = (isset($content_type['content_types'][1]['schema'][$i]['schema']));
346-
$this->assertTrue($flag);
341+
for ($j = 0; $j < count($content_type['content_types']); $j++)
342+
{
343+
for($i = 0; $i < count($content_type['content_types'][$j]['schema']); $i++) {
344+
if($content_type['content_types'][$j]['schema'][$i]['data_type'] === 'global_field') {
345+
$flag = (isset($content_type['content_types'][$j]['schema'][$i]['schema']));
346+
$this->assertTrue($flag);
347+
}
347348
}
348349
}
350+
349351
}
350352

351353
public function testFindLogicalOrQueryObject() {

0 commit comments

Comments
 (0)