1919 */
2020class HttpRequestJson
2121{
22-
2322 /**
2423 * HTTP request headers
2524 *
@@ -32,7 +31,7 @@ class HttpRequestJson
3231 *
3332 * @var string
3433 */
35- private static $ postDataJSON ;
34+ protected static $ postDataJSON ;
3635
3736
3837 /**
@@ -68,9 +67,7 @@ public static function get($url, $httpHeaders = array())
6867 {
6968 self ::prepareRequest ($ httpHeaders );
7069
71- $ response = CurlRequest::get ($ url , self ::$ httpHeaders );
72-
73- return self ::processResponse ($ response );
70+ return self ::processRequest ('GET ' , $ url );
7471 }
7572
7673 /**
@@ -86,9 +83,7 @@ public static function post($url, $dataArray, $httpHeaders = array())
8683 {
8784 self ::prepareRequest ($ httpHeaders , $ dataArray );
8885
89- $ response = CurlRequest::post ($ url , self ::$ postDataJSON , self ::$ httpHeaders );
90-
91- return self ::processResponse ($ response );
86+ return self ::processRequest ('POST ' , $ url );
9287 }
9388
9489 /**
@@ -104,9 +99,7 @@ public static function put($url, $dataArray, $httpHeaders = array())
10499 {
105100 self ::prepareRequest ($ httpHeaders , $ dataArray );
106101
107- $ response = CurlRequest::put ($ url , self ::$ postDataJSON , self ::$ httpHeaders );
108-
109- return self ::processResponse ($ response );
102+ return self ::processRequest ('PUT ' , $ url );
110103 }
111104
112105 /**
@@ -121,9 +114,68 @@ public static function delete($url, $httpHeaders = array())
121114 {
122115 self ::prepareRequest ($ httpHeaders );
123116
124- $ response = CurlRequest::delete ($ url , self ::$ httpHeaders );
117+ return self ::processRequest ('DELETE ' , $ url );
118+ }
119+
120+ /**
121+ * Process a curl request and return decoded JSON response
122+ *
123+ * @param string $method Request http method ('GET', 'POST', 'PUT' or 'DELETE')
124+ * @param string $url Request URL
125+ *
126+ * @throws CurlException if response received with unexpected HTTP code.
127+ *
128+ * @return array
129+ */
130+ public static function processRequest ($ method , $ url ) {
131+ $ retry = 0 ;
132+ $ raw = null ;
133+
134+ while (true ) {
135+ try {
136+ switch ($ method ) {
137+ case 'GET ' :
138+ $ raw = CurlRequest::get ($ url , self ::$ httpHeaders );
139+ break ;
140+ case 'POST ' :
141+ $ raw = CurlRequest::post ($ url , self ::$ postDataJSON , self ::$ httpHeaders );
142+ break ;
143+ case 'PUT ' :
144+ $ raw = CurlRequest::put ($ url , self ::$ postDataJSON , self ::$ httpHeaders );
145+ break ;
146+ case 'DELETE ' :
147+ $ raw = CurlRequest::delete ($ url , self ::$ httpHeaders );
148+ break ;
149+ default :
150+ throw new \Exception ("unexpected request method ' $ method' " );
151+ }
152+
153+ return self ::processResponse ($ raw );
154+ } catch (\Exception $ e ) {
155+ if (!self ::shouldRetry ($ raw , $ e , $ retry ++)) {
156+ throw $ e ;
157+ }
158+ }
159+ }
160+ }
161+
162+ /**
163+ * Evaluate if send again a request
164+ *
165+ * @param string $response Raw request response
166+ * @param exception $error the request error occured
167+ * @param integer $retry the current number of retry
168+ *
169+ * @return bool
170+ */
171+ public static function shouldRetry ($ response , $ error , $ retry ) {
172+ $ config = ShopifySDK::$ config ;
173+
174+ if (isset ($ config ['RequestRetryCallback ' ])) {
175+ return $ config ['RequestRetryCallback ' ]($ response , $ error , $ retry );
176+ }
125177
126- return self :: processResponse ( $ response ) ;
178+ return false ;
127179 }
128180
129181 /**
@@ -135,8 +187,29 @@ public static function delete($url, $httpHeaders = array())
135187 */
136188 protected static function processResponse ($ response )
137189 {
190+ $ responseArray = json_decode ($ response , true );
138191
139- return json_decode ($ response , true );
140- }
192+ if ($ responseArray === null ) {
193+ //Something went wrong, Checking HTTP Codes
194+ $ httpOK = 200 ; //Request Successful, OK.
195+ $ httpCreated = 201 ; //Create Successful.
196+ $ httpDeleted = 204 ; //Delete Successful
197+ $ httpOther = 303 ; //See other (headers).
198+
199+ $ lastHttpResponseHeaders = CurlRequest::$ lastHttpResponseHeaders ;
200+
201+ //should be null if any other library used for http calls
202+ $ httpCode = CurlRequest::$ lastHttpCode ;
203+
204+ if ($ httpCode == $ httpOther && array_key_exists ('location ' , $ lastHttpResponseHeaders )) {
205+ return ['location ' => $ lastHttpResponseHeaders ['location ' ]];
206+ }
141207
142- }
208+ if ($ httpCode != null && $ httpCode != $ httpOK && $ httpCode != $ httpCreated && $ httpCode != $ httpDeleted ) {
209+ throw new Exception \CurlException ("Request failed with HTTP Code $ httpCode. " , $ httpCode );
210+ }
211+ }
212+
213+ return $ responseArray ;
214+ }
215+ }
0 commit comments