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\MAL; |
18
|
|
|
|
19
|
|
|
use Amp\Artax\{Client, FormBody, Request}; |
20
|
|
|
use Aviat\AnimeClient\API\{ |
21
|
|
|
MAL as M, |
22
|
|
|
APIRequestBuilder, |
23
|
|
|
XML |
24
|
|
|
}; |
25
|
|
|
use Aviat\AnimeClient\API\MALRequestBuilder; |
|
|
|
|
26
|
|
|
use Aviat\Ion\Json; |
27
|
|
|
use InvalidArgumentException; |
28
|
|
|
|
29
|
|
|
trait MALTrait { |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The request builder for the MAL API |
33
|
|
|
* @var MALRequestBuilder |
34
|
|
|
*/ |
35
|
|
|
protected $requestBuilder; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The base url for api requests |
39
|
|
|
* @var string $base_url |
40
|
|
|
*/ |
41
|
|
|
protected $baseUrl = M::BASE_URL; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* HTTP headers to send with every request |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $defaultHeaders = [ |
49
|
|
|
'Accept' => 'text/xml', |
50
|
|
|
'Accept-Encoding' => 'gzip', |
51
|
|
|
'Content-type' => 'application/x-www-form-urlencoded', |
52
|
|
|
'User-Agent' => "Tim's Anime Client/4.0" |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set the request builder object |
57
|
|
|
* |
58
|
|
|
* @param MALRequestBuilder $requestBuilder |
59
|
|
|
* @return self |
60
|
|
|
*/ |
61
|
|
|
public function setRequestBuilder($requestBuilder): self |
62
|
|
|
{ |
63
|
|
|
$this->requestBuilder = $requestBuilder; |
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Unencode the dual-encoded ampersands in the body |
69
|
|
|
* |
70
|
|
|
* This is a dirty hack until I can fully track down where |
71
|
|
|
* the dual-encoding happens |
72
|
|
|
* |
73
|
|
|
* @param FormBody $formBody The form builder object to fix |
|
|
|
|
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
private function fixBody(FormBody $formBody): string |
77
|
|
|
{ |
78
|
|
|
$rawBody = \Amp\wait($formBody->getBody()); |
79
|
|
|
return html_entity_decode($rawBody, \ENT_HTML5, 'UTF-8'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Create a request object |
84
|
|
|
* |
85
|
|
|
* @param string $type |
86
|
|
|
* @param string $url |
87
|
|
|
* @param array $options |
88
|
|
|
* @return \Amp\Promise |
|
|
|
|
89
|
|
|
*/ |
90
|
|
|
public function setUpRequest(string $type, string $url, array $options = []) |
91
|
|
|
{ |
92
|
|
|
$this->defaultHeaders['User-Agent'] = $_SERVER['HTTP_USER_AGENT'] ?? $this->defaultHeaders; |
93
|
|
|
|
94
|
|
|
$type = strtoupper($type); |
95
|
|
|
$validTypes = ['GET', 'POST', 'DELETE']; |
96
|
|
|
|
97
|
|
|
if ( ! in_array($type, $validTypes)) |
98
|
|
|
{ |
99
|
|
|
throw new InvalidArgumentException('Invalid http request type'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$config = $this->container->get('config'); |
|
|
|
|
103
|
|
|
$logger = $this->container->getLogger('mal-request'); |
|
|
|
|
104
|
|
|
|
105
|
|
|
$headers = array_merge($this->defaultHeaders, $options['headers'] ?? [], [ |
106
|
|
|
'Authorization' => 'Basic ' . |
107
|
|
|
base64_encode($config->get(['mal','username']) . ':' .$config->get(['mal','password'])) |
108
|
|
|
]); |
109
|
|
|
|
110
|
|
|
$query = $options['query'] ?? []; |
111
|
|
|
|
112
|
|
|
$url = (strpos($url, '//') !== FALSE) |
113
|
|
|
? $url |
114
|
|
|
: $this->baseUrl . $url; |
115
|
|
|
|
116
|
|
|
if ( ! empty($query)) |
117
|
|
|
{ |
118
|
|
|
$url .= '?' . http_build_query($query); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$request = (new Request) |
122
|
|
|
->setMethod($type) |
123
|
|
|
->setUri($url) |
124
|
|
|
->setProtocol('1.1') |
125
|
|
|
->setAllHeaders($headers); |
126
|
|
|
|
127
|
|
|
if (array_key_exists('body', $options)) |
128
|
|
|
{ |
129
|
|
|
$request->setBody($options['body']); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $request; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Make a request |
137
|
|
|
* |
138
|
|
|
* @param string $type |
139
|
|
|
* @param string $url |
140
|
|
|
* @param array $options |
141
|
|
|
* @return \Amp\Artax\Response |
142
|
|
|
*/ |
143
|
|
|
private function getResponse(string $type, string $url, array $options = []) |
144
|
|
|
{ |
145
|
|
|
$logger = null; |
146
|
|
|
if ($this->getContainer()) |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
$logger = $this->container->getLogger('mal-request'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$request = $this->setUpRequest($type, $url, $options); |
152
|
|
|
$response = \Amp\wait((new Client)->request($request)); |
153
|
|
|
|
154
|
|
|
$logger->debug('MAL api request', [ |
155
|
|
|
'url' => $url, |
156
|
|
|
'status' => $response->getStatus(), |
157
|
|
|
'reason' => $response->getReason(), |
158
|
|
|
'headers' => $response->getAllHeaders(), |
159
|
|
|
'requestHeaders' => $request->getAllHeaders(), |
160
|
|
|
'requestBody' => $request->hasBody() ? $request->getBody() : 'No request body', |
161
|
|
|
'requestBodyBeforeEncode' => $request->hasBody() ? urldecode($request->getBody()) : '', |
162
|
|
|
'body' => $response->getBody() |
163
|
|
|
]); |
164
|
|
|
|
165
|
|
|
return $response; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Make a request |
170
|
|
|
* |
171
|
|
|
* @param string $type |
172
|
|
|
* @param string $url |
173
|
|
|
* @param array $options |
174
|
|
|
* @return array |
175
|
|
|
*/ |
176
|
|
View Code Duplication |
private function request(string $type, string $url, array $options = []): array |
|
|
|
|
177
|
|
|
{ |
178
|
|
|
$logger = null; |
179
|
|
|
if ($this->getContainer()) |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
$logger = $this->container->getLogger('mal-request'); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$response = $this->getResponse($type, $url, $options); |
185
|
|
|
|
186
|
|
|
if ((int) $response->getStatus() > 299 || (int) $response->getStatus() < 200) |
187
|
|
|
{ |
188
|
|
|
if ($logger) |
189
|
|
|
{ |
190
|
|
|
$logger->warning('Non 200 response for api call', $response->getBody()); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return XML::toArray((string) $response->getBody()); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Remove some boilerplate for get requests |
199
|
|
|
* |
200
|
|
|
* @param array $args |
201
|
|
|
* @return array |
202
|
|
|
*/ |
203
|
|
|
protected function getRequest(...$args): array |
204
|
|
|
{ |
205
|
|
|
return $this->request('GET', ...$args); |
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Remove some boilerplate for post requests |
210
|
|
|
* |
211
|
|
|
* @param array $args |
212
|
|
|
* @return array |
213
|
|
|
*/ |
214
|
|
View Code Duplication |
protected function postRequest(...$args): array |
|
|
|
|
215
|
|
|
{ |
216
|
|
|
$logger = null; |
217
|
|
|
if ($this->getContainer()) |
|
|
|
|
218
|
|
|
{ |
219
|
|
|
$logger = $this->container->getLogger('mal-request'); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$response = $this->getResponse('POST', ...$args); |
|
|
|
|
223
|
|
|
$validResponseCodes = [200, 201]; |
224
|
|
|
|
225
|
|
|
if ( ! in_array((int) $response->getStatus(), $validResponseCodes)) |
226
|
|
|
{ |
227
|
|
|
if ($logger) |
228
|
|
|
{ |
229
|
|
|
$logger->warning('Non 201 response for POST api call', $response->getBody()); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return XML::toArray($response->getBody()); |
234
|
|
|
} |
235
|
|
|
} |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: