Version 5.1 - All the GraphQL #32

Closed
timw4mail wants to merge 1160 commits from develop into master
3 changed files with 127 additions and 135 deletions
Showing only changes of commit 47bfe810fc - Show all commits

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
* *
@ -88,20 +62,28 @@ 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
];
$options = array_merge($defaultOptions, $options); $query = $options['query'] ?? [];
$url = (strpos($url, '//') !== FALSE)
? $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

@ -23,6 +23,7 @@ use Aviat\AnimeClient\API\MAL\{
}; };
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
@ -41,15 +42,13 @@ class Model {
*/ */
public function __construct(ListItem $listItem) public function __construct(ListItem $listItem)
{ {
// Set up Guzzle trait //$this->animeListTransformer = new AnimeListTransformer();
$this->init();
$this->animeListTransformer = new AnimeListTransformer();
$this->listItem = $listItem; $this->listItem = $listItem;
} }
public function createListItem(array $data): bool public function createListItem(array $data): bool
{ {
return FALSE; return $this->listItem->create($data);
} }
public function getListItem(string $listId): array public function getListItem(string $listId): array
@ -59,12 +58,12 @@ class Model {
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);
} }
} }