1 | <?php namespace Stevenmaguire\Services\Trello; |
||
9 | class Http |
||
10 | { |
||
11 | /** |
||
12 | * Multipart resources to include in next request. |
||
13 | * |
||
14 | * @var array |
||
15 | */ |
||
16 | protected $multipartResources = []; |
||
17 | |||
18 | /** |
||
19 | * Http client |
||
20 | * |
||
21 | * @var HttpClientInterface |
||
22 | */ |
||
23 | protected $httpClient; |
||
24 | |||
25 | /** |
||
26 | * Creates a new http broker. |
||
27 | */ |
||
28 | 688 | public function __construct() |
|
32 | |||
33 | /** |
||
34 | * Adds authentication credentials to given request. |
||
35 | * |
||
36 | * @param RequestInterface $request |
||
37 | * |
||
38 | * @return RequestInterface |
||
39 | */ |
||
40 | 668 | protected function authenticateRequest(RequestInterface $request) |
|
52 | |||
53 | /** |
||
54 | * Creates a request. |
||
55 | * |
||
56 | * @param string $verb |
||
57 | * @param string $path |
||
58 | * @param array $parameters |
||
59 | * |
||
60 | * @return Request |
||
61 | */ |
||
62 | 670 | protected function createRequest($verb, $path, $parameters = []) |
|
63 | { |
||
64 | 670 | if (isset($parameters['file'])) { |
|
65 | 4 | $this->queueResourceAs( |
|
66 | 2 | 'file', |
|
67 | 2 | \GuzzleHttp\Psr7\stream_for($parameters['file']) |
|
68 | 2 | ); |
|
69 | 2 | unset($parameters['file']); |
|
70 | 2 | } |
|
71 | |||
72 | 670 | $request = new Request( |
|
73 | 670 | $verb, |
|
74 | 670 | $this->getUrlFromPath($path), |
|
75 | 670 | $this->getHeaders() |
|
76 | 670 | ); |
|
77 | |||
78 | 670 | return $request->withUri( |
|
79 | 670 | $request->getUri()->withQuery( |
|
80 | 670 | http_build_query($parameters) |
|
81 | 670 | ) |
|
82 | 670 | ); |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * Retrieves http response for a request with the delete method. |
||
87 | * |
||
88 | * @param string $path |
||
89 | * @param array $parameters |
||
90 | * |
||
91 | * @return object |
||
92 | */ |
||
93 | 60 | public function delete($path, $parameters = []) |
|
99 | |||
100 | /** |
||
101 | * Retrieves http response for a request with the get method. |
||
102 | * |
||
103 | * @param string $path |
||
104 | * @param array $parameters |
||
105 | * |
||
106 | * @return object |
||
107 | */ |
||
108 | 308 | public function get($path, $parameters = []) |
|
114 | |||
115 | /** |
||
116 | * Creates and returns a request. |
||
117 | * |
||
118 | * @param string $method |
||
119 | * @param string $path |
||
120 | * @param array $parameters |
||
121 | * |
||
122 | * @return RequestInterface |
||
123 | */ |
||
124 | 670 | public function getRequest($method, $path, $parameters = [], $authenticated = true) |
|
134 | |||
135 | /** |
||
136 | * Retrieves default headers. |
||
137 | * |
||
138 | * @return array |
||
139 | */ |
||
140 | 670 | protected function getHeaders() |
|
144 | |||
145 | /** |
||
146 | * Prepares an array of important exception parts based on composition of a |
||
147 | * given exception. |
||
148 | * |
||
149 | * @param RequestException $requestException |
||
150 | * |
||
151 | * @return array |
||
152 | */ |
||
153 | 8 | private function getRequestExceptionParts(RequestException $requestException) |
|
163 | |||
164 | /** |
||
165 | * Creates an array of request options based on the current status of the |
||
166 | * http client. |
||
167 | * |
||
168 | * @return array |
||
169 | */ |
||
170 | 666 | protected function getRequestOptions() |
|
182 | |||
183 | /** |
||
184 | * Creates fully qualified domain from given path. |
||
185 | * |
||
186 | * @param string $path |
||
187 | * |
||
188 | * @return string |
||
189 | */ |
||
190 | 670 | protected function getUrlFromPath($path = '/') |
|
194 | |||
195 | /** |
||
196 | * Retrieves http response for a request with the post method. |
||
197 | * |
||
198 | * @param string $path |
||
199 | * @param array $parameters |
||
200 | * |
||
201 | * @return object |
||
202 | */ |
||
203 | 82 | public function post($path, $parameters) |
|
209 | |||
210 | /** |
||
211 | * Retrieves http response for a request with the put method. |
||
212 | * |
||
213 | * @param string $path |
||
214 | * @param array $parameters |
||
215 | * |
||
216 | * @return object |
||
217 | */ |
||
218 | 216 | public function put($path, $parameters) |
|
224 | |||
225 | /** |
||
226 | * Adds a given resource to multipart stream collection, to be processed by next request. |
||
227 | * |
||
228 | * @param string $name |
||
229 | * @param resource|string|Psr\Http\Message\StreamInterface $resource |
||
230 | * |
||
231 | * @return void |
||
232 | */ |
||
233 | 2 | protected function queueResourceAs($name, $resource) |
|
240 | |||
241 | /** |
||
242 | * Retrieves http response for a given request. |
||
243 | * |
||
244 | * @param RequestInterface $request |
||
245 | * |
||
246 | * @return object |
||
247 | * @throws Exceptions\Exception |
||
248 | */ |
||
249 | 666 | protected function sendRequest(RequestInterface $request) |
|
264 | |||
265 | /** |
||
266 | * Updates the http client. |
||
267 | * |
||
268 | * @param HttpClientInterface $httpClient |
||
269 | * |
||
270 | * @return Http |
||
271 | */ |
||
272 | 666 | public function setClient(HttpClientInterface $httpClient) |
|
278 | |||
279 | /** |
||
280 | * Creates local exception from guzzle request exception, which includes |
||
281 | * response body. |
||
282 | * |
||
283 | * @param RequestException $requestException |
||
284 | * |
||
285 | * @return void |
||
286 | * @throws Exceptions\Exception |
||
287 | */ |
||
288 | 8 | protected function throwRequestException(RequestException $requestException) |
|
307 | } |
||
308 |