Able to create list items on MAL

This commit is contained in:
Timothy Warren 2017-02-01 09:53:02 -05:00
parent 82e0078dbf
commit 47bfe810fc
3 changed files with 127 additions and 135 deletions

View File

@ -16,29 +16,44 @@
namespace Aviat\AnimeClient\API\MAL; namespace Aviat\AnimeClient\API\MAL;
use Aviat\AnimeClient\API\AbstractListItem; use Amp\Artax\FormBody;
use Aviat\AnimeClient\API\{
AbstractListItem,
XML
};
use Aviat\Ion\Di\ContainerAware; use Aviat\Ion\Di\ContainerAware;
/** /**
* CRUD operations for MAL list items * CRUD operations for MAL list items
*/ */
class ListItem extends AbstractListItem { class ListItem {
use ContainerAware; use ContainerAware;
use MALTrait; use MALTrait;
public function __construct()
{
$this->init();
}
public function create(array $data): bool public function create(array $data): bool
{ {
return FALSE; $id = $data['id'];
$body = (new FormBody)
->addField('id', $data['id'])
->addField('data', XML::toXML(['entry' => $data['data']]));
$response = $this->getResponse('POST', "animelist/add/{$id}.xml", [
'headers' => [
'Content-type' => 'application/x-www-form-urlencoded',
'Accept' => 'text/plain'
],
'body' => $body
]);
return $response->getStatus() === 201;
} }
public function delete(string $id): bool public function delete(string $id): bool
{ {
return FALSE; $response = $this->getResponse('DELETE', "animeclient/delete/{$id}.xml", [
'body' => (new FormBody)->addField('id', $id)
]);
return $response->getBody() === 'Deleted';
} }
public function get(string $id): array public function get(string $id): array
@ -48,6 +63,16 @@ class ListItem extends AbstractListItem {
public function update(string $id, array $data): Response public function update(string $id, array $data): Response
{ {
$body = (new FormBody)
->addField('id', $id)
->addField('data', XML::toXML(['entry' => $data]))
return $this->postRequest("animelist/update/{$id}.xml", [
'headers' => [
'Content-type' => 'application/x-www-form-urlencoded',
'Accept' => 'text/plain'
],
'body' => $body
]);
} }
} }

View File

@ -16,18 +16,15 @@
namespace Aviat\AnimeClient\API\MAL; namespace Aviat\AnimeClient\API\MAL;
use Amp\Artax\{Client, Request};
use Aviat\AnimeClient\API\{ use Aviat\AnimeClient\API\{
GuzzleTrait,
MAL as M, MAL as M,
XML XML
}; };
use GuzzleHttp\Client; use Aviat\Ion\Json;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Psr7\Response;
use InvalidArgumentException; use InvalidArgumentException;
trait MALTrait { trait MALTrait {
use GuzzleTrait;
/** /**
* The base url for api requests * The base url for api requests
@ -44,29 +41,6 @@ trait MALTrait {
'User-Agent' => "Tim's Anime Client/4.0" 'User-Agent' => "Tim's Anime Client/4.0"
]; ];
/**
* Set up the class properties
*
* @return void
*/
protected function init()
{
$defaults = [
'cookies' => $this->cookieJar,
'headers' => $this->defaultHeaders,
'timeout' => 25,
'connect_timeout' => 25
];
$this->cookieJar = new CookieJar();
$this->client = new Client([
'base_uri' => $this->baseUrl,
'cookies' => TRUE,
'http_errors' => TRUE,
'defaults' => $defaults
]);
}
/** /**
* Make a request via Guzzle * Make a request via Guzzle
* *
@ -87,21 +61,29 @@ trait MALTrait {
$config = $this->container->get('config'); $config = $this->container->get('config');
$logger = $this->container->getLogger('request'); $logger = $this->container->getLogger('request');
$defaultOptions = [ $headers = array_merge($this->defaultHeaders, $options['headers'] ?? [], [
'auth' => [ 'Authorization' => 'Basic ' .
$config->get(['mal','username']), base64_encode($config->get(['mal','username']) . ':' .$config->get(['mal','password']))
$config->get(['mal','password']) ]);
],
'headers' => $this->defaultHeaders $query = $options['query'] ?? [];
];
$url = (strpos($url, '//') !== FALSE)
$options = array_merge($defaultOptions, $options); ? $url . '?' . http_build_query($query)
: $this->baseUrl . $url . '?' . http_build_query($query);
$request = (new Request)
->setMethod($type)
->setUri($url)
->setProtocol('1.1')
->setAllHeaders($headers)
->setBody($options['body']);
$logger->debug(Json::encode([$type, $url])); $logger->debug(Json::encode([$type, $url]));
$logger->debug(Json::encode($options)); $logger->debug(Json::encode($options));
return $this->client->request($type, $url, $options); return \Amp\wait((new Client)->request($request));
} }
/** /**
@ -122,15 +104,13 @@ trait MALTrait {
$response = $this->getResponse($type, $url, $options); $response = $this->getResponse($type, $url, $options);
if ((int) $response->getStatusCode() > 299 || (int) $response->getStatusCode() < 200) if ((int) $response->getStatus() > 299 || (int) $response->getStatus() < 200)
{ {
if ($logger) if ($logger)
{ {
$logger->warning('Non 200 response for api call'); $logger->warning('Non 200 response for api call');
$logger->warning($response->getBody()); $logger->warning($response->getBody());
} }
// throw new RuntimeException($response->getBody());
} }
return XML::toArray((string) $response->getBody()); return XML::toArray((string) $response->getBody());
@ -164,7 +144,7 @@ trait MALTrait {
$response = $this->getResponse('POST', ...$args); $response = $this->getResponse('POST', ...$args);
$validResponseCodes = [200, 201]; $validResponseCodes = [200, 201];
if ( ! in_array((int) $response->getStatusCode(), $validResponseCodes)) if ( ! in_array((int) $response->getStatus(), $validResponseCodes))
{ {
if ($logger) if ($logger)
{ {
@ -173,18 +153,6 @@ trait MALTrait {
} }
} }
return XML::toArray((string) $response->getBody()); return XML::toArray($response->getBody());
}
/**
* Remove some boilerplate for delete requests
*
* @param array $args
* @return bool
*/
protected function deleteRequest(...$args): bool
{
$response = $this->getResponse('DELETE', ...$args);
return ((int) $response->getStatusCode() === 204);
} }
} }

