Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Stack/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function Query()
public function fetch()
{
$this->operation = __FUNCTION__;
return Utility::contentstackRequest($this, 'asset');
return Utility::contentstackRequest($this->stack, $this, 'asset');
}
}

264 changes: 261 additions & 3 deletions src/Stack/BaseQuery.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Stack/ContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function fetch($params = null)
$myArray = json_decode($params, true);
$this->_query = $myArray;
}
return Utility::contentstackRequest($this);
return Utility::contentstackRequest($this->stack, $this);
}
/**
* Query object to create the "Query" on the specified ContentType
Expand Down
2 changes: 1 addition & 1 deletion src/Stack/ContentType/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ public function __construct($entryUid = '', $contentType = '')
public function fetch()
{
$this->operation = __FUNCTION__;
return Utility::contentstackRequest($this);
return Utility::contentstackRequest($this->contentType->stack, $this);
}
}
8 changes: 4 additions & 4 deletions src/Stack/ContentType/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public function find()
{
$this->operation = __FUNCTION__;
if ($this->type == 'assets') {
return Utility::contentstackRequest($this, 'assets');
return Utility::contentstackRequest($this->assets->stack, $this, 'assets');
} else if ($this->type == 'contentType') {
return Utility::contentstackRequest($this);
return Utility::contentstackRequest($this->contentType->stack, $this);
}
}

Expand All @@ -75,9 +75,9 @@ public function findOne()
$this->operation = __FUNCTION__;
$this->_query['limit'] = 1;
if ($this->type == 'assets') {
return Utility::contentstackRequest($this, 'assets');
return Utility::contentstackRequest($this->assets->stack, $this, 'assets');
} elseif ($this->type == 'contentType') {
return Utility::contentstackRequest($this);
return Utility::contentstackRequest($this->contentType->stack, $this);
}
}
}
9 changes: 7 additions & 2 deletions src/Stack/Stack.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public function __construct(
unset($this->header['environment']);
$livePreview = array('enable' => false, 'host' => $previewHost);
$this->live_preview = $config['live_preview'] ? array_merge($livePreview, $config['live_preview']) : $livePreview;
$this->proxy = array_key_exists("proxy",$config) ? $config['proxy'] : array('proxy'=>array());
$this->timeout = array_key_exists("timeout",$config) ? $config['timeout'] : '3000';
$this->retryDelay = array_key_exists("retryDelay",$config) ? $config['retryDelay'] : '3000';
$this->retryLimit = array_key_exists("retryLimit",$config) ? $config['retryLimit'] : '5';
$this->errorRetry = array_key_exists("errorRetry",$config) ? $config['errorRetry'] : array('errorRetry'=>array(408, 429));
return $this;
}

Expand Down Expand Up @@ -332,7 +337,7 @@ public function getContentTypes($params)
$this->_query = $myArray;
}

return Utility::contentstackRequest($this, "getcontentTypes");
return Utility::contentstackRequest($this, $this, "getcontentTypes");
}

/**
Expand All @@ -347,6 +352,6 @@ public function sync($params)
if ($params && $params !== "undefined") {
$this->_query = $params;
}
return Utility::contentstackRequest($this, "sync");
return Utility::contentstackRequest($this, $this, "sync");
}
}
56 changes: 47 additions & 9 deletions src/Support/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,11 @@ public static function wrapResult($result = '', $queryObject = '')
*
* @return Result
* */
public static function contentstackRequest($queryObject = '', $type = '')
public static function contentstackRequest($stack, $queryObject = '', $type = '', $count = 0)
{
$server_output = '';

$retryDelay = $stack->retryDelay;
$retryLimit = $stack->retryLimit;
$errorRetry = $stack->errorRetry;
if ($queryObject) {
if (Utility::isLivePreview($queryObject)) {
$queryObject->_query['live_preview'] = ($queryObject->contentType->stack->live_preview['live_preview'] ?? 'init');
Expand All @@ -399,30 +400,67 @@ public static function contentstackRequest($queryObject = '', $type = '')
if ($Headers["branch"] !== '' && $Headers["branch"] !== "undefined") {
$request_headers[] = 'branch: '.$Headers["branch"];
}

$proxy_details = $stack->proxy;
$timeout = $stack->timeout;

curl_setopt($http, CURLOPT_HTTPHEADER, $request_headers);

curl_setopt($http, CURLOPT_HEADER, false);
// setting the GET request
curl_setopt($http, CURLOPT_CUSTOMREQUEST, "GET");
// receive server response ...
curl_setopt($http, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($http);
// set the cURL time out
curl_setopt($http, CURLOPT_TIMEOUT_MS, $timeout);

if(array_key_exists("url",$proxy_details) && array_key_exists("port",$proxy_details)){
if($proxy_details['url'] != '' && $proxy_details['port'] != '') {

// Set the proxy IP
curl_setopt($http, CURLOPT_PROXY, $proxy_details['url']);
// Set the port
curl_setopt($http, CURLOPT_PROXYPORT, $proxy_details['port']);

if(array_key_exists("username",$proxy_details) && array_key_exists("password",$proxy_details)){
if($proxy_details['username'] != '' && $proxy_details['password'] != '') {

$proxyauth = $proxy_details['username'].":".$proxy_details['password'];
// Set the username and password
curl_setopt($http, CURLOPT_PROXYUSERPWD, $proxyauth);

}
}
}
}

$response = curl_exec($http);
// status code extraction
$httpcode = curl_getinfo($http, CURLINFO_HTTP_CODE);

// close the curl
curl_close($http);
if ($httpcode > 199 && $httpcode < 300) {
// wrapper the server result
$response = Utility::wrapResult($response, $queryObject);
if(in_array($httpcode,$errorRetry)){
if($count < $retryLimit){
$retryDelay = round($retryDelay/1000); //converting retry_delay from milliseconds into seconds
sleep($retryDelay); //sleep method requires time in seconds
$count += 1;
return Utility::contentstackRequest($stack, $queryObject, $type, $count);
}
} else {
throw new CSException($response, $httpcode);
if ($httpcode > 199 && $httpcode < 300) {
// wrapper the server result
$response = Utility::wrapResult($response, $queryObject);
}
else{
throw new CSException($response, $httpcode);
}
}
}
return $response;
}


/**
* Validate the key is set or not
*
Expand Down
16 changes: 9 additions & 7 deletions test/EntriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public function testLivePreviewEntrywithQuery () {
try {
self::$LivePreviewStack->livePreviewQuery(array('content_type_uid' => CT_ContentType));
$_entry = self::$LivePreviewStack->ContentType(CT_ContentType)->Entry(self::$_uid)->toJSON()->fetch();

} catch (Exception $e) {
$this->assertTrue(true);
}
Expand Down Expand Up @@ -178,9 +177,8 @@ public function testFindIncludeContentType() {

public function testFindIncludeEmbeddedItems() {
$_entries = self::$Stack->ContentType(CT_ContentType)->Query()->toJSON()->includeEmbeddedItems()->find();

for($i = 0; $i < count($_entries[0]); $i++) {
if ($_entries[0][$i]["rich_text_editor"]) {
if (array_key_exists('rich_text_editor', $_entries[0][$i])) {
$embedded = Contentstack::renderContent($_entries[0][$i]["rich_text_editor"], new Option($_entries[0][$i]));
}
}
Expand Down Expand Up @@ -340,12 +338,16 @@ public function testFindDescending() {
public function testGetContentTypes() {
$globalfield = '{"include_global_field_schema": "true"}';
$content_type = self::$Stack->getContentTypes($globalfield);
for($i = 0; $i < count($content_type['content_types'][1]['schema']); $i++) {
if($content_type['content_types'][1]['schema'][$i]['data_type'] === 'global_field') {
$flag = (isset($content_type['content_types'][1]['schema'][$i]['schema']));
$this->assertTrue($flag);
for ($j = 0; $j < count($content_type['content_types']); $j++)
{
for($i = 0; $i < count($content_type['content_types'][$j]['schema']); $i++) {
if($content_type['content_types'][$j]['schema'][$i]['data_type'] === 'global_field') {
$flag = (isset($content_type['content_types'][$j]['schema'][$i]['schema']));
$this->assertTrue($flag);
}
}
}

}

public function testFindLogicalOrQueryObject() {
Expand Down