@@ -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_key_exists ("url " ,$ proxy_details ) && array_key_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_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 );
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 key is set or not
428466 *
0 commit comments