GeezerVison is showing github.com — Merge pull request #29 from contentstack/feature/PHP-proxy · contentstack/contentstack-php@472e0fa
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/Stack/Assets.php +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -81,7 +81,7 @@ public function Query() | 81 | 81 | public function fetch() | 82 | 82 | { | 83 | 83 | $this->operation = __FUNCTION__; | 84 | | - return Utility::contentstackRequest($this, 'asset'); | | 84 | + return Utility::contentstackRequest($this->stack, $this, 'asset'); | 85 | 85 | } | 86 | 86 | } | 87 | 87 |
|
Collapse file Copy file name to clipboardExpand all lines: src/Stack/BaseQuery.php +261-3Lines changed: 261 additions & 3 deletions Large diffs are not rendered by default. Collapse file Copy file name to clipboardExpand all lines: src/Stack/ContentType.php +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -76,7 +76,7 @@ public function fetch($params = null) | 76 | 76 | $myArray = json_decode($params, true); | 77 | 77 | $this->_query = $myArray; | 78 | 78 | } | 79 | | - return Utility::contentstackRequest($this); | | 79 | + return Utility::contentstackRequest($this->stack, $this); | 80 | 80 | } | 81 | 81 | /** | 82 | 82 | * Query object to create the "Query" on the specified ContentType | |
Collapse file Copy file name to clipboardExpand all lines: src/Stack/ContentType/Entry.php +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -60,6 +60,6 @@ public function __construct($entryUid = '', $contentType = '') | 60 | 60 | public function fetch() | 61 | 61 | { | 62 | 62 | $this->operation = __FUNCTION__; | 63 | | - return Utility::contentstackRequest($this); | | 63 | + return Utility::contentstackRequest($this->contentType->stack, $this); | 64 | 64 | } | 65 | 65 | } |
Collapse file Copy file name to clipboardExpand all lines: src/Stack/ContentType/Query.php +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -56,9 +56,9 @@ public function find() | 56 | 56 | { | 57 | 57 | $this->operation = __FUNCTION__; | 58 | 58 | if ($this->type == 'assets') { | 59 | | - return Utility::contentstackRequest($this, 'assets'); | | 59 | + return Utility::contentstackRequest($this->assets->stack, $this, 'assets'); | 60 | 60 | } else if ($this->type == 'contentType') { | 61 | | - return Utility::contentstackRequest($this); | | 61 | + return Utility::contentstackRequest($this->contentType->stack, $this); | 62 | 62 | } | 63 | 63 | } | 64 | 64 |
| @@ -75,9 +75,9 @@ public function findOne() | 75 | 75 | $this->operation = __FUNCTION__; | 76 | 76 | $this->_query['limit'] = 1; | 77 | 77 | if ($this->type == 'assets') { | 78 | | - return Utility::contentstackRequest($this, 'assets'); | | 78 | + return Utility::contentstackRequest($this->assets->stack, $this, 'assets'); | 79 | 79 | } elseif ($this->type == 'contentType') { | 80 | | - return Utility::contentstackRequest($this); | | 80 | + return Utility::contentstackRequest($this->contentType->stack, $this); | 81 | 81 | } | 82 | 82 | } | 83 | 83 | } |
Collapse file Copy file name to clipboardExpand all lines: src/Stack/Stack.php +7-2Lines changed: 7 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -73,6 +73,11 @@ public function __construct( | 73 | 73 | unset($this->header['environment']); | 74 | 74 | $livePreview = array('enable' => false, 'host' => $previewHost); | 75 | 75 | $this->live_preview = $config['live_preview'] ? array_merge($livePreview, $config['live_preview']) : $livePreview; | | 76 | + $this->proxy = array_key_exists("proxy",$config) ? $config['proxy'] : array('proxy'=>array()); | | 77 | + $this->timeout = array_key_exists("timeout",$config) ? $config['timeout'] : '3000'; | | 78 | + $this->retryDelay = array_key_exists("retryDelay",$config) ? $config['retryDelay'] : '3000'; | | 79 | + $this->retryLimit = array_key_exists("retryLimit",$config) ? $config['retryLimit'] : '5'; | | 80 | + $this->errorRetry = array_key_exists("errorRetry",$config) ? $config['errorRetry'] : array('errorRetry'=>array(408, 429)); | 76 | 81 | return $this; | 77 | 82 | } | 78 | 83 |
| @@ -332,7 +337,7 @@ public function getContentTypes($params) | 332 | 337 | $this->_query = $myArray; | 333 | 338 | } | 334 | 339 |
| 335 | | - return Utility::contentstackRequest($this, "getcontentTypes"); | | 340 | + return Utility::contentstackRequest($this, $this, "getcontentTypes"); | 336 | 341 | } | 337 | 342 |
| 338 | 343 | /** | @@ -347,6 +352,6 @@ public function sync($params) | 347 | 352 | if ($params && $params !== "undefined") { | 348 | 353 | $this->_query = $params; | 349 | 354 | } | 350 | | - return Utility::contentstackRequest($this, "sync"); | | 355 | + return Utility::contentstackRequest($this, $this, "sync"); | 351 | 356 | } | 352 | 357 | } |
Collapse file Copy file name to clipboardExpand all lines: src/Support/Utility.php +47-9Lines changed: 47 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -375,10 +375,11 @@ public static function wrapResult($result = '', $queryObject = '') | 375 | 375 | * | 376 | 376 | * @return Result | 377 | 377 | * */ | 378 | | - public static function contentstackRequest($queryObject = '', $type = '') | | 378 | + public static function contentstackRequest($stack, $queryObject = '', $type = '', $count = 0) | 379 | 379 | { | 380 | | - $server_output = ''; | 381 | | - | | 380 | + $retryDelay = $stack->retryDelay; | | 381 | + $retryLimit = $stack->retryLimit; | | 382 | + $errorRetry = $stack->errorRetry; | 382 | 383 | if ($queryObject) { | 383 | 384 | if (Utility::isLivePreview($queryObject)) { | 384 | 385 | $queryObject->_query['live_preview'] = ($queryObject->contentType->stack->live_preview['live_preview'] ?? 'init'); | @@ -399,30 +400,67 @@ public static function contentstackRequest($queryObject = '', $type = '') | 399 | 400 | if ($Headers["branch"] !== '' && $Headers["branch"] !== "undefined") { | 400 | 401 | $request_headers[] = 'branch: '.$Headers["branch"]; | 401 | 402 | } | | 403 | + | | 404 | + $proxy_details = $stack->proxy; | | 405 | + $timeout = $stack->timeout; | | 406 | + | 402 | 407 | curl_setopt($http, CURLOPT_HTTPHEADER, $request_headers); | 403 | 408 |
| 404 | 409 | curl_setopt($http, CURLOPT_HEADER, false); | 405 | 410 | // setting the GET request | 406 | 411 | curl_setopt($http, CURLOPT_CUSTOMREQUEST, "GET"); | 407 | 412 | // receive server response ... | 408 | 413 | 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_key_exists("url",$proxy_details) && array_key_exists("port",$proxy_details)){ | | 418 | + if($proxy_details['url'] != '' && $proxy_details['port'] != '') { | 410 | 419 |
| | 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_key_exists("username",$proxy_details) && array_key_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); | 411 | 438 | // status code extraction | 412 | 439 | $httpcode = curl_getinfo($http, CURLINFO_HTTP_CODE); | 413 | | - | | 440 | + | 414 | 441 | // close the curl | 415 | 442 | 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 | + } | 419 | 450 | } 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 | + } | 421 | 458 | } | 422 | 459 | } | 423 | 460 | return $response; | 424 | 461 | } | 425 | 462 |
| | 463 | + | 426 | 464 | /** | 427 | 465 | * Validate the key is set or not | 428 | 466 | * | |
Collapse file Copy file name to clipboardExpand all lines: test/EntriesTest.php +9-7Lines changed: 9 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -66,7 +66,6 @@ public function testLivePreviewEntrywithQuery () { | 66 | 66 | try { | 67 | 67 | self::$LivePreviewStack->livePreviewQuery(array('content_type_uid' => CT_ContentType)); | 68 | 68 | $_entry = self::$LivePreviewStack->ContentType(CT_ContentType)->Entry(self::$_uid)->toJSON()->fetch(); | 69 | | - | 70 | 69 | } catch (Exception $e) { | 71 | 70 | $this->assertTrue(true); | 72 | 71 | } | @@ -178,9 +177,8 @@ public function testFindIncludeContentType() { | 178 | 177 |
| 179 | 178 | public function testFindIncludeEmbeddedItems() { | 180 | 179 | $_entries = self::$Stack->ContentType(CT_ContentType)->Query()->toJSON()->includeEmbeddedItems()->find(); | 181 | | - | 182 | 180 | for($i = 0; $i < count($_entries[0]); $i++) { | 183 | | - if ($_entries[0][$i]["rich_text_editor"]) { | | 181 | + if (array_key_exists('rich_text_editor', $_entries[0][$i])) { | 184 | 182 | $embedded = Contentstack::renderContent($_entries[0][$i]["rich_text_editor"], new Option($_entries[0][$i])); | 185 | 183 | } | 186 | 184 | } | @@ -340,12 +338,16 @@ public function testFindDescending() { | 340 | 338 | public function testGetContentTypes() { | 341 | 339 | $globalfield = '{"include_global_field_schema": "true"}'; | 342 | 340 | $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 | + } | 347 | 348 | } | 348 | 349 | } | | 350 | + | 349 | 351 | } | 350 | 352 |
| 351 | 353 | public function testFindLogicalOrQueryObject() { | |
|