19
19
*/
20
20
class HttpRequestJson
21
21
{
22
-
23
22
/**
24
23
* HTTP request headers
25
24
*
@@ -32,7 +31,7 @@ class HttpRequestJson
32
31
*
33
32
* @var string
34
33
*/
35
- private static $ postDataJSON ;
34
+ protected static $ postDataJSON ;
36
35
37
36
38
37
/**
@@ -68,9 +67,7 @@ public static function get($url, $httpHeaders = array())
68
67
{
69
68
self ::prepareRequest ($ httpHeaders );
70
69
71
- $ response = CurlRequest::get ($ url , self ::$ httpHeaders );
72
-
73
- return self ::processResponse ($ response );
70
+ return self ::processRequest ('GET ' , $ url );
74
71
}
75
72
76
73
/**
@@ -86,9 +83,7 @@ public static function post($url, $dataArray, $httpHeaders = array())
86
83
{
87
84
self ::prepareRequest ($ httpHeaders , $ dataArray );
88
85
89
- $ response = CurlRequest::post ($ url , self ::$ postDataJSON , self ::$ httpHeaders );
90
-
91
- return self ::processResponse ($ response );
86
+ return self ::processRequest ('POST ' , $ url );
92
87
}
93
88
94
89
/**
@@ -104,9 +99,7 @@ public static function put($url, $dataArray, $httpHeaders = array())
104
99
{
105
100
self ::prepareRequest ($ httpHeaders , $ dataArray );
106
101
107
- $ response = CurlRequest::put ($ url , self ::$ postDataJSON , self ::$ httpHeaders );
108
-
109
- return self ::processResponse ($ response );
102
+ return self ::processRequest ('PUT ' , $ url );
110
103
}
111
104
112
105
/**
@@ -121,9 +114,68 @@ public static function delete($url, $httpHeaders = array())
121
114
{
122
115
self ::prepareRequest ($ httpHeaders );
123
116
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
+ }
125
177
126
- return self :: processResponse ( $ response ) ;
178
+ return false ;
127
179
}
128
180
129
181
/**
@@ -135,8 +187,29 @@ public static function delete($url, $httpHeaders = array())
135
187
*/
136
188
protected static function processResponse ($ response )
137
189
{
190
+ $ responseArray = json_decode ($ response , true );
138
191
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
+ }
141
207
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