|
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 Amp\Artax\Request; |
|
22
|
|
|
use Aviat\AnimeClient\API\AbstractListItem; |
|
23
|
|
|
use Aviat\Ion\Di\ContainerAware; |
|
24
|
|
|
use Aviat\Ion\Json; |
|
25
|
|
|
use RuntimeException; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* CRUD operations for Kitsu list items |
|
29
|
|
|
*/ |
|
30
|
|
|
class ListItem extends AbstractListItem { |
|
31
|
|
|
use ContainerAware; |
|
32
|
|
|
use KitsuTrait; |
|
33
|
|
|
|
|
34
|
|
|
private function getAuthHeader() |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
$sessionSegment = $this->getContainer() |
|
37
|
|
|
->get('session') |
|
38
|
|
|
->getSegment(SESSION_SEGMENT); |
|
39
|
|
|
|
|
40
|
|
|
if ($sessionSegment->get('auth_token') !== null) |
|
41
|
|
|
{ |
|
42
|
|
|
$token = $sessionSegment->get('auth_token'); |
|
43
|
|
|
return "bearer {$token}"; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return FALSE; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function create(array $data): Request |
|
50
|
|
|
{ |
|
51
|
|
|
$body = [ |
|
52
|
|
|
'data' => [ |
|
53
|
|
|
'type' => 'libraryEntries', |
|
54
|
|
|
'attributes' => [ |
|
55
|
|
|
'status' => $data['status'], |
|
56
|
|
|
'progress' => $data['progress'] ?? 0 |
|
57
|
|
|
], |
|
58
|
|
|
'relationships' => [ |
|
59
|
|
|
'user' => [ |
|
60
|
|
|
'data' => [ |
|
61
|
|
|
'id' => $data['user_id'], |
|
62
|
|
|
'type' => 'users' |
|
63
|
|
|
] |
|
64
|
|
|
], |
|
65
|
|
|
'media' => [ |
|
66
|
|
|
'data' => [ |
|
67
|
|
|
'id' => $data['id'], |
|
68
|
|
|
'type' => $data['type'] |
|
69
|
|
|
] |
|
70
|
|
|
] |
|
71
|
|
|
] |
|
72
|
|
|
] |
|
73
|
|
|
]; |
|
74
|
|
|
|
|
75
|
|
|
$authHeader = $this->getAuthHeader(); |
|
76
|
|
|
|
|
77
|
|
|
$request = $this->requestBuilder->newRequest('POST', 'library-entries'); |
|
78
|
|
|
|
|
79
|
|
|
if ($authHeader !== FALSE) |
|
80
|
|
|
{ |
|
81
|
|
|
$request = $request->setHeader('Authorization', $authHeader); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $request->setJsonBody($body) |
|
85
|
|
|
->getFullRequest(); |
|
86
|
|
|
|
|
87
|
|
|
// return ($response->getStatus() === 201); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function delete(string $id): Request |
|
91
|
|
|
{ |
|
92
|
|
|
$authHeader = $this->getAuthHeader(); |
|
93
|
|
|
$request = $this->requestBuilder->newRequest('DELETE', "library-entries/{$id}"); |
|
94
|
|
|
|
|
95
|
|
|
if ($authHeader !== FALSE) |
|
96
|
|
|
{ |
|
97
|
|
|
$request = $request->setHeader('Authorization', $authHeader); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $request->getFullRequest(); |
|
101
|
|
|
|
|
102
|
|
|
// return ($response->getStatus() === 204); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function get(string $id): array |
|
106
|
|
|
{ |
|
107
|
|
|
$authHeader = $this->getAuthHeader(); |
|
108
|
|
|
|
|
109
|
|
|
$request = $this->requestBuilder->newRequest('GET', "library-entries/{$id}") |
|
110
|
|
|
->setQuery([ |
|
111
|
|
|
'include' => 'media,media.genres,media.mappings' |
|
112
|
|
|
]); |
|
113
|
|
|
|
|
114
|
|
|
if ($authHeader !== FALSE) |
|
115
|
|
|
{ |
|
116
|
|
|
$request = $request->setHeader('Authorization', $authHeader); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$request = $request->getFullRequest(); |
|
120
|
|
|
|
|
121
|
|
|
$response = \Amp\wait((new \Amp\Artax\Client)->request($request)); |
|
122
|
|
|
return Json::decode($response->getBody()); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function update(string $id, array $data): Request |
|
126
|
|
|
{ |
|
127
|
|
|
$authHeader = $this->getAuthHeader(); |
|
128
|
|
|
$requestData = [ |
|
129
|
|
|
'data' => [ |
|
130
|
|
|
'id' => $id, |
|
131
|
|
|
'type' => 'libraryEntries', |
|
132
|
|
|
'attributes' => $data |
|
133
|
|
|
] |
|
134
|
|
|
]; |
|
135
|
|
|
|
|
136
|
|
|
$request = $this->requestBuilder->newRequest('PATCH', "library-entries/{$id}") |
|
137
|
|
|
->setJsonBody($requestData); |
|
138
|
|
|
|
|
139
|
|
|
if ($authHeader !== FALSE) |
|
140
|
|
|
{ |
|
141
|
|
|
$request = $request->setHeader('Authorization', $authHeader); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $request->getFullRequest(); |
|
145
|
|
|
} |
|
146
|
|
|
} |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.