|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* Anime List Client |
|
4
|
|
|
* |
|
5
|
|
|
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists |
|
6
|
|
|
* |
|
7
|
|
|
* PHP version 7 |
|
8
|
|
|
* |
|
9
|
|
|
* @package AnimeListClient |
|
10
|
|
|
* @author Timothy J. Warren <[email protected]> |
|
11
|
|
|
* @copyright 2015 - 2017 Timothy J. Warren |
|
12
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
13
|
|
|
* @version 4.0 |
|
14
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace Aviat\AnimeClient\API\Kitsu; |
|
18
|
|
|
|
|
19
|
|
|
use Aviat\AnimeClient\AnimeClient; |
|
20
|
|
|
use Aviat\AnimeClient\API\GuzzleTrait; |
|
21
|
|
|
use Aviat\AnimeClient\API\Kitsu as K; |
|
22
|
|
|
use Aviat\Ion\Json; |
|
23
|
|
|
use GuzzleHttp\Client; |
|
24
|
|
|
use GuzzleHttp\Cookie\CookieJar; |
|
25
|
|
|
use GuzzleHttp\Psr7\Response; |
|
26
|
|
|
use InvalidArgumentException; |
|
27
|
|
|
use RuntimeException; |
|
28
|
|
|
|
|
29
|
|
|
trait KitsuTrait { |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The request builder for the MAL API |
|
33
|
|
|
* @var MALRequestBuilder |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $requestBuilder; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The Guzzle http client object |
|
39
|
|
|
* @var object |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $client; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Cookie jar object for api requests |
|
45
|
|
|
* @var object |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $cookieJar; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The base url for api requests |
|
51
|
|
|
* @var string $base_url |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $baseUrl = "https://kitsu.io/api/edge/"; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* HTTP headers to send with every request |
|
57
|
|
|
* |
|
58
|
|
|
* @var array |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $defaultHeaders = [ |
|
61
|
|
|
'User-Agent' => "Tim's Anime Client/4.0", |
|
62
|
|
|
'Accept-Encoding' => 'application/vnd.api+json', |
|
63
|
|
|
'Content-Type' => 'application/vnd.api+json', |
|
64
|
|
|
'client_id' => 'dd031b32d2f56c990b1425efe6c42ad847e7fe3ab46bf1299f05ecd856bdb7dd', |
|
65
|
|
|
'client_secret' => '54d7307928f63414defd96399fc31ba847961ceaecef3a5fd93144e960c0e151', |
|
66
|
|
|
]; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Set the request builder object |
|
70
|
|
|
* |
|
71
|
|
|
* @param KitsuRequestBuilder $requestBuilder |
|
72
|
|
|
* @return self |
|
73
|
|
|
*/ |
|
74
|
|
|
public function setRequestBuilder($requestBuilder): self |
|
75
|
|
|
{ |
|
76
|
|
|
$this->requestBuilder = $requestBuilder; |
|
|
|
|
|
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Set up the class properties |
|
82
|
|
|
* |
|
83
|
|
|
* @return void |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function init() |
|
86
|
|
|
{ |
|
87
|
|
|
$defaults = [ |
|
88
|
|
|
'cookies' => $this->cookieJar, |
|
89
|
|
|
'headers' => $this->defaultHeaders, |
|
90
|
|
|
'timeout' => 25, |
|
91
|
|
|
'connect_timeout' => 25 |
|
92
|
|
|
]; |
|
93
|
|
|
|
|
94
|
|
|
$this->cookieJar = new CookieJar(); |
|
95
|
|
|
$this->client = new Client([ |
|
96
|
|
|
'base_uri' => $this->baseUrl, |
|
97
|
|
|
'cookies' => TRUE, |
|
98
|
|
|
'http_errors' => TRUE, |
|
99
|
|
|
'defaults' => $defaults |
|
100
|
|
|
]); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Make a request via Guzzle |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $type |
|
107
|
|
|
* @param string $url |
|
108
|
|
|
* @param array $options |
|
109
|
|
|
* @return Response |
|
110
|
|
|
*/ |
|
111
|
|
|
private function getResponse(string $type, string $url, array $options = []) |
|
112
|
|
|
{ |
|
113
|
|
|
$logger = null; |
|
|
|
|
|
|
114
|
|
|
$validTypes = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']; |
|
115
|
|
|
|
|
116
|
|
|
if ( ! in_array($type, $validTypes)) |
|
117
|
|
|
{ |
|
118
|
|
|
throw new InvalidArgumentException('Invalid http request type'); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$defaultOptions = [ |
|
122
|
|
|
'headers' => $this->defaultHeaders |
|
123
|
|
|
]; |
|
124
|
|
|
|
|
125
|
|
|
$logger = $this->container->getLogger('kitsu-request'); |
|
|
|
|
|
|
126
|
|
|
$sessionSegment = $this->getContainer() |
|
|
|
|
|
|
127
|
|
|
->get('session') |
|
128
|
|
|
->getSegment(AnimeClient::SESSION_SEGMENT); |
|
129
|
|
|
|
|
130
|
|
|
if ($sessionSegment->get('auth_token') !== null && $url !== K::AUTH_URL) |
|
131
|
|
|
{ |
|
132
|
|
|
$token = $sessionSegment->get('auth_token'); |
|
133
|
|
|
$defaultOptions['headers']['Authorization'] = "bearer {$token}"; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$options = array_merge($defaultOptions, $options); |
|
137
|
|
|
|
|
138
|
|
|
$response = $this->client->request($type, $url, $options); |
|
139
|
|
|
|
|
140
|
|
|
$logger->debug('Kitsu API request', [ |
|
141
|
|
|
'requestParams' => [ |
|
142
|
|
|
'type' => $type, |
|
143
|
|
|
'url' => $url, |
|
144
|
|
|
], |
|
145
|
|
|
'responseValues' => [ |
|
146
|
|
|
'status' => $response->getStatusCode() |
|
147
|
|
|
] |
|
148
|
|
|
]); |
|
149
|
|
|
|
|
150
|
|
|
return $response; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Make a request via Guzzle |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $type |
|
157
|
|
|
* @param string $url |
|
158
|
|
|
* @param array $options |
|
159
|
|
|
* @return array |
|
160
|
|
|
*/ |
|
161
|
|
View Code Duplication |
private function request(string $type, string $url, array $options = []): array |
|
|
|
|
|
|
162
|
|
|
{ |
|
163
|
|
|
$logger = null; |
|
164
|
|
|
if ($this->getContainer()) |
|
|
|
|
|
|
165
|
|
|
{ |
|
166
|
|
|
$logger = $this->container->getLogger('kitsu-request'); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
$response = $this->getResponse($type, $url, $options); |
|
170
|
|
|
|
|
171
|
|
|
if ((int) $response->getStatusCode() > 299 || (int) $response->getStatusCode() < 200) |
|
172
|
|
|
{ |
|
173
|
|
|
if ($logger) |
|
174
|
|
|
{ |
|
175
|
|
|
$logger->warning('Non 200 response for api call', $response->getBody()); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return JSON::decode($response->getBody(), TRUE); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Remove some boilerplate for get requests |
|
184
|
|
|
* |
|
185
|
|
|
* @param array $args |
|
186
|
|
|
* @return array |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function getRequest(...$args): array |
|
189
|
|
|
{ |
|
190
|
|
|
return $this->request('GET', ...$args); |
|
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Remove some boilerplate for patch requests |
|
195
|
|
|
* |
|
196
|
|
|
* @param array $args |
|
197
|
|
|
* @return array |
|
198
|
|
|
*/ |
|
199
|
|
|
protected function patchRequest(...$args): array |
|
200
|
|
|
{ |
|
201
|
|
|
return $this->request('PATCH', ...$args); |
|
|
|
|
|
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Remove some boilerplate for post requests |
|
206
|
|
|
* |
|
207
|
|
|
* @param array $args |
|
208
|
|
|
* @return array |
|
209
|
|
|
*/ |
|
210
|
|
View Code Duplication |
protected function postRequest(...$args): array |
|
|
|
|
|
|
211
|
|
|
{ |
|
212
|
|
|
$logger = null; |
|
213
|
|
|
if ($this->getContainer()) |
|
|
|
|
|
|
214
|
|
|
{ |
|
215
|
|
|
$logger = $this->container->getLogger('kitsu-request'); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
$response = $this->getResponse('POST', ...$args); |
|
|
|
|
|
|
219
|
|
|
$validResponseCodes = [200, 201]; |
|
220
|
|
|
|
|
221
|
|
|
if ( ! in_array((int) $response->getStatusCode(), $validResponseCodes)) |
|
222
|
|
|
{ |
|
223
|
|
|
if ($logger) |
|
224
|
|
|
{ |
|
225
|
|
|
$logger->warning('Non 201 response for POST api call', $response->getBody()); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
return JSON::decode($response->getBody(), TRUE); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Remove some boilerplate for delete requests |
|
234
|
|
|
* |
|
235
|
|
|
* @param array $args |
|
236
|
|
|
* @return bool |
|
237
|
|
|
*/ |
|
238
|
|
|
protected function deleteRequest(...$args): bool |
|
239
|
|
|
{ |
|
240
|
|
|
$response = $this->getResponse('DELETE', ...$args); |
|
|
|
|
|
|
241
|
|
|
return ((int) $response->getStatusCode() === 204); |
|
242
|
|
|
} |
|
243
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..