View File

@ -1,70 +1,69 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
/** /**
* Anime List Client * Anime List Client
* *
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists * An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
* *
* PHP version 7 * PHP version 7
* *
* @package AnimeListClient * @package AnimeListClient
* @author Timothy J. Warren <tim@timshomepage.net> * @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2015 - 2017 Timothy J. Warren * @copyright 2015 - 2017 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License * @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 4.0 * @version 4.0
* @link https://github.com/timw4mail/HummingBirdAnimeClient * @link https://github.com/timw4mail/HummingBirdAnimeClient
*/ */
namespace Aviat\AnimeClient\API\MAL; namespace Aviat\AnimeClient\API\MAL;
use Aviat\AnimeClient\API\MAL as M; use Aviat\AnimeClient\API\MAL as M;
use Aviat\AnimeClient\API\MAL\{ use Aviat\AnimeClient\API\MAL\{
AnimeListTransformer, AnimeListTransformer,
ListItem ListItem
}; };
use Aviat\AnimeClient\API\XML; use Aviat\AnimeClient\API\XML;
use Aviat\Ion\Di\ContainerAware; use Aviat\Ion\Di\ContainerAware;
use Aviat\Ion\Json;
/**
* MyAnimeList API Model /**
*/ * MyAnimeList API Model
class Model { */
use ContainerAware; class Model {
use MALTrait; use ContainerAware;
use MALTrait;
/**
* @var AnimeListTransformer /**
*/ * @var AnimeListTransformer
protected $animeListTransformer; */
protected $animeListTransformer;
/**
* KitsuModel constructor. /**
*/ * KitsuModel constructor.
public function __construct(ListItem $listItem) */
{ public function __construct(ListItem $listItem)
// Set up Guzzle trait {
$this->init(); //$this->animeListTransformer = new AnimeListTransformer();
$this->animeListTransformer = new AnimeListTransformer(); $this->listItem = $listItem;
$this->listItem = $listItem; }
}
public function createListItem(array $data): bool
public function createListItem(array $data): bool {
{ return $this->listItem->create($data);
return FALSE; }
}
public function getListItem(string $listId): array
public function getListItem(string $listId): array {
{ return [];
return []; }
}
public function updateListItem(array $data)
public function updateListItem(array $data) {
{ //$updateData = $this->animeListTransformer->transform($data['data']);
$updateData = $this->animeListTransformer->transform($data['data']); return $this->listItem->update($data['mal_id'], $updateData);
return $this->listItem->update($data['mal_id'], $updateData); }
}
public function deleteListItem(string $id): bool
public function deleteListItem(string $id): bool {
{ return $this->listItem->delete($id);
}
}
} }