Fix update requests broken by Artax conversion

This commit is contained in:
Timothy Warren 2017-02-09 20:10:13 -05:00
parent c9f74dd863
commit e2aa61f580
7 changed files with 48 additions and 31 deletions

View File

@ -157,12 +157,15 @@ class APIRequestBuilder {
/** /**
* Set the request body * Set the request body
* *
* @param array|FormBody|string $body * @param mixed $body
* @return self * @return self
*/ */
public function setJsonBody(array $body): self public function setJsonBody($body): self
{ {
$requestBody = Json::encode($body); $requestBody = ( ! is_scalar($body))
? Json::encode($body)
: $body;
return $this->setBody($requestBody); return $this->setBody($requestBody);
} }

View File

@ -17,7 +17,6 @@
namespace Aviat\AnimeClient\API\Kitsu; namespace Aviat\AnimeClient\API\Kitsu;
use const Aviat\AnimeClient\SESSION_SEGMENT; use const Aviat\AnimeClient\SESSION_SEGMENT;
use const Aviat\AnimeClient\API\Kitsu\AUTH_TOKEN_CACHE_KEY;
use Aviat\AnimeClient\AnimeClient; use Aviat\AnimeClient\AnimeClient;
use Aviat\AnimeClient\API\{ use Aviat\AnimeClient\API\{
@ -87,7 +86,7 @@ class Auth {
if (FALSE !== $auth) if (FALSE !== $auth)
{ {
// Set the token in the cache for command line operations // Set the token in the cache for command line operations
$cacheItem = $this->cache->getItem(AUTH_TOKEN_CACHE_KEY); $cacheItem = $this->cache->getItem(K::AUTH_TOKEN_CACHE_KEY);
$cacheItem->set($auth['access_token']); $cacheItem->set($auth['access_token']);
$cacheItem->save(); $cacheItem->save();

View File

@ -72,6 +72,11 @@ trait KitsuTrait {
// $defaultOptions['headers']['Authorization'] = "bearer {$token}"; // $defaultOptions['headers']['Authorization'] = "bearer {$token}";
} }
if (array_key_exists('form_params', $options))
{
$request->setFormFields($options['form_params']);
}
if (array_key_exists('query', $options)) if (array_key_exists('query', $options))
{ {
$request->setQuery($options['query']); $request->setQuery($options['query']);

View File

@ -104,11 +104,19 @@ class ListItem extends AbstractListItem {
public function get(string $id): array public function get(string $id): array
{ {
$authHeader = $this->getAuthHeader();
$request = $this->requestBuilder->newRequest('GET', "library-entries/{$id}") $request = $this->requestBuilder->newRequest('GET', "library-entries/{$id}")
->setQuery([ ->setQuery([
'include' => 'media,media.genres,media.mappings' 'include' => 'media,media.genres,media.mappings'
]) ]);
->getFullRequest();
if ($authHeader !== FALSE)
{
$request = $request->setHeader('Authorization', $authHeader);
}
$request = $request->getFullRequest();
$response = \Amp\wait((new \Amp\Artax\Client)->request($request)); $response = \Amp\wait((new \Amp\Artax\Client)->request($request));
return Json::decode($response->getBody()); return Json::decode($response->getBody());
@ -116,6 +124,7 @@ class ListItem extends AbstractListItem {
public function update(string $id, array $data): Request public function update(string $id, array $data): Request
{ {
$authHeader = $this->getAuthHeader();
$requestData = [ $requestData = [
'data' => [ 'data' => [
'id' => $id, 'id' => $id,
@ -124,10 +133,14 @@ class ListItem extends AbstractListItem {
] ]
]; ];
$response = $this->getResponse('PATCH', "library-entries/{$id}", [ $request = $this->requestBuilder->newRequest('PATCH', "library-entries/{$id}")
'body' => JSON::encode($requestData) ->setJsonBody($requestData);
]);
return $response; if ($authHeader !== FALSE)
{
$request = $request->setHeader('Authorization', $authHeader);
}
return $request->getFullRequest();
} }
} }

View File

@ -398,21 +398,7 @@ class Model {
*/ */
public function updateListItem(array $data): Request public function updateListItem(array $data): Request
{ {
try return $this->listItem->update($data['id'], $data['data']);
{
$response = $this->listItem->update($data['id'], $data['data']);
return [
'statusCode' => $response->getStatus(),
'body' => $response->getBody(),
];
}
catch(ClientException $e)
{
return [
'statusCode' => $e->getResponse()->getStatus(),
'body' => Json::decode((string)$e->getResponse()->getBody())
];
}
} }
/** /**

View File

@ -16,7 +16,7 @@
namespace Aviat\AnimeClient\API\MAL; namespace Aviat\AnimeClient\API\MAL;
use Amp\Artax\Request; use Amp\Artax\{FormBody, Request};
use Aviat\AnimeClient\API\{ use Aviat\AnimeClient\API\{
AbstractListItem, AbstractListItem,
XML XML

View File

@ -168,12 +168,23 @@ class Anime extends API {
*/ */
public function updateLibraryItem(array $data): array public function updateLibraryItem(array $data): array
{ {
$requests = [];
if ($this->useMALAPI) if ($this->useMALAPI)
{ {
$this->malModel->updateListItem($data); $requests['mal'] = $this->malModel->updateListItem($data);
} }
return $this->kitsuModel->updateListItem($data); $requests['kitsu'] = $this->kitsuModel->updateListItem($data);
$promises = (new Client)->requestMulti($requests);
$results = wait(some($promises));
return [
'body' => Json::decode($results[1]['kitsu']->getBody()),
'statusCode' => $results[1]['kitsu']->getStatus()
];
} }
/** /**