Skip to content

Commit ea200d0

Browse files
committed
Automatically re-run API requests that fail with no error/code (likely timeouts)
1 parent e52c0c1 commit ea200d0

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

src/WC/API/Client.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class WC_API_Client
1818
*/
1919
const HASH_ALGORITHM = 'SHA256';
2020

21-
const VERSION = '0.3.7';
21+
const VERSION = '0.3.8';
2222
/**
2323
* The API URL
2424
*
@@ -61,6 +61,7 @@ class WC_API_Client
6161
* @param string $consumer_secret The consumer secret
6262
* @param string $store_url The URL to the WooCommerce store
6363
* @param boolean $is_ssl If the URL is secure or not, optional
64+
* @throws Exception
6465
*/
6566
public function __construct( $consumer_key, $consumer_secret, $store_url, $is_ssl = false )
6667
{
@@ -93,7 +94,7 @@ public function get_index()
9394
*
9495
* @param array $params
9596
*
96-
* @return mixed|jason string
97+
* @return mixed|json string
9798
*/
9899
public function get_orders( $params = array() )
99100
{
@@ -577,7 +578,7 @@ public function set_return_as_object( $is_object = true )
577578
* Make the call to the API
578579
*
579580
* @param string $endpoint
580-
* @param array $params
581+
* @param array $data
581582
* @param string $method
582583
*
583584
* @return mixed|json string
@@ -632,15 +633,36 @@ private function _make_api_call( $endpoint, $data = array(), $method = 'GET' )
632633

633634
$return = curl_exec( $ch );
634635

635-
$code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
636+
$code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
636637

637638
if ($this->_return_as_object) {
638639
$return = json_decode( $return );
639640
}
640641

641642
if (empty( $return )) {
642-
$return = '{"errors":[{"code":"' . $code . '","message":"cURL HTTP error ' . $code . '"}]}';
643-
$return = json_decode( $return );
643+
644+
// If the code returned is 0 it's possible/likely that the request timed out, let's try to run it again
645+
// (We're having issues on the client site that this package is being used on where the WooCommerce API is timing out on some
646+
// queries, until we can diagnose and fix that issue we're going to attempt to re-run those requests here)
647+
if($code == 0)
648+
{
649+
// Take a short break as to not hammer the already failing API endpoint
650+
sleep(1);
651+
652+
// Re-run the request
653+
$return = curl_exec( $ch );
654+
$code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
655+
if ($this->_return_as_object) {
656+
$return = json_decode( $return );
657+
}
658+
659+
// If the request still failed we'll return the error
660+
if( empty($return))
661+
{
662+
$return = '{"errors":[{"code":"' . $code . '","message":"cURL HTTP error ' . $code . '"}]}';
663+
$return = json_decode( $return );
664+
}
665+
}
644666
}
645667

646668
return $return;

0 commit comments

Comments
 (0)