1 | <?php namespace Stevenmaguire\Services\Trello; |
||
10 | class Http |
||
11 | { |
||
12 | const HTTP_DELETE = 'DELETE'; |
||
13 | const HTTP_GET = 'GET'; |
||
14 | const HTTP_POST = 'POST'; |
||
15 | const HTTP_PUT = 'PUT'; |
||
16 | |||
17 | /** |
||
18 | * Multipart resources to include in next request. |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $multipartResources = []; |
||
23 | |||
24 | /** |
||
25 | * Http client |
||
26 | * |
||
27 | * @var HttpClientInterface |
||
28 | */ |
||
29 | protected $httpClient; |
||
30 | |||
31 | /** |
||
32 | * Creates a new http broker. |
||
33 | */ |
||
34 | 702 | public function __construct() |
|
38 | |||
39 | /** |
||
40 | * Adds authentication credentials to given request. |
||
41 | * |
||
42 | * @param RequestInterface $request |
||
43 | * |
||
44 | * @return RequestInterface |
||
45 | */ |
||
46 | 682 | protected function authenticateRequest(RequestInterface $request) |
|
58 | |||
59 | /** |
||
60 | * Creates a request. |
||
61 | * |
||
62 | * @param string $verb |
||
63 | * @param string $path |
||
64 | * @param array $parameters |
||
65 | * |
||
66 | * @return Request |
||
67 | */ |
||
68 | 684 | protected function createRequest($verb, $path, $parameters = []) |
|
90 | |||
91 | /** |
||
92 | * Retrieves http response for a request with the delete method. |
||
93 | * |
||
94 | * @param string $path |
||
95 | * @param array $parameters |
||
96 | * |
||
97 | * @return object |
||
98 | */ |
||
99 | 62 | public function delete($path, $parameters = []) |
|
105 | |||
106 | /** |
||
107 | * Retrieves http response for a request with the get method. |
||
108 | * |
||
109 | * @param string $path |
||
110 | * @param array $parameters |
||
111 | * |
||
112 | * @return object |
||
113 | */ |
||
114 | 312 | public function get($path, $parameters = []) |
|
120 | |||
121 | /** |
||
122 | * Creates and returns a request. |
||
123 | * |
||
124 | * @param string $method |
||
125 | * @param string $path |
||
126 | * @param array $parameters |
||
127 | * |
||
128 | * @return RequestInterface |
||
129 | */ |
||
130 | 684 | public function getRequest($method, $path, $parameters = [], $authenticated = true) |
|
140 | |||
141 | /** |
||
142 | * Retrieves default headers. |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | 684 | protected function getHeaders() |
|
150 | |||
151 | /** |
||
152 | * Prepares an array of important exception parts based on composition of a |
||
153 | * given exception. |
||
154 | * |
||
155 | * @param RequestException $requestException |
||
156 | * |
||
157 | * @return array |
||
158 | */ |
||
159 | 8 | private function getRequestExceptionParts(RequestException $requestException) |
|
169 | |||
170 | /** |
||
171 | * Creates an array of request options based on the current status of the |
||
172 | * http client. |
||
173 | * |
||
174 | * @return array |
||
175 | */ |
||
176 | 680 | protected function getRequestOptions() |
|
188 | |||
189 | /** |
||
190 | * Creates fully qualified domain from given path. |
||
191 | * |
||
192 | * @param string $path |
||
193 | * |
||
194 | * @return string |
||
195 | */ |
||
196 | 684 | protected function getUrlFromPath($path = '/') |
|
200 | |||
201 | /** |
||
202 | * Retrieves http response for a request with the post method. |
||
203 | * |
||
204 | * @param string $path |
||
205 | * @param array $parameters |
||
206 | * |
||
207 | * @return object |
||
208 | */ |
||
209 | 86 | public function post($path, $parameters) |
|
215 | |||
216 | /** |
||
217 | * Retrieves http response for a request with the put method. |
||
218 | * |
||
219 | * @param string $path |
||
220 | * @param array $parameters |
||
221 | * |
||
222 | * @return object |
||
223 | */ |
||
224 | 218 | public function put($path, $parameters) |
|
230 | |||
231 | /** |
||
232 | * Retrieves http response for a request with the put method, |
||
233 | * ensuring parameters are passed as body. |
||
234 | * |
||
235 | * @param string $path |
||
236 | * @param array $parameters |
||
237 | * |
||
238 | * @return object |
||
239 | */ |
||
240 | 2 | public function putAsBody($path, $parameters) |
|
247 | |||
248 | /** |
||
249 | * Adds a given resource to multipart stream collection, to be processed by next request. |
||
250 | * |
||
251 | * @param string $name |
||
252 | * @param resource|string|Psr\Http\Message\StreamInterface $resource |
||
253 | * |
||
254 | * @return void |
||
255 | */ |
||
256 | 2 | protected function queueResourceAs($name, $resource) |
|
263 | |||
264 | /** |
||
265 | * Retrieves http response for a given request. |
||
266 | * |
||
267 | * @param RequestInterface $request |
||
268 | * |
||
269 | * @return object |
||
270 | * @throws Exceptions\Exception |
||
271 | */ |
||
272 | 680 | protected function sendRequest(RequestInterface $request) |
|
287 | |||
288 | /** |
||
289 | * Updates the http client. |
||
290 | * |
||
291 | * @param HttpClientInterface $httpClient |
||
292 | * |
||
293 | * @return Http |
||
294 | */ |
||
295 | 680 | public function setClient(HttpClientInterface $httpClient) |
|
301 | |||
302 | /** |
||
303 | * Creates local exception from guzzle request exception, which includes |
||
304 | * response body. |
||
305 | * |
||
306 | * @param RequestException $requestException |
||
307 | * |
||
308 | * @return void |
||
309 | * @throws Exceptions\Exception |
||
310 | */ |
||
311 | 8 | protected function throwRequestException(RequestException $requestException) |
|
330 | } |
||
331 |