|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Pinterest PHP library. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Hans Ott <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE.md. |
|
10
|
|
|
* |
|
11
|
|
|
* Source: https://github.com/hansott/pinterest-php |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Pinterest\Http; |
|
15
|
|
|
|
|
16
|
|
|
use Pinterest\Http\Exceptions\MalformedJson; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The response class. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Hans Ott <[email protected]> |
|
22
|
|
|
* @author Toon Daelman <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
final class Response |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* The parsed response body. |
|
28
|
|
|
* |
|
29
|
|
|
* @var mixed |
|
30
|
|
|
*/ |
|
31
|
|
|
public $body; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The response status code. |
|
35
|
|
|
* |
|
36
|
|
|
* @var int |
|
37
|
|
|
*/ |
|
38
|
|
|
private $statusCode; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The raw response body. |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
private $rawBody; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* The HTTP headers. |
|
49
|
|
|
* |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
private $headers; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* The processed result. |
|
56
|
|
|
* |
|
57
|
|
|
* @var mixed |
|
58
|
|
|
*/ |
|
59
|
|
|
private $result; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* The request object. |
|
63
|
|
|
* |
|
64
|
|
|
* @var Request |
|
65
|
|
|
*/ |
|
66
|
|
|
private $request; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* The constructor. |
|
70
|
|
|
* |
|
71
|
|
|
* @param Request $request The request object. |
|
72
|
|
|
* @param int $statusCode The status code. |
|
73
|
|
|
* @param string $rawBody The raw response body. |
|
74
|
|
|
* @param array $headers A key => value representation of response headers. |
|
75
|
|
|
* |
|
76
|
|
|
* @throws MalformedJson |
|
77
|
|
|
*/ |
|
78
|
34 |
|
public function __construct(Request $request, $statusCode, $rawBody, array $headers) |
|
79
|
|
|
{ |
|
80
|
34 |
|
$this->request = $request; |
|
81
|
34 |
|
$this->statusCode = (int) $statusCode; |
|
82
|
34 |
|
$this->rawBody = (string) $rawBody; |
|
83
|
34 |
|
$this->body = $this->parseJson($this->rawBody); |
|
84
|
34 |
|
$this->headers = $headers; |
|
85
|
34 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Checks if the response is okay. |
|
89
|
|
|
* |
|
90
|
|
|
* @return bool Whether the response is okay. |
|
91
|
|
|
*/ |
|
92
|
34 |
|
public function ok() |
|
93
|
|
|
{ |
|
94
|
|
|
return ( |
|
95
|
34 |
|
!isset($this->body->error) |
|
96
|
34 |
|
&& $this->statusCode >= 200 |
|
97
|
34 |
|
&& $this->statusCode < 300 |
|
98
|
17 |
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get the HTTP status code. |
|
103
|
|
|
* |
|
104
|
|
|
* @return int |
|
105
|
|
|
*/ |
|
106
|
5 |
|
public function getStatusCode() |
|
107
|
|
|
{ |
|
108
|
5 |
|
return $this->statusCode; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get the error message. |
|
113
|
|
|
* |
|
114
|
|
|
* @return null|string |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getError() |
|
117
|
|
|
{ |
|
118
|
|
|
return !empty($this->body->message) ? (string) $this->body->message : null; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Checks if the response is rate-limited. |
|
123
|
|
|
* |
|
124
|
|
|
* @return bool Whether the response is rate-limited. |
|
125
|
|
|
*/ |
|
126
|
34 |
|
public function rateLimited() |
|
127
|
|
|
{ |
|
128
|
34 |
|
return $this->statusCode === 429; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Parse the response json. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $rawBody The raw response body. |
|
135
|
|
|
* @param bool $toArray Return as array? |
|
136
|
|
|
* |
|
137
|
|
|
* @throws MalformedJson |
|
138
|
|
|
* |
|
139
|
|
|
* @return mixed The parsed json. |
|
140
|
|
|
*/ |
|
141
|
34 |
|
private function parseJson($rawBody, $toArray = false) |
|
142
|
|
|
{ |
|
143
|
34 |
|
$json = json_decode($rawBody, $toArray); |
|
144
|
34 |
|
if (json_last_error() === JSON_ERROR_NONE) { |
|
145
|
34 |
|
return $json; |
|
146
|
|
|
} else { |
|
147
|
|
|
throw new MalformedJson(); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get the processed result. |
|
153
|
|
|
* |
|
154
|
|
|
* @return mixed The processed result. |
|
155
|
|
|
*/ |
|
156
|
15 |
|
public function result() |
|
157
|
|
|
{ |
|
158
|
15 |
|
return $this->result; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Set the processed result. |
|
163
|
|
|
* |
|
164
|
|
|
* @param mixed $result The result. |
|
165
|
|
|
* |
|
166
|
|
|
* @return $this |
|
167
|
|
|
*/ |
|
168
|
15 |
|
public function setResult($result) |
|
169
|
|
|
{ |
|
170
|
15 |
|
$this->result = $result; |
|
171
|
|
|
|
|
172
|
15 |
|
return $this; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Get the request object. |
|
177
|
|
|
* |
|
178
|
|
|
* @return Request |
|
179
|
|
|
*/ |
|
180
|
|
|
public function getRequest() |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->request; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Get the HTTP Headers. |
|
187
|
|
|
* |
|
188
|
|
|
* @return array |
|
189
|
|
|
*/ |
|
190
|
5 |
|
public function getHeaders() |
|
191
|
|
|
{ |
|
192
|
5 |
|
return $this->headers; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Get a specific HTTP header. |
|
197
|
|
|
* |
|
198
|
|
|
* @param string $header |
|
199
|
|
|
* |
|
200
|
|
|
* @return string|null The header value. |
|
201
|
|
|
*/ |
|
202
|
1 |
|
public function getHeader($header) |
|
203
|
|
|
{ |
|
204
|
1 |
|
$header = strtolower($header); |
|
205
|
1 |
|
foreach ($this->headers as $name => $value) { |
|
206
|
1 |
|
if (strtolower($name) === $header) { |
|
207
|
1 |
|
return $value; |
|
208
|
|
|
} |
|
209
|
1 |
|
} |
|
210
|
|
|
|
|
211
|
|
|
return null; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Get the rate-limit. |
|
216
|
|
|
* |
|
217
|
|
|
* @return int|null The rate-limit. |
|
218
|
|
|
*/ |
|
219
|
1 |
|
public function getRateLimit() |
|
220
|
|
|
{ |
|
221
|
1 |
|
$limit = $this->getHeader('X-RateLimit-Limit'); |
|
222
|
|
|
|
|
223
|
1 |
|
return is_numeric($limit) ? (int) $limit : null; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Get the remaining requests before reaching the rate-limit. |
|
228
|
|
|
* |
|
229
|
|
|
* @return int|null The remaining requests. |
|
230
|
|
|
*/ |
|
231
|
1 |
|
public function getRemainingRequests() |
|
232
|
|
|
{ |
|
233
|
1 |
|
$remaining = $this->getHeader('X-RateLimit-Remaining'); |
|
234
|
|
|
|
|
235
|
1 |
|
return is_numeric($remaining) ? (int) $remaining : null; |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|