Fix update requests broken by Artax conversion

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

View File

@ -157,12 +157,15 @@ class APIRequestBuilder {
/**
* Set the request body
*
* @param array|FormBody|string $body
* @param mixed $body
* @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);
}

View File

@ -17,7 +17,6 @@
namespace Aviat\AnimeClient\API\Kitsu;
use const Aviat\AnimeClient\SESSION_SEGMENT;
use const Aviat\AnimeClient\API\Kitsu\AUTH_TOKEN_CACHE_KEY;
use Aviat\AnimeClient\AnimeClient;
use Aviat\AnimeClient\API\{
@ -87,7 +86,7 @@ class Auth {
if (FALSE !== $auth)
{
// 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->save();

View File

@ -71,6 +71,11 @@ trait KitsuTrait {
$request = $request->setAuth('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))
{

View File

@ -104,11 +104,19 @@ class ListItem extends AbstractListItem {
public function get(string $id): array
{
$authHeader = $this->getAuthHeader();
$request = $this->requestBuilder->newRequest('GET', "library-entries/{$id}")
->setQuery([
'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));
return Json::decode($response->getBody());
@ -116,6 +124,7 @@ class ListItem extends AbstractListItem {
public function update(string $id, array $data): Request
{
$authHeader = $this->getAuthHeader();
$requestData = [
'data' => [
'id' => $id,
@ -123,11 +132,15 @@ class ListItem extends AbstractListItem {
'attributes' => $data
]
];
$response = $this->getResponse('PATCH', "library-entries/{$id}", [
'body' => JSON::encode($requestData)
]);
return $response;
$request = $this->requestBuilder->newRequest('PATCH', "library-entries/{$id}")
->setJsonBody($requestData);
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
{
try
{
$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())
];
}
return $this->listItem->update($data['id'], $data['data']);
}
/**

View File

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

View File

@ -168,12 +168,23 @@ class Anime extends API {
*/
public function updateLibraryItem(array $data): array
{
$requests = [];
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()
];
}
/**