Version 5.1 - All the GraphQL #32
@ -13,7 +13,7 @@
|
||||
</th>
|
||||
<th>
|
||||
<article class="media">
|
||||
<?= $helper->img($item['cover_image']); ?>
|
||||
<?= $helper->img($urlGenerator->assetUrl("images/anime/{$item['hummingbird_id']}.jpg")); ?>
|
||||
</article>
|
||||
</th>
|
||||
</tr>
|
||||
|
@ -393,12 +393,12 @@ class DoubleQuoteUsageSniff extends VariableUsageSniff
|
||||
&& false === $smpl_qt_at
|
||||
) {
|
||||
$error = 'Single-quoted strings should be used unless it contains variables, special chars like \n or single quotes.';
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
$phpcsFile->addError($error, $stackPtr, 111);
|
||||
} else if (false !== $smpl_qt_at && false !== $dbl_qt_at
|
||||
&& false === $has_variable && false === $has_specific_sequence
|
||||
) {
|
||||
$warning = 'It is encouraged to use a single-quoted string, since it doesn\'t contain any variable nor special char though it mixes single and double quotes.';
|
||||
$phpcsFile->addWarning($warning, $stackPtr);
|
||||
$phpcsFile->addWarning($warning, $stackPtr, 222);
|
||||
}
|
||||
}//end processDoubleQuotedString()
|
||||
|
||||
@ -426,7 +426,7 @@ class DoubleQuoteUsageSniff extends VariableUsageSniff
|
||||
$smpl_qt_at = strpos($qtString, "'");
|
||||
if (false === $has_variable && false !== $smpl_qt_at && false === $dbl_qt_at) {
|
||||
$warning = 'You may also use double-quoted strings if the string contains single quotes, so you do not have to use escape characters.';
|
||||
$phpcsFile->addWarning($warning, $stackPtr);
|
||||
$phpcsFile->addWarning($warning, $stackPtr, 333);
|
||||
}
|
||||
}//end processSingleQuotedString()
|
||||
|
||||
|
@ -17,7 +17,7 @@
|
||||
}
|
||||
},
|
||||
"require": {
|
||||
"amphp/artax": "^2.0",
|
||||
"amphp/artax": "^3.0",
|
||||
"aura/html": "^2.0",
|
||||
"aura/router": "^3.0",
|
||||
"aura/session": "^2.0",
|
||||
@ -42,7 +42,9 @@
|
||||
"sebastian/phpcpd": "^3.0",
|
||||
"spatie/phpunit-snapshot-assertions": "^1.2.0",
|
||||
"squizlabs/php_codesniffer": "^3.0.0@beta",
|
||||
"theseer/phpdox": "^0.10.1"
|
||||
"symfony/var-dumper": "^4.0.1",
|
||||
"theseer/phpdox": "^0.10.1",
|
||||
"filp/whoops": "^2.1"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "vendor/bin/robo build",
|
||||
|
@ -28,6 +28,10 @@ if ($timezone === '' || $timezone === FALSE)
|
||||
// Load composer autoloader
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
$whoops = new \Whoops\Run;
|
||||
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);
|
||||
$whoops->register();
|
||||
|
||||
// Define base directories
|
||||
$APP_DIR = _dir(__DIR__, 'app');
|
||||
$APPCONF_DIR = _dir($APP_DIR, 'appConf');
|
||||
|
@ -16,8 +16,9 @@
|
||||
|
||||
namespace Aviat\EasyMin;
|
||||
|
||||
use function Amp\wait;
|
||||
use Amp\Artax\{Client, FormBody, Request};
|
||||
use function Amp\Promise\wait;
|
||||
use Amp\Artax\Request;
|
||||
use Aviat\AnimeClient\API\HummingbirdClient;
|
||||
use Aviat\Ion\{Json, JsonException};
|
||||
|
||||
// Include Amp and Artax
|
||||
@ -113,24 +114,24 @@ class JSMin {
|
||||
* Makes a call to google closure compiler service
|
||||
*
|
||||
* @param array $options - Form parameters
|
||||
* @throws \TypeError
|
||||
* @return object
|
||||
*/
|
||||
protected function closureCall(array $options)
|
||||
{
|
||||
$formFields = http_build_query($options);
|
||||
|
||||
$request = (new Request)
|
||||
->setMethod('POST')
|
||||
->setUri('https://closure-compiler.appspot.com/compile')
|
||||
->setAllHeaders([
|
||||
$request = (new Request('https://closure-compiler.appspot.com/compile'))
|
||||
->withMethod('POST')
|
||||
->withHeaders([
|
||||
'Accept' => 'application/json',
|
||||
'Accept-Encoding' => 'gzip',
|
||||
'Content-type' => 'application/x-www-form-urlencoded'
|
||||
])
|
||||
->setBody($formFields);
|
||||
->withBody($formFields);
|
||||
|
||||
$response = wait((new Client)->request($request, [
|
||||
Client::OP_AUTO_ENCODING => false
|
||||
$response = wait((new HummingbirdClient)->request($request, [
|
||||
HummingbirdClient::OP_AUTO_ENCODING => false
|
||||
]));
|
||||
|
||||
return $response;
|
||||
@ -147,7 +148,7 @@ class JSMin {
|
||||
try
|
||||
{
|
||||
$errorRes = $this->closureCall($options);
|
||||
$errorJson = $errorRes->getBody();
|
||||
$errorJson = wait($errorRes->getBody());
|
||||
$errorObj = Json::decode($errorJson) ?: (object)[];
|
||||
|
||||
|
||||
@ -237,7 +238,7 @@ class JSMin {
|
||||
// Now actually retrieve the compiled code
|
||||
$options['output_info'] = 'compiled_code';
|
||||
$res = $this->closureCall($options);
|
||||
$json = $res->getBody();
|
||||
$json = wait($res->getBody());
|
||||
$obj = Json::decode($json);
|
||||
|
||||
//return $obj;
|
||||
|
@ -16,6 +16,8 @@
|
||||
|
||||
namespace Aviat\AnimeClient\API;
|
||||
|
||||
use function Amp\Promise\wait;
|
||||
|
||||
use Amp;
|
||||
use Amp\Artax\{FormBody, Request};
|
||||
use Aviat\Ion\Json;
|
||||
@ -23,7 +25,7 @@ use InvalidArgumentException;
|
||||
use Psr\Log\LoggerAwareTrait;
|
||||
|
||||
/**
|
||||
* Wrapper around Artex to make it easier to build API requests
|
||||
* Wrapper around Artax to make it easier to build API requests
|
||||
*/
|
||||
class APIRequestBuilder {
|
||||
use LoggerAwareTrait;
|
||||
@ -60,7 +62,7 @@ class APIRequestBuilder {
|
||||
|
||||
/**
|
||||
* The current request
|
||||
* @var \Amp\Promise
|
||||
* @var \Amp\Artax\Request
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
@ -96,11 +98,12 @@ class APIRequestBuilder {
|
||||
* Set the request body
|
||||
*
|
||||
* @param FormBody|string $body
|
||||
* @throws \TypeError
|
||||
* @return self
|
||||
*/
|
||||
public function setBody($body): self
|
||||
{
|
||||
$this->request->setBody($body);
|
||||
$this->request = $this->request->withBody($body);
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -108,13 +111,26 @@ class APIRequestBuilder {
|
||||
* Set body as form fields
|
||||
*
|
||||
* @param array $fields Mapping of field names to values
|
||||
* @throws \TypeError
|
||||
* @return self
|
||||
*/
|
||||
public function setFormFields(array $fields): self
|
||||
{
|
||||
$this->setHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||
$body = (new FormBody)->addFields($fields);
|
||||
$this->setBody($body);
|
||||
$body = new FormBody();
|
||||
$body->addFields($fields);
|
||||
|
||||
return $this->setBody($body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unset a request header
|
||||
*
|
||||
* @param string $name
|
||||
* @return self
|
||||
*/
|
||||
public function unsetHeader(string $name): self
|
||||
{
|
||||
$this->request = $this->request->withoutHeader($name);
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -125,9 +141,17 @@ class APIRequestBuilder {
|
||||
* @param string $value
|
||||
* @return self
|
||||
*/
|
||||
public function setHeader(string $name, string $value): self
|
||||
public function setHeader(string $name, string $value = NULL): self
|
||||
{
|
||||
$this->request->setHeader($name, $value);
|
||||
if (NULL === $value)
|
||||
{
|
||||
$this->unsetHeader($name);
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->request = $this->request->withHeader($name, $value);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -153,6 +177,7 @@ class APIRequestBuilder {
|
||||
* Set the request body
|
||||
*
|
||||
* @param mixed $body
|
||||
* @throws \TypeError
|
||||
* @return self
|
||||
*/
|
||||
public function setJsonBody($body): self
|
||||
@ -179,9 +204,10 @@ class APIRequestBuilder {
|
||||
/**
|
||||
* Return the promise for the current request
|
||||
*
|
||||
* @return \Amp\Promise
|
||||
* @throws \Throwable
|
||||
* @return \Amp\Artax\Request
|
||||
*/
|
||||
public function getFullRequest()
|
||||
public function getFullRequest(): Request
|
||||
{
|
||||
$this->buildUri();
|
||||
|
||||
@ -189,8 +215,12 @@ class APIRequestBuilder {
|
||||
{
|
||||
$this->logger->debug('API Request', [
|
||||
'request_url' => $this->request->getUri(),
|
||||
'request_headers' => $this->request->getAllHeaders(),
|
||||
'request_body' => $this->request->getBody()
|
||||
'request_headers' => $this->request->getHeaders(),
|
||||
'request_body' => wait(
|
||||
$this->request->getBody()
|
||||
->createBodyStream()
|
||||
->read()
|
||||
)
|
||||
]);
|
||||
}
|
||||
|
||||
@ -207,19 +237,21 @@ class APIRequestBuilder {
|
||||
*/
|
||||
public function newRequest(string $type, string $uri): self
|
||||
{
|
||||
if ( ! in_array($type, $this->validMethods))
|
||||
if ( ! \in_array($type, $this->validMethods, TRUE))
|
||||
{
|
||||
throw new InvalidArgumentException('Invalid HTTP methods');
|
||||
throw new InvalidArgumentException('Invalid HTTP method');
|
||||
}
|
||||
|
||||
$this->resetState();
|
||||
|
||||
$this->request
|
||||
->setMethod($type)
|
||||
->setProtocol('1.1');
|
||||
$realUrl = (strpos($uri, '//') !== FALSE)
|
||||
? $uri
|
||||
: $this->baseUrl . $uri;
|
||||
|
||||
$this->resetState($realUrl, $type);
|
||||
$this->path = $uri;
|
||||
|
||||
// Actually create the full url!
|
||||
$this->buildUri();
|
||||
|
||||
if ( ! empty($this->defaultHeaders))
|
||||
{
|
||||
$this->setHeaders($this->defaultHeaders);
|
||||
@ -231,9 +263,9 @@ class APIRequestBuilder {
|
||||
/**
|
||||
* Create the full request url
|
||||
*
|
||||
* @return void
|
||||
* @return Request
|
||||
*/
|
||||
private function buildUri()
|
||||
private function buildUri(): Request
|
||||
{
|
||||
$url = (strpos($this->path, '//') !== FALSE)
|
||||
? $this->path
|
||||
@ -244,18 +276,25 @@ class APIRequestBuilder {
|
||||
$url .= '?' . $this->query;
|
||||
}
|
||||
|
||||
$this->request->setUri($url);
|
||||
$this->request = $this->request->withUri($url);
|
||||
|
||||
return $this->request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the class state for a new request
|
||||
*
|
||||
* @param string $url
|
||||
* @param string $type
|
||||
* @return void
|
||||
*/
|
||||
private function resetState()
|
||||
private function resetState($url, $type = 'GET')
|
||||
{
|
||||
$requestUrl = $url ?: $this->baseUrl;
|
||||
|
||||
$this->path = '';
|
||||
$this->query = '';
|
||||
$this->request = new Request();
|
||||
$this->request = (new Request($requestUrl))
|
||||
->withMethod($type);
|
||||
}
|
||||
}
|
1155
src/API/HummingbirdClient.php
Normal file
1155
src/API/HummingbirdClient.php
Normal file
File diff suppressed because it is too large
Load Diff
@ -73,14 +73,14 @@ class Auth {
|
||||
$config = $this->container->get('config');
|
||||
$username = $config->get(['kitsu_username']);
|
||||
|
||||
try
|
||||
// try
|
||||
{
|
||||
$auth = $this->model->authenticate($username, $password);
|
||||
}
|
||||
catch (Exception $e)
|
||||
/* catch (Exception $e)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
if (FALSE !== $auth)
|
||||
|
@ -16,10 +16,7 @@
|
||||
|
||||
namespace Aviat\AnimeClient\API\Kitsu;
|
||||
|
||||
use Aviat\AnimeClient\API\{
|
||||
APIRequestBuilder,
|
||||
Kitsu as K
|
||||
};
|
||||
use Aviat\AnimeClient\API\APIRequestBuilder;
|
||||
|
||||
class KitsuRequestBuilder extends APIRequestBuilder {
|
||||
|
||||
@ -27,7 +24,7 @@ class KitsuRequestBuilder extends APIRequestBuilder {
|
||||
* The base url for api requests
|
||||
* @var string $base_url
|
||||
*/
|
||||
protected $baseUrl = "https://kitsu.io/api/edge/";
|
||||
protected $baseUrl = 'https://kitsu.io/api/edge/';
|
||||
|
||||
/**
|
||||
* HTTP headers to send with every request
|
||||
@ -36,9 +33,9 @@ class KitsuRequestBuilder extends APIRequestBuilder {
|
||||
*/
|
||||
protected $defaultHeaders = [
|
||||
'User-Agent' => "Tim's Anime Client/4.0",
|
||||
'Accept-Encoding' => 'application/vnd.api+json',
|
||||
'Accept' => 'application/vnd.api+json',
|
||||
'Content-Type' => 'application/vnd.api+json',
|
||||
'client_id' => 'dd031b32d2f56c990b1425efe6c42ad847e7fe3ab46bf1299f05ecd856bdb7dd',
|
||||
'client_secret' => '54d7307928f63414defd96399fc31ba847961ceaecef3a5fd93144e960c0e151',
|
||||
'CLIENT_ID' => 'dd031b32d2f56c990b1425efe6c42ad847e7fe3ab46bf1299f05ecd856bdb7dd',
|
||||
'CLIENT_SECRET' => '54d7307928f63414defd96399fc31ba847961ceaecef3a5fd93144e960c0e151',
|
||||
];
|
||||
}
|
@ -18,12 +18,17 @@ namespace Aviat\AnimeClient\API\Kitsu;
|
||||
|
||||
use const Aviat\AnimeClient\SESSION_SEGMENT;
|
||||
|
||||
use function Amp\wait;
|
||||
use function Amp\Promise\wait;
|
||||
|
||||
use Amp\Artax\{Client, Request};
|
||||
use Amp\Artax\Request;
|
||||
use Aviat\AnimeClient\AnimeClient;
|
||||
use Aviat\AnimeClient\API\{FailedResponseException, Kitsu as K};
|
||||
use Aviat\AnimeClient\API\{
|
||||
FailedResponseException,
|
||||
HummingbirdClient,
|
||||
Kitsu as K
|
||||
};
|
||||
use Aviat\Ion\Json;
|
||||
use Aviat\Ion\JsonException;
|
||||
|
||||
trait KitsuTrait {
|
||||
|
||||
@ -80,24 +85,29 @@ trait KitsuTrait {
|
||||
$token = $cacheItem->get();
|
||||
}
|
||||
|
||||
if ( ! is_null($token))
|
||||
if (NULL !== $token)
|
||||
{
|
||||
$request = $request->setAuth('bearer', $token);
|
||||
}
|
||||
|
||||
if (array_key_exists('form_params', $options))
|
||||
{
|
||||
$request->setFormFields($options['form_params']);
|
||||
$request = $request->setFormFields($options['form_params']);
|
||||
}
|
||||
|
||||
if (array_key_exists('query', $options))
|
||||
{
|
||||
$request->setQuery($options['query']);
|
||||
$request = $request->setQuery($options['query']);
|
||||
}
|
||||
|
||||
if (array_key_exists('body', $options))
|
||||
{
|
||||
$request->setJsonBody($options['body']);
|
||||
$request = $request->setJsonBody($options['body']);
|
||||
}
|
||||
|
||||
if (array_key_exists('headers', $options))
|
||||
{
|
||||
$request = $request->setHeaders($options['headers']);
|
||||
}
|
||||
|
||||
return $request->getFullRequest();
|
||||
@ -113,9 +123,24 @@ trait KitsuTrait {
|
||||
*/
|
||||
private function getResponse(string $type, string $url, array $options = [])
|
||||
{
|
||||
$logger = NULL;
|
||||
if ($this->getContainer())
|
||||
{
|
||||
$logger = $this->container->getLogger('kitsu-request');
|
||||
}
|
||||
|
||||
$request = $this->setUpRequest($type, $url, $options);
|
||||
|
||||
$response = wait((new Client)->request($request));
|
||||
$response = wait((new HummingbirdClient)->request($request));
|
||||
|
||||
if ($logger)
|
||||
{
|
||||
$logger->debug('Kitsu API Response', [
|
||||
'response_status' => $response->getStatus(),
|
||||
'request_headers' => $response->getOriginalRequest()->getHeaders(),
|
||||
'response_headers' => $response->getHeaders()
|
||||
]);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
@ -126,6 +151,8 @@ trait KitsuTrait {
|
||||
* @param string $type
|
||||
* @param string $url
|
||||
* @param array $options
|
||||
* @throws \Aviat\Ion\JsonException
|
||||
* @throws FailedResponseException
|
||||
* @return array
|
||||
*/
|
||||
private function request(string $type, string $url, array $options = []): array
|
||||
@ -148,7 +175,16 @@ trait KitsuTrait {
|
||||
throw new FailedResponseException('Failed to get the proper response from the API');
|
||||
}
|
||||
|
||||
return Json::decode($response->getBody(), TRUE);
|
||||
try
|
||||
{
|
||||
return Json::decode(wait($response->getBody()), TRUE);
|
||||
}
|
||||
catch (JsonException $e)
|
||||
{
|
||||
print_r($e);
|
||||
die();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -198,7 +234,7 @@ trait KitsuTrait {
|
||||
}
|
||||
}
|
||||
|
||||
return JSON::decode($response->getBody(), TRUE);
|
||||
return JSON::decode(wait($response->getBody()), TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -18,8 +18,10 @@ namespace Aviat\AnimeClient\API\Kitsu;
|
||||
|
||||
use const Aviat\AnimeClient\SESSION_SEGMENT;
|
||||
|
||||
use function Amp\Promise\wait;
|
||||
|
||||
use Amp\Artax\Request;
|
||||
use Aviat\AnimeClient\API\AbstractListItem;
|
||||
use Aviat\AnimeClient\API\{AbstractListItem, HummingbirdClient};
|
||||
use Aviat\Ion\Di\ContainerAware;
|
||||
use Aviat\Ion\Json;
|
||||
use RuntimeException;
|
||||
@ -126,8 +128,8 @@ class ListItem extends AbstractListItem {
|
||||
|
||||
$request = $request->getFullRequest();
|
||||
|
||||
$response = \Amp\wait((new \Amp\Artax\Client)->request($request));
|
||||
return Json::decode($response->getBody());
|
||||
$response = wait((new HummingbirdClient)->request($request));
|
||||
return Json::decode(wait($response->getBody()));
|
||||
}
|
||||
|
||||
public function update(string $id, array $data): Request
|
||||
|
@ -16,9 +16,9 @@
|
||||
|
||||
namespace Aviat\AnimeClient\API\Kitsu;
|
||||
|
||||
use function Amp\{all, wait};
|
||||
use function Amp\Promise\wait;
|
||||
|
||||
use Amp\Artax\{Client, Request};
|
||||
use Amp\Artax\Request;
|
||||
use Aviat\AnimeClient\API\{
|
||||
CacheTrait,
|
||||
JsonAPI,
|
||||
@ -101,22 +101,37 @@ class Model {
|
||||
*/
|
||||
public function authenticate(string $username, string $password)
|
||||
{
|
||||
// K::AUTH_URL
|
||||
$response = $this->getResponse('POST', K::AUTH_URL, [
|
||||
'headers' => [],
|
||||
'headers' => [
|
||||
'accept' => NULL,
|
||||
'Content-type' => 'application/x-www-form-urlencoded',
|
||||
'client_id' => NULL,
|
||||
'client_secret' => NULL
|
||||
],
|
||||
'form_params' => [
|
||||
'grant_type' => 'password',
|
||||
'username' => $username,
|
||||
'password' => $password
|
||||
]
|
||||
]);
|
||||
$data = Json::decode(wait($response->getBody()));
|
||||
|
||||
$data = Json::decode((string)$response->getBody());
|
||||
//dump($response);
|
||||
|
||||
if (array_key_exists('access_token', $data))
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
if (array_key_exists('error', $data))
|
||||
{
|
||||
dump($data['error']);
|
||||
dump($response);
|
||||
die();
|
||||
//throw new \Exception('auth error');
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
@ -129,14 +144,17 @@ class Model {
|
||||
public function reAuthenticate(string $token)
|
||||
{
|
||||
$response = $this->getResponse('POST', K::AUTH_URL, [
|
||||
'headers' => [],
|
||||
'headers' => [
|
||||
'Accept-encoding' => '*'
|
||||
|
||||
],
|
||||
'form_params' => [
|
||||
'grant_type' => 'refresh_token',
|
||||
'refresh_token' => $token
|
||||
]
|
||||
]);
|
||||
|
||||
$data = Json::decode((string)$response->getBody());
|
||||
$data = Json::decode(wait($response->getBody()));
|
||||
|
||||
if (array_key_exists('access_token', $data))
|
||||
{
|
||||
@ -186,7 +204,7 @@ class Model {
|
||||
*/
|
||||
public function getCharacter(string $slug): array
|
||||
{
|
||||
$data = $this->getRequest('/characters', [
|
||||
$data = $this->getRequest('characters', [
|
||||
'query' => [
|
||||
'filter' => [
|
||||
'slug' => $slug,
|
||||
@ -211,7 +229,7 @@ class Model {
|
||||
public function getUserData(string $username): array
|
||||
{
|
||||
// $userId = $this->getUserIdByUsername($username);
|
||||
$data = $this->getRequest("/users", [
|
||||
$data = $this->getRequest("users", [
|
||||
'query' => [
|
||||
'filter' => [
|
||||
'name' => $username,
|
||||
@ -425,7 +443,7 @@ class Model {
|
||||
|
||||
foreach($responses as $response)
|
||||
{
|
||||
$data = Json::decode($response->getBody());
|
||||
$data = Json::decode($response);
|
||||
$output = array_merge_recursive($output, $data);
|
||||
}
|
||||
|
||||
@ -522,7 +540,6 @@ class Model {
|
||||
*/
|
||||
public function getRawAnimeList(string $status): array
|
||||
{
|
||||
|
||||
$options = [
|
||||
'filter' => [
|
||||
'user_id' => $this->getUserIdByUsername($this->getUsername()),
|
||||
@ -689,7 +706,7 @@ class Model {
|
||||
|
||||
foreach($responses as $response)
|
||||
{
|
||||
$data = Json::decode($response->getBody());
|
||||
$data = Json::decode($response);
|
||||
$output = array_merge_recursive($output, $data);
|
||||
}
|
||||
|
||||
|
@ -94,9 +94,9 @@ class ListItem {
|
||||
$config = $this->container->get('config');
|
||||
|
||||
$xml = XML::toXML(['entry' => $data]);
|
||||
$body = (new FormBody)
|
||||
->addField('id', $id)
|
||||
->addField('data', $xml);
|
||||
$body = new FormBody();
|
||||
$body->addField('id', $id);
|
||||
$body->addField('data', $xml);
|
||||
|
||||
return $this->requestBuilder->newRequest('POST', "{$type}list/update/{$id}.xml")
|
||||
->setFormFields([
|
||||
|
@ -16,15 +16,13 @@
|
||||
|
||||
namespace Aviat\AnimeClient\API\MAL;
|
||||
|
||||
use Amp\Artax\{Client, FormBody, Request};
|
||||
use function Amp\Promise\wait;
|
||||
|
||||
use Aviat\AnimeClient\API\{
|
||||
HummingbirdClient,
|
||||
MAL as M,
|
||||
APIRequestBuilder,
|
||||
XML
|
||||
};
|
||||
use Aviat\AnimeClient\API\MALRequestBuilder;
|
||||
use Aviat\Ion\Json;
|
||||
use InvalidArgumentException;
|
||||
|
||||
trait MALTrait {
|
||||
|
||||
@ -82,12 +80,12 @@ trait MALTrait {
|
||||
|
||||
if (array_key_exists('query', $options))
|
||||
{
|
||||
$request->setQuery($options['query']);
|
||||
$request = $request->setQuery($options['query']);
|
||||
}
|
||||
|
||||
if (array_key_exists('body', $options))
|
||||
{
|
||||
$request->setBody($options['body']);
|
||||
$request = $request->setBody($options['body']);
|
||||
}
|
||||
|
||||
return $request->getFullRequest();
|
||||
@ -110,14 +108,14 @@ trait MALTrait {
|
||||
}
|
||||
|
||||
$request = $this->setUpRequest($type, $url, $options);
|
||||
$response = \Amp\wait((new Client)->request($request));
|
||||
$response = wait((new HummingbirdClient)->request($request));
|
||||
|
||||
$logger->debug('MAL api response', [
|
||||
'status' => $response->getStatus(),
|
||||
'reason' => $response->getReason(),
|
||||
'body' => $response->getBody(),
|
||||
'headers' => $response->getAllHeaders(),
|
||||
'requestHeaders' => $request->getAllHeaders(),
|
||||
'headers' => $response->getHeaders(),
|
||||
'requestHeaders' => $request->getHeaders(),
|
||||
]);
|
||||
|
||||
return $response;
|
||||
@ -149,7 +147,7 @@ trait MALTrait {
|
||||
}
|
||||
}
|
||||
|
||||
return XML::toArray((string) $response->getBody());
|
||||
return XML::toArray(wait($response->getBody()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -16,8 +16,8 @@
|
||||
|
||||
namespace Aviat\AnimeClient\API;
|
||||
|
||||
use Amp;
|
||||
use Amp\Artax\Client;
|
||||
use function Amp\call;
|
||||
use function Amp\Promise\{all, wait};
|
||||
|
||||
/**
|
||||
* Class to simplify making and validating simultaneous requests
|
||||
@ -65,18 +65,23 @@ class ParallelAPIRequest {
|
||||
/**
|
||||
* Actually make the requests
|
||||
*
|
||||
* @param bool $allowFailingRequests
|
||||
* @return array
|
||||
*/
|
||||
public function makeRequests(bool $allowFailingRequests = FALSE): array
|
||||
public function makeRequests(): array
|
||||
{
|
||||
$client = new Client();
|
||||
$promises = $client->requestMulti($this->requests);
|
||||
$client = new HummingbirdClient();
|
||||
$promises = [];
|
||||
|
||||
$func = ($allowFailingRequests) ? '\Amp\some' : '\Amp\all';
|
||||
foreach ($this->requests as $key => $url)
|
||||
{
|
||||
$promises[$key] = call(function () use ($client, $url) {
|
||||
$response = yield $client->request($url);
|
||||
$body = yield $response->getBody();
|
||||
|
||||
$results = Amp\wait($func($promises));
|
||||
return $body;
|
||||
});
|
||||
}
|
||||
|
||||
return $results;
|
||||
return wait(all($promises));
|
||||
}
|
||||
}
|
@ -21,7 +21,6 @@ use Aviat\AnimeClient\Model\{
|
||||
Anime as AnimeModel,
|
||||
AnimeCollection as AnimeCollectionModel
|
||||
};
|
||||
use Aviat\AnimeClient\UrlGenerator;
|
||||
use Aviat\Ion\Di\ContainerInterface;
|
||||
|
||||
/**
|
||||
|
@ -16,11 +16,10 @@
|
||||
|
||||
namespace Aviat\AnimeClient\Controller;
|
||||
|
||||
use function Amp\wait;
|
||||
use function Amp\Promise\wait;
|
||||
|
||||
use Amp\Artax\Client;
|
||||
use Aviat\AnimeClient\Controller as BaseController;
|
||||
use Aviat\AnimeClient\API\JsonAPI;
|
||||
use Aviat\AnimeClient\API\{HummingbirdClient, JsonAPI};
|
||||
use Aviat\Ion\View\HtmlView;
|
||||
|
||||
/**
|
||||
@ -109,13 +108,17 @@ class Index extends BaseController {
|
||||
$username = $this->config->get(['kitsu_username']);
|
||||
$model = $this->container->get('kitsu-model');
|
||||
$data = $model->getUserData($username);
|
||||
$orgData = JsonAPI::organizeData($data);
|
||||
$orgData = JsonAPI::organizeData($data)[0];
|
||||
$rels = $orgData['relationships'] ?? [];
|
||||
$favorites = array_key_exists('favorites', $rels) ? $rels['favorites'] : [];
|
||||
|
||||
|
||||
$this->outputHTML('me', [
|
||||
'title' => 'About ' . $this->config->get('whose_list'),
|
||||
'data' => $orgData[0],
|
||||
'attributes' => $orgData[0]['attributes'],
|
||||
'relationships' => $orgData[0]['relationships'],
|
||||
'favorites' => $this->organizeFavorites($orgData[0]['relationships']['favorites']),
|
||||
'data' => $orgData,
|
||||
'attributes' => $orgData['attributes'],
|
||||
'relationships' => $rels,
|
||||
'favorites' => $this->organizeFavorites($favorites),
|
||||
]);
|
||||
}
|
||||
|
||||
@ -151,14 +154,14 @@ class Index extends BaseController {
|
||||
return;
|
||||
}
|
||||
|
||||
$promise = (new Client)->request($kitsuUrl);
|
||||
$promise = (new HummingbirdClient)->request($kitsuUrl);
|
||||
$response = wait($promise);
|
||||
$data = (string) $response->getBody();
|
||||
$data = wait($response->getBody());
|
||||
|
||||
$baseSavePath = $this->config->get('img_cache_path');
|
||||
file_put_contents("{$baseSavePath}/{$type}/{$id}.{$ext}", $data);
|
||||
header('Content-type: ' . $response->getHeader('content-type')[0]);
|
||||
echo (string) $response->getBody();
|
||||
echo $data;
|
||||
}
|
||||
|
||||
private function organizeFavorites(array $rawfavorites): array
|
||||
|
@ -158,9 +158,9 @@ class Anime extends API {
|
||||
|
||||
$requester->addRequest($this->kitsuModel->createListItem($data), 'kitsu');
|
||||
|
||||
$results = $requester->makeRequests(TRUE);
|
||||
$results = $requester->makeRequests();
|
||||
|
||||
return count($results[1]) > 0;
|
||||
return count($results) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -180,11 +180,13 @@ class Anime extends API {
|
||||
|
||||
$requester->addRequest($this->kitsuModel->updateListItem($data), 'kitsu');
|
||||
|
||||
$results = $requester->makeRequests(TRUE);
|
||||
$results = $requester->makeRequests();
|
||||
$body = Json::decode($results['kitsu']);
|
||||
$statusCode = (array_key_exists('error', $body)) ? 400: 200;
|
||||
|
||||
return [
|
||||
'body' => Json::decode($results[1]['kitsu']->getBody()),
|
||||
'statusCode' => $results[1]['kitsu']->getStatus()
|
||||
'body' => Json::decode($results['kitsu']),
|
||||
'statusCode' => $statusCode
|
||||
];
|
||||
}
|
||||
|
||||
@ -206,8 +208,8 @@ class Anime extends API {
|
||||
|
||||
$requester->addRequest($this->kitsuModel->deleteListItem($id), 'kitsu');
|
||||
|
||||
$results = $requester->makeRequests(TRUE);
|
||||
$results = $requester->makeRequests();
|
||||
|
||||
return count($results[1]) > 0;
|
||||
return count($results) > 0;
|
||||
}
|
||||
}
|
@ -131,9 +131,9 @@ class Manga extends API
|
||||
|
||||
$requester->addRequest($this->kitsuModel->createListItem($data), 'kitsu');
|
||||
|
||||
$results = $requester->makeRequests(TRUE);
|
||||
$results = $requester->makeRequests();
|
||||
|
||||
return count($results[1]) > 0;
|
||||
return count($results) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -153,11 +153,13 @@ class Manga extends API
|
||||
|
||||
$requester->addRequest($this->kitsuModel->updateListItem($data), 'kitsu');
|
||||
|
||||
$results = $requester->makeRequests(TRUE);
|
||||
$results = $requester->makeRequests();
|
||||
$body = Json::decode($results['kitsu']);
|
||||
$statusCode = (array_key_exists('error', $body)) ? 400: 200;
|
||||
|
||||
return [
|
||||
'body' => Json::decode($results[1]['kitsu']->getBody()),
|
||||
'statusCode' => $results[1]['kitsu']->getStatus()
|
||||
'body' => Json::decode($results['kitsu']),
|
||||
'statusCode' => $statusCode
|
||||
];
|
||||
}
|
||||
|
||||
@ -179,9 +181,9 @@ class Manga extends API
|
||||
|
||||
$requester->addRequest($this->kitsuModel->deleteListItem($id), 'kitsu');
|
||||
|
||||
$results = $requester->makeRequests(TRUE);
|
||||
$results = $requester->makeRequests();
|
||||
|
||||
return count($results[1]) > 0;
|
||||
return count($results) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -16,9 +16,8 @@
|
||||
|
||||
namespace Aviat\AnimeClient\Tests\API;
|
||||
|
||||
use Amp;
|
||||
use Amp\Artax\Client;
|
||||
use Aviat\AnimeClient\API\APIRequestBuilder;
|
||||
use function Amp\Promise\wait;
|
||||
use Aviat\AnimeClient\API\{APIRequestBuilder, HummingbirdClient};
|
||||
use Aviat\Ion\Json;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\NullLogger;
|
||||
@ -42,8 +41,8 @@ class APIRequestBuilderTest extends TestCase {
|
||||
{
|
||||
$request = $this->builder->newRequest('GET', 'gzip')
|
||||
->getFullRequest();
|
||||
$response = Amp\wait((new Client)->request($request));
|
||||
$body = Json::decode($response->getBody());
|
||||
$response = wait((new HummingbirdClient)->request($request));
|
||||
$body = Json::decode(wait($response->getBody()));
|
||||
$this->assertEquals(1, $body['gzipped']);
|
||||
}
|
||||
|
||||
@ -60,8 +59,8 @@ class APIRequestBuilderTest extends TestCase {
|
||||
->setBasicAuth('username', 'password')
|
||||
->getFullRequest();
|
||||
|
||||
$response = Amp\wait((new Client)->request($request));
|
||||
$body = Json::decode($response->getBody());
|
||||
$response = wait((new HummingbirdClient)->request($request));
|
||||
$body = Json::decode(wait($response->getBody()));
|
||||
|
||||
$this->assertEquals('Basic dXNlcm5hbWU6cGFzc3dvcmQ=', $body['headers']['Authorization']);
|
||||
}
|
||||
@ -88,8 +87,8 @@ class APIRequestBuilderTest extends TestCase {
|
||||
->setQuery($query)
|
||||
->getFullRequest();
|
||||
|
||||
$response = Amp\wait((new Client)->request($request));
|
||||
$body = Json::decode($response->getBody());
|
||||
$response = wait((new HummingbirdClient)->request($request));
|
||||
$body = Json::decode(wait($response->getBody()));
|
||||
|
||||
$this->assertEquals($expected, $body['args']);
|
||||
}
|
||||
@ -105,8 +104,8 @@ class APIRequestBuilderTest extends TestCase {
|
||||
->setFormFields($formValues)
|
||||
->getFullRequest();
|
||||
|
||||
$response = Amp\wait((new Client)->request($request));
|
||||
$body = Json::decode($response->getBody());
|
||||
$response = wait((new HummingbirdClient)->request($request));
|
||||
$body = Json::decode(wait($response->getBody()));
|
||||
|
||||
$this->assertEquals($formValues, $body['form']);
|
||||
}
|
||||
@ -129,8 +128,8 @@ class APIRequestBuilderTest extends TestCase {
|
||||
->setJsonBody($data)
|
||||
->getFullRequest();
|
||||
|
||||
$response = Amp\wait((new Client)->request($request));
|
||||
$body = Json::decode($response->getBody());
|
||||
$response = wait((new HummingbirdClient)->request($request));
|
||||
$body = Json::decode(wait($response->getBody()));
|
||||
|
||||
$this->assertEquals($data, $body['json']);
|
||||
}
|
||||
|
@ -46,6 +46,5 @@ class MALTraitTest extends AnimeClientTestCase {
|
||||
]);
|
||||
$this->assertInstanceOf(\Amp\Artax\Request::class, $request);
|
||||
$this->assertEquals($request->getUri(), 'https://myanimelist.net/api/foo?foo=bar');
|
||||
$this->assertEquals($request->getBody(), '');
|
||||
}
|
||||
}
|
@ -53,9 +53,9 @@ class AnimeClientTestCase extends TestCase {
|
||||
public static function setUpBeforeClass()
|
||||
{
|
||||
// Use mock session handler
|
||||
$session_handler = new TestSessionHandler();
|
||||
session_set_save_handler($session_handler, TRUE);
|
||||
self::$session_handler = $session_handler;
|
||||
//$session_handler = new TestSessionHandler();
|
||||
//session_set_save_handler($session_handler, TRUE);
|
||||
//self::$session_handler = $session_handler;
|
||||
|
||||
// Remove test cache files
|
||||
$files = glob(_dir(TEST_DATA_DIR, 'cache', '*.json'));
|
||||
|
Loading…
Reference in New Issue
Block a user