Skip to content

Commit 2977a44

Browse files
Resolved merge conflits
2 parents f81508b + a5ad378 commit 2977a44

File tree

8 files changed

+336
-28
lines changed

8 files changed

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

@@ -337,7 +342,7 @@ public function getContentTypes($params)
337342
$this->_query = $myArray;
338343
}
339344

340-
return Utility::contentstackRequest($this, "getcontentTypes");
345+
return Utility::contentstackRequest($this, $this, "getcontentTypes");
341346
}
342347

343348
/**
@@ -352,6 +357,6 @@ public function sync($params)
352357
if ($params && $params !== "undefined") {
353358
$this->_query = $params;
354359
}
355-
return Utility::contentstackRequest($this, "sync");
360+
return Utility::contentstackRequest($this, $this, "sync");
356361
}
357362
}

src/Support/Utility.php

+52-9
Original file line numberDiff line numberDiff line change
@@ -375,13 +375,16 @@ 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
{
380380
$server_output = '';
381381
STATIC $live_response_decode = '';
382382
STATIC $entry_uid = '';
383383
STATIC $content_type_uid = '';
384384

385+
$retryDelay = $stack->retryDelay;
386+
$retryLimit = $stack->retryLimit;
387+
$errorRetry = $stack->errorRetry;
385388
if ($queryObject) {
386389
if (Utility::isLivePreview($queryObject)) {
387390
$queryObject->_query['live_preview'] = ($queryObject->contentType->stack->live_preview['live_preview'] ?? 'init');
@@ -402,22 +405,57 @@ public static function contentstackRequest($queryObject = '', $type = '')
402405
if ($Headers["branch"] !== '' && $Headers["branch"] !== "undefined") {
403406
$request_headers[] = 'branch: '.$Headers["branch"];
404407
}
408+
409+
$proxy_details = $stack->proxy;
410+
$timeout = $stack->timeout;
411+
405412
curl_setopt($http, CURLOPT_HTTPHEADER, $request_headers);
406413

407414
curl_setopt($http, CURLOPT_HEADER, false);
408415
// setting the GET request
409416
curl_setopt($http, CURLOPT_CUSTOMREQUEST, "GET");
410417
// receive server response ...
411418
curl_setopt($http, CURLOPT_RETURNTRANSFER, true);
412-
$response = curl_exec($http);
419+
// set the cURL time out
420+
curl_setopt($http, CURLOPT_TIMEOUT_MS, $timeout);
413421

422+
if(array_***_exists("url",$proxy_details) && array_***_exists("port",$proxy_details)){
423+
if($proxy_details['url'] != '' && $proxy_details['port'] != '') {
424+
425+
// Set the proxy IP
426+
curl_setopt($http, CURLOPT_PROXY, $proxy_details['url']);
427+
// Set the port
428+
curl_setopt($http, CURLOPT_PROXYPORT, $proxy_details['port']);
429+
430+
if(array_***_exists("username",$proxy_details) && array_***_exists("password",$proxy_details)){
431+
if($proxy_details['username'] != '' && $proxy_details['password'] != '') {
432+
433+
$proxyauth = $proxy_details['username'].":".$proxy_details['password'];
434+
// Set the username and password
435+
curl_setopt($http, CURLOPT_PROXYUSERPWD, $proxyauth);
436+
437+
}
438+
}
439+
}
440+
}
441+
442+
$response = curl_exec($http);
414443
// status code extraction
415444
$httpcode = curl_getinfo($http, CURLINFO_HTTP_CODE);
416-
445+
417446
// close the curl
418447
curl_close($http);
419-
if ($httpcode > 199 && $httpcode < 300) {
420-
if (!Utility::isLivePreview($queryObject)) {
448+
449+
if(in_array($httpcode,$errorRetry)){
450+
if($count < $retryLimit){
451+
$retryDelay = round($retryDelay/1000); //converting retry_delay from milliseconds into seconds
452+
sleep($retryDelay); //sleep method requires time in seconds
453+
$count += 1;
454+
return Utility::contentstackRequest($stack, $queryObject, $type, $count);
455+
}
456+
} else {
457+
if ($httpcode > 199 && $httpcode < 300) {
458+
if (!Utility::isLivePreview($queryObject)) {
421459
$result = json_decode($response, true);
422460
Utility::to_render_content($result, $entry_uid, $live_response_decode);
423461
$response = json_encode($result, true);
@@ -430,15 +468,19 @@ public static function contentstackRequest($queryObject = '', $type = '')
430468
$live_response_decode = json_decode($response, true);
431469

432470
}
433-
// wrapper the server result
434-
$response = Utility::wrapResult($response, $queryObject);
435-
} else {
436-
throw new CSException($response, $httpcode);
471+
// wrapper the server result
472+
$response = Utility::wrapResult($response, $queryObject);
473+
}
474+
else{
475+
throw new CSException($response, $httpcode);
476+
}
437477
}
438478
}
479+
439480
return $response;
440481
}
441482

483+
442484
public static function to_render_content(&$resp, $entry_uid, $live_response_decode ){
443485
if (is_array($resp)) {
444486
if(array_***_exists('uid', $resp) && $resp['uid'] == $entry_uid){
@@ -452,6 +494,7 @@ public static function to_render_content(&$resp, $entry_uid, $live_response_deco
452494
}
453495
}
454496

497+
455498
/**
456499
* Validate the *** is set or not
457500
*

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)