Do you wish to register an account?
API client for Kitsu.io, with optional Anime collection, and optional Anilist syncing.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

167 lines
3.7 KiB

  1. <?php declare(strict_types=1);
  2. /**
  3. * Hummingbird Anime List Client
  4. *
  5. * An API client for Kitsu to manage anime and manga watch lists
  6. *
  7. * PHP version 7.1
  8. *
  9. * @package HummingbirdAnimeClient
  10. * @author Timothy J. Warren <tim@timshomepage.net>
  11. * @copyright 2015 - 2018 Timothy J. Warren
  12. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  13. * @version 4.1
  14. * @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
  15. */
  16. namespace Aviat\AnimeClient\API\Kitsu;
  17. use const Aviat\AnimeClient\SESSION_SEGMENT;
  18. use function Amp\Promise\wait;
  19. use Amp\Artax\Request;
  20. use Aviat\AnimeClient\API\{
  21. HummingbirdClient,
  22. ListItemInterface
  23. };
  24. use Aviat\AnimeClient\Types\FormItemData;
  25. use Aviat\Ion\Di\ContainerAware;
  26. use Aviat\Ion\Json;
  27. /**
  28. * CRUD operations for Kitsu list items
  29. */
  30. final class ListItem implements ListItemInterface {
  31. use ContainerAware;
  32. use KitsuTrait;
  33. public function create(array $data): Request
  34. {
  35. $body = [
  36. 'data' => [
  37. 'type' => 'libraryEntries',
  38. 'attributes' => [
  39. 'status' => $data['status'],
  40. 'progress' => $data['progress'] ?? 0
  41. ],
  42. 'relationships' => [
  43. 'user' => [
  44. 'data' => [
  45. 'id' => $data['user_id'],
  46. 'type' => 'users'
  47. ]
  48. ],
  49. 'media' => [
  50. 'data' => [
  51. 'id' => $data['id'],
  52. 'type' => $data['type']
  53. ]
  54. ]
  55. ]
  56. ]
  57. ];
  58. if (array_key_exists('notes', $data))
  59. {
  60. $body['data']['attributes']['notes'] = $data['notes'];
  61. }
  62. $authHeader = $this->getAuthHeader();
  63. $request = $this->requestBuilder->newRequest('POST', 'library-entries');
  64. if ($authHeader !== FALSE)
  65. {
  66. $request = $request->setHeader('Authorization', $authHeader);
  67. }
  68. return $request->setJsonBody($body)
  69. ->getFullRequest();
  70. // return ($response->getStatus() === 201);
  71. }
  72. public function delete(string $id): Request
  73. {
  74. $authHeader = $this->getAuthHeader();
  75. $request = $this->requestBuilder->newRequest('DELETE', "library-entries/{$id}");
  76. if ($authHeader !== FALSE)
  77. {
  78. $request = $request->setHeader('Authorization', $authHeader);
  79. }
  80. return $request->getFullRequest();
  81. // return ($response->getStatus() === 204);
  82. }
  83. public function get(string $id): array
  84. {
  85. $authHeader = $this->getAuthHeader();
  86. $request = $this->requestBuilder->newRequest('GET', "library-entries/{$id}")
  87. ->setQuery([
  88. 'include' => 'media,media.categories,media.mappings'
  89. ]);
  90. if ($authHeader !== FALSE)
  91. {
  92. $request = $request->setHeader('Authorization', $authHeader);
  93. }
  94. $request = $request->getFullRequest();
  95. $response = wait((new HummingbirdClient)->request($request));
  96. return Json::decode(wait($response->getBody()));
  97. }
  98. public function increment(string $id, FormItemData $data): Request
  99. {
  100. return $this->update($id, $data);
  101. }
  102. public function update(string $id, FormItemData $data): Request
  103. {
  104. $authHeader = $this->getAuthHeader();
  105. $requestData = [
  106. 'data' => [
  107. 'id' => $id,
  108. 'type' => 'libraryEntries',
  109. 'attributes' => $data
  110. ]
  111. ];
  112. $request = $this->requestBuilder->newRequest('PATCH', "library-entries/{$id}")
  113. ->setJsonBody($requestData);
  114. if ($authHeader !== FALSE)
  115. {
  116. $request = $request->setHeader('Authorization', $authHeader);
  117. }
  118. return $request->getFullRequest();
  119. }
  120. private function getAuthHeader()
  121. {
  122. $cache = $this->getContainer()->get('cache');
  123. $cacheItem = $cache->getItem('kitsu-auth-token');
  124. $sessionSegment = $this->getContainer()
  125. ->get('session')
  126. ->getSegment(SESSION_SEGMENT);
  127. if ($sessionSegment->get('auth_token') !== NULL) {
  128. $token = $sessionSegment->get('auth_token');
  129. return "bearer {$token}";
  130. }
  131. if ($cacheItem->isHit()) {
  132. $token = $cacheItem->get();
  133. return "bearer {$token}";
  134. }
  135. return FALSE;
  136. }
  137. }