-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathHttpRequestJson.php
215 lines (186 loc) · 5.77 KB
/
HttpRequestJson.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* Created by PhpStorm.
* @author Tareq Mahmood <[email protected]>
* Created at: 8/24/16 9:03 AM UTC+06:00
*/
namespace PHPShopify;
/**
* Class HttpRequestJson
*
* Prepare the data / headers for JSON requests, make the call and decode the response
* Accepts data in array format returns data in array format
*
* @uses CurlRequest
*
* @package PHPShopify
*/
class HttpRequestJson
{
/**
* HTTP request headers
*
* @var array
*/
protected static $httpHeaders;
/**
* Prepared JSON string to be posted with request
*
* @var string
*/
protected static $postDataJSON;
/**
* Prepare the data and request headers before making the call
*
* @param array $httpHeaders
* @param array $dataArray
*
* @return void
*/
protected static function prepareRequest($httpHeaders = array(), $dataArray = array())
{
self::$postDataJSON = json_encode($dataArray);
self::$httpHeaders = $httpHeaders;
if (!empty($dataArray)) {
self::$httpHeaders['Content-type'] = 'application/json';
self::$httpHeaders['Content-Length'] = strlen(self::$postDataJSON);
}
}
/**
* Implement a GET request and return json decoded output
*
* @param string $url
* @param array $httpHeaders
*
* @return array
*/
public static function get($url, $httpHeaders = array())
{
self::prepareRequest($httpHeaders);
return self::processRequest('GET', $url);
}
/**
* Implement a POST request and return json decoded output
*
* @param string $url
* @param array $dataArray
* @param array $httpHeaders
*
* @return array
*/
public static function post($url, $dataArray, $httpHeaders = array())
{
self::prepareRequest($httpHeaders, $dataArray);
return self::processRequest('POST', $url);
}
/**
* Implement a PUT request and return json decoded output
*
* @param string $url
* @param array $dataArray
* @param array $httpHeaders
*
* @return array
*/
public static function put($url, $dataArray, $httpHeaders = array())
{
self::prepareRequest($httpHeaders, $dataArray);
return self::processRequest('PUT', $url);
}
/**
* Implement a DELETE request and return json decoded output
*
* @param string $url
* @param array $httpHeaders
*
* @return array
*/
public static function delete($url, $httpHeaders = array())
{
self::prepareRequest($httpHeaders);
return self::processRequest('DELETE', $url);
}
/**
* Process a curl request and return decoded JSON response
*
* @param string $method Request http method ('GET', 'POST', 'PUT' or 'DELETE')
* @param string $url Request URL
*
* @throws CurlException if response received with unexpected HTTP code.
*
* @return array
*/
public static function processRequest($method, $url) {
$retry = 0;
$raw = null;
while(true) {
try {
switch($method) {
case 'GET':
$raw = CurlRequest::get($url, self::$httpHeaders);
break;
case 'POST':
$raw = CurlRequest::post($url, self::$postDataJSON, self::$httpHeaders);
break;
case 'PUT':
$raw = CurlRequest::put($url, self::$postDataJSON, self::$httpHeaders);
break;
case 'DELETE':
$raw = CurlRequest::delete($url, self::$httpHeaders);
break;
default:
throw new \Exception("unexpected request method '$method'");
}
return self::processResponse($raw);
} catch(\Exception $e) {
if (!self::shouldRetry($raw, $e, $retry++)) {
throw $e;
}
}
}
}
/**
* Evaluate if send again a request
*
* @param string $response Raw request response
* @param exception $error the request error occured
* @param integer $retry the current number of retry
*
* @return bool
*/
public static function shouldRetry($response, $error, $retry) {
$config = [];
if (isset($config['RequestRetryCallback'])) {
return $config['RequestRetryCallback']($response, $error, $retry);
}
return false;
}
/**
* Decode JSON response
*
* @param string $response
*
* @return array
*/
protected static function processResponse($response)
{
$responseArray = json_decode($response, true);
if ($responseArray === null) {
//Something went wrong, Checking HTTP Codes
$httpOK = 200; //Request Successful, OK.
$httpCreated = 201; //Create Successful.
$httpDeleted = 204; //Delete Successful
$httpOther = 303; //See other (headers).
$lastHttpResponseHeaders = CurlRequest::$lastHttpResponseHeaders;
//should be null if any other library used for http calls
$httpCode = CurlRequest::$lastHttpCode;
if ($httpCode == $httpOther && array_key_exists('location', $lastHttpResponseHeaders)) {
return ['location' => $lastHttpResponseHeaders['location']];
}
if ($httpCode != null && $httpCode != $httpOK && $httpCode != $httpCreated && $httpCode != $httpDeleted) {
throw new Exception\CurlException("Request failed with HTTP Code $httpCode.", $httpCode);
}
}
return $responseArray;
}
}