|
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 const Aviat\AnimeClient\SESSION_SEGMENT; |
|
20
|
|
|
|
|
21
|
|
|
use function Amp\wait; |
|
22
|
|
|
|
|
23
|
|
|
use Amp\Artax\Client; |
|
24
|
|
|
use Aviat\AnimeClient\AnimeClient; |
|
25
|
|
|
use Aviat\AnimeClient\API\Kitsu as K; |
|
26
|
|
|
use Aviat\Ion\Json; |
|
27
|
|
|
use InvalidArgumentException; |
|
28
|
|
|
use RuntimeException; |
|
29
|
|
|
|
|
30
|
|
|
trait KitsuTrait { |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The request builder for the MAL API |
|
34
|
|
|
* @var MALRequestBuilder |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $requestBuilder; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Set the request builder object |
|
40
|
|
|
* |
|
41
|
|
|
* @param KitsuRequestBuilder $requestBuilder |
|
42
|
|
|
* @return self |
|
43
|
|
|
*/ |
|
44
|
|
|
public function setRequestBuilder($requestBuilder): self |
|
45
|
|
|
{ |
|
46
|
|
|
$this->requestBuilder = $requestBuilder; |
|
|
|
|
|
|
47
|
|
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Create a request object |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $type |
|
54
|
|
|
* @param string $url |
|
55
|
|
|
* @param array $options |
|
56
|
|
|
* @return \Amp\Artax\Response |
|
57
|
|
|
*/ |
|
58
|
|
|
public function setUpRequest(string $type, string $url, array $options = []) |
|
59
|
|
|
{ |
|
60
|
|
|
$config = $this->container->get('config'); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
$request = $this->requestBuilder->newRequest($type, $url); |
|
63
|
|
|
|
|
64
|
|
|
$sessionSegment = $this->getContainer() |
|
|
|
|
|
|
65
|
|
|
->get('session') |
|
66
|
|
|
->getSegment(SESSION_SEGMENT); |
|
67
|
|
|
|
|
68
|
|
|
if ($sessionSegment->get('auth_token') !== null && $url !== K::AUTH_URL) |
|
69
|
|
|
{ |
|
70
|
|
|
$token = $sessionSegment->get('auth_token'); |
|
71
|
|
|
$request = $request->setAuth('bearer', $token); |
|
72
|
|
|
// $defaultOptions['headers']['Authorization'] = "bearer {$token}"; |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if (array_key_exists('form_params', $options)) |
|
76
|
|
|
{ |
|
77
|
|
|
$request->setFormFields($options['form_params']); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (array_key_exists('query', $options)) |
|
81
|
|
|
{ |
|
82
|
|
|
$request->setQuery($options['query']); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if (array_key_exists('body', $options)) |
|
86
|
|
|
{ |
|
87
|
|
|
$request->setJsonBody($options['body']); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $request->getFullRequest(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Make a request |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $type |
|
97
|
|
|
* @param string $url |
|
98
|
|
|
* @param array $options |
|
99
|
|
|
* @return Response |
|
100
|
|
|
*/ |
|
101
|
|
|
private function getResponse(string $type, string $url, array $options = []) |
|
102
|
|
|
{ |
|
103
|
|
|
$request = $this->setUpRequest($type, $url, $options); |
|
104
|
|
|
$logger = $this->container->getLogger('kitsu-request'); |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
$response = wait((new Client)->request($request)); |
|
107
|
|
|
|
|
108
|
|
|
/* $logger->debug('Kitsu api response', [ |
|
|
|
|
|
|
109
|
|
|
'status' => $response->getStatus(), |
|
110
|
|
|
'reason' => $response->getReason(), |
|
111
|
|
|
'body' => $response->getBody(), |
|
112
|
|
|
'headers' => $response->getAllHeaders(), |
|
113
|
|
|
'requestHeaders' => $request->getAllHeaders(), |
|
114
|
|
|
]); */ |
|
115
|
|
|
|
|
116
|
|
|
return $response; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Make a request |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $type |
|
123
|
|
|
* @param string $url |
|
124
|
|
|
* @param array $options |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
|
View Code Duplication |
private function request(string $type, string $url, array $options = []): array |
|
|
|
|
|
|
128
|
|
|
{ |
|
129
|
|
|
$logger = null; |
|
130
|
|
|
if ($this->getContainer()) |
|
|
|
|
|
|
131
|
|
|
{ |
|
132
|
|
|
$logger = $this->container->getLogger('kitsu-request'); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$response = $this->getResponse($type, $url, $options); |
|
136
|
|
|
|
|
137
|
|
|
if ((int) $response->getStatus() > 299 || (int) $response->getStatus() < 200) |
|
138
|
|
|
{ |
|
139
|
|
|
if ($logger) |
|
140
|
|
|
{ |
|
141
|
|
|
$logger->warning('Non 200 response for api call', $response->getBody()); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return JSON::decode($response->getBody(), TRUE); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Remove some boilerplate for get requests |
|
150
|
|
|
* |
|
151
|
|
|
* @param array $args |
|
152
|
|
|
* @return array |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function getRequest(...$args): array |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->request('GET', ...$args); |
|
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Remove some boilerplate for patch requests |
|
161
|
|
|
* |
|
162
|
|
|
* @param array $args |
|
163
|
|
|
* @return array |
|
164
|
|
|
*/ |
|
165
|
|
|
protected function patchRequest(...$args): array |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->request('PATCH', ...$args); |
|
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Remove some boilerplate for post requests |
|
172
|
|
|
* |
|
173
|
|
|
* @param array $args |
|
174
|
|
|
* @return array |
|
175
|
|
|
*/ |
|
176
|
|
View Code Duplication |
protected function postRequest(...$args): array |
|
|
|
|
|
|
177
|
|
|
{ |
|
178
|
|
|
$logger = null; |
|
179
|
|
|
if ($this->getContainer()) |
|
|
|
|
|
|
180
|
|
|
{ |
|
181
|
|
|
$logger = $this->container->getLogger('kitsu-request'); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
$response = $this->getResponse('POST', ...$args); |
|
|
|
|
|
|
185
|
|
|
$validResponseCodes = [200, 201]; |
|
186
|
|
|
|
|
187
|
|
|
if ( ! in_array((int) $response->getStatus(), $validResponseCodes)) |
|
188
|
|
|
{ |
|
189
|
|
|
if ($logger) |
|
190
|
|
|
{ |
|
191
|
|
|
$logger->warning('Non 201 response for POST api call', $response->getBody()); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
return JSON::decode($response->getBody(), TRUE); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Remove some boilerplate for delete requests |
|
200
|
|
|
* |
|
201
|
|
|
* @param array $args |
|
202
|
|
|
* @return bool |
|
203
|
|
|
*/ |
|
204
|
|
|
protected function deleteRequest(...$args): bool |
|
205
|
|
|
{ |
|
206
|
|
|
$response = $this->getResponse('DELETE', ...$args); |
|
|
|
|
|
|
207
|
|
|
return ((int) $response->getStatus() === 204); |
|
208
|
|
|
} |
|
209
|
|
|
} |
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..