-
-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathAbstractApi.php
More file actions
220 lines (202 loc) · 5.44 KB
/
AbstractApi.php
File metadata and controls
220 lines (202 loc) · 5.44 KB
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
216
217
218
219
220
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <michael@wtfz.net>
* @copyright (c) 2013, Michael Roterman
* @version 4.0.0
*/
namespace Tmdb\Api;
use JsonException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Tmdb\Client;
use Tmdb\Exception\InvalidArgumentException;
use Tmdb\Exception\UnexpectedResponseException;
use Tmdb\HttpClient\HttpClient;
/**
* Class AbstractApi
* @package Tmdb\Api
*/
abstract class AbstractApi implements ApiInterface
{
/**
* The client
*
* @var Client
*/
protected $client;
/**
* Constructor
*
* @param Client $client
*/
public function __construct(Client $client)
{
$this->client = $client;
}
/**
* Send a GET request
*
* @param string $path
* @param array $parameters
* @param array $headers
* @return array
*/
public function get(string $path, array $parameters = [], array $headers = []): array
{
return $this->decodeResponse(
$this->getHttpClient()->send($path, 'GET', $parameters, $headers)
);
}
/**
* Send a POST request
*
* @param string $path
* @param null $postBody
* @param array $parameters
* @param array $headers
* @return array
*/
public function post(string $path, $postBody = null, array $parameters = [], array $headers = []): array
{
return $this->decodeResponse(
$this->getHttpClient()->send($path, 'POST', $parameters, $headers, $postBody)
);
}
/**
* Send a POST request but json_encode the post body in the request
*
* @param string $path
* @param mixed $postBody
* @param array $parameters
* @param array $headers
* @return array
* @throws InvalidArgumentException
*/
public function postJson(string $path, $postBody = null, array $parameters = [], array $headers = []): array
{
try {
if (is_array($postBody)) {
$postBody = json_encode($postBody, JSON_THROW_ON_ERROR);
}
return $this->post($path, $postBody, $parameters, $headers);
} catch (JsonException $e) {
throw new InvalidArgumentException(
'Unable to json_encode the data provided.',
0,
$e
);
}
}
/**
* Send a HEAD request
*
* @param string $path
* @param array $parameters
* @param array $headers
* @return array
*/
public function head(string $path, array $parameters = [], array $headers = []): array
{
return $this->decodeResponse(
$this->getHttpClient()->send($path, 'HEAD', $parameters, $headers)
);
}
/**
* Send a PUT request
*
* @param string $path
* @param null $body
* @param array $parameters
* @param array $headers
* @return array
*/
public function put(string $path, $body = null, array $parameters = [], array $headers = []): array
{
return $this->decodeResponse(
$this->getHttpClient()->send($path, 'PUT', $parameters, $headers, $body)
);
}
/**
* Send a DELETE request
*
* @param string $path
* @param null $body
* @param array $parameters
* @param array $headers
* @return array
*/
public function delete(string $path, $body = null, array $parameters = [], array $headers = []): array
{
return $this->decodeResponse(
$this->getHttpClient()->send($path, 'DELETE', $parameters, $headers, $body)
);
}
/**
* Send a PATCH request
*
* @param string $path
* @param string|null $body
* @param array $parameters
* @param array $headers
* @return array
*/
public function patch(string $path, $body = null, array $parameters = [], array $headers = []): array
{
return $this->decodeResponse(
$this->getHttpClient()->send($path, 'PATCH', $parameters, $headers, $body)
);
}
/**
* Retrieve the client
*
* @return Client
*/
public function getClient()
{
return $this->client;
}
/**
* Retrieve the http client
*
* @return HttpClient
*/
public function getHttpClient()
{
return $this->client->getHttpClient();
}
/**
* Decode the response
*
* @param ResponseInterface $response
* @return array
*/
private function decodeResponse(ResponseInterface $response)
{
try {
if ($response->getBody() instanceof StreamInterface) {
$body = (string)$response->getBody();
if (trim($body) === '') {
return [];
}
return json_decode($body, true, 512, JSON_THROW_ON_ERROR);
}
return [];
} catch (JsonException $e) {
throw new UnexpectedResponseException(
sprintf(
'Unable to decode response with body "%s", %s.',
(string)$response->getBody(),
json_last_error_msg()
),
$response->getStatusCode(),
$e
);
}
}
}