Able to create list items on MAL
This commit is contained in:
parent
82e0078dbf
commit
47bfe810fc
@ -16,29 +16,44 @@
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* CRUD operations for MAL list items
|
||||
*/
|
||||
class ListItem extends AbstractListItem {
|
||||
class ListItem {
|
||||
use ContainerAware;
|
||||
use MALTrait;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->init();
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
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
|
||||
@ -48,6 +63,16 @@ class ListItem extends AbstractListItem {
|
||||
|
||||
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
|
||||
]);
|
||||
}
|
||||
}
|
@ -16,18 +16,15 @@
|
||||
|
||||
namespace Aviat\AnimeClient\API\MAL;
|
||||
|
||||
use Amp\Artax\{Client, Request};
|
||||
use Aviat\AnimeClient\API\{
|
||||
GuzzleTrait,
|
||||
MAL as M,
|
||||
XML
|
||||
};
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Cookie\CookieJar;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Aviat\Ion\Json;
|
||||
use InvalidArgumentException;
|
||||
|
||||
trait MALTrait {
|
||||
use GuzzleTrait;
|
||||
|
||||
/**
|
||||
* The base url for api requests
|
||||
@ -44,29 +41,6 @@ trait MALTrait {
|
||||
'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
|
||||
*
|
||||
@ -88,20 +62,28 @@ trait MALTrait {
|
||||
$config = $this->container->get('config');
|
||||
$logger = $this->container->getLogger('request');
|
||||
|
||||
$defaultOptions = [
|
||||
'auth' => [
|
||||
$config->get(['mal','username']),
|
||||
$config->get(['mal','password'])
|
||||
],
|
||||
'headers' => $this->defaultHeaders
|
||||
];
|
||||
$headers = array_merge($this->defaultHeaders, $options['headers'] ?? [], [
|
||||
'Authorization' => 'Basic ' .
|
||||
base64_encode($config->get(['mal','username']) . ':' .$config->get(['mal','password']))
|
||||
]);
|
||||
|
||||
$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($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);
|
||||
|
||||
if ((int) $response->getStatusCode() > 299 || (int) $response->getStatusCode() < 200)
|
||||
if ((int) $response->getStatus() > 299 || (int) $response->getStatus() < 200)
|
||||
{
|
||||
if ($logger)
|
||||
{
|
||||
$logger->warning('Non 200 response for api call');
|
||||
$logger->warning($response->getBody());
|
||||
}
|
||||
|
||||
// throw new RuntimeException($response->getBody());
|
||||
}
|
||||
|
||||
return XML::toArray((string) $response->getBody());
|
||||
@ -164,7 +144,7 @@ trait MALTrait {
|
||||
$response = $this->getResponse('POST', ...$args);
|
||||
$validResponseCodes = [200, 201];
|
||||
|
||||
if ( ! in_array((int) $response->getStatusCode(), $validResponseCodes))
|
||||
if ( ! in_array((int) $response->getStatus(), $validResponseCodes))
|
||||
{
|
||||
if ($logger)
|
||||
{
|
||||
@ -173,18 +153,6 @@ trait MALTrait {
|
||||
}
|
||||
}
|
||||
|
||||
return XML::toArray((string) $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);
|
||||
return XML::toArray($response->getBody());
|
||||
}
|
||||
}
|
@ -23,6 +23,7 @@ use Aviat\AnimeClient\API\MAL\{
|
||||
};
|
||||
use Aviat\AnimeClient\API\XML;
|
||||
use Aviat\Ion\Di\ContainerAware;
|
||||
use Aviat\Ion\Json;
|
||||
|
||||
/**
|
||||
* MyAnimeList API Model
|
||||
@ -41,15 +42,13 @@ class Model {
|
||||
*/
|
||||
public function __construct(ListItem $listItem)
|
||||
{
|
||||
// Set up Guzzle trait
|
||||
$this->init();
|
||||
$this->animeListTransformer = new AnimeListTransformer();
|
||||
//$this->animeListTransformer = new AnimeListTransformer();
|
||||
$this->listItem = $listItem;
|
||||
}
|
||||
|
||||
public function createListItem(array $data): bool
|
||||
{
|
||||
return FALSE;
|
||||
return $this->listItem->create($data);
|
||||
}
|
||||
|
||||
public function getListItem(string $listId): array
|
||||
@ -59,12 +58,12 @@ class Model {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public function deleteListItem(string $id): bool
|
||||
{
|
||||
|
||||
return $this->listItem->delete($id);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user