2017-02-07 13:11:42 -05:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Anime List Client
|
|
|
|
*
|
|
|
|
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
|
|
|
|
*
|
|
|
|
* PHP version 7
|
|
|
|
*
|
|
|
|
* @package AnimeListClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
|
|
|
* @copyright 2015 - 2017 Timothy J. Warren
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @version 4.0
|
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\API;
|
|
|
|
|
|
|
|
use Amp\Artax\{
|
2017-02-08 00:44:57 -05:00
|
|
|
Client,
|
|
|
|
FormBody,
|
2017-02-07 13:11:42 -05:00
|
|
|
Request
|
|
|
|
};
|
|
|
|
use Aviat\Ion\Di\ContainerAware;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use Psr\Log\LoggerAwareTrait;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper around Artex to make it easier to build API requests
|
|
|
|
*/
|
|
|
|
class APIRequestBuilder {
|
|
|
|
use LoggerAwareTrait;
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
/**
|
|
|
|
* Url prefix for making url requests
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $baseUrl = '';
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
/**
|
|
|
|
* Url path of the request
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $path = '';
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
/**
|
|
|
|
* Query string for the request
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $query = '';
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
/**
|
|
|
|
* Default request headers
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $defaultHeaders = [];
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
/**
|
|
|
|
* Valid HTTP request methos
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $validMethods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'];
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
/**
|
|
|
|
* The current request
|
|
|
|
* @var \Amp\Promise
|
|
|
|
*/
|
|
|
|
protected $request;
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
/**
|
2017-02-08 00:44:57 -05:00
|
|
|
* Set a basic authentication header
|
|
|
|
*
|
|
|
|
* @param string $username
|
|
|
|
* @param string $password
|
2017-02-07 13:11:42 -05:00
|
|
|
* @return self
|
|
|
|
*/
|
2017-02-08 00:44:57 -05:00
|
|
|
public function setBasicAuth(string $username, string $password): self
|
2017-02-07 13:11:42 -05:00
|
|
|
{
|
2017-02-08 00:44:57 -05:00
|
|
|
$authString = 'Basic ' . base64_encode($username . ':' . $password);
|
|
|
|
$this->setHeader('Authorization', $authString);
|
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
return $this;
|
|
|
|
}
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
/**
|
|
|
|
* Set the request body
|
|
|
|
*
|
|
|
|
* @param FormBody|string $body
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setBody($body): self
|
|
|
|
{
|
|
|
|
$this->request->setBody($body);
|
|
|
|
return $this;
|
|
|
|
}
|
2017-02-08 00:44:57 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set body as form fields
|
|
|
|
*
|
|
|
|
* @param array $fields Mapping of field names to values
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setFormFields(array $fields): self
|
|
|
|
{
|
|
|
|
$this->setHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
$body = $this->fixBody((new FormBody)->addFields($fields));
|
|
|
|
$this->setBody($body);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
/**
|
|
|
|
* Set a request header
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param string $value
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setHeader(string $name, string $value): self
|
|
|
|
{
|
|
|
|
$this->request->setHeader($name, $value);
|
|
|
|
return $this;
|
|
|
|
}
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
/**
|
|
|
|
* Set multiple request headers
|
2017-02-08 00:44:57 -05:00
|
|
|
*
|
2017-02-07 13:11:42 -05:00
|
|
|
* name => value
|
|
|
|
*
|
|
|
|
* @param array $headers
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setHeaders(array $headers): self
|
|
|
|
{
|
|
|
|
foreach ($headers as $name => $value)
|
|
|
|
{
|
|
|
|
$this->setHeader($name, $value);
|
|
|
|
}
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
return $this;
|
|
|
|
}
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
/**
|
|
|
|
* Append a query string in array format
|
|
|
|
*
|
|
|
|
* @param array $params
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setQuery(array $params): self
|
|
|
|
{
|
2017-02-08 00:44:57 -05:00
|
|
|
$this->query = http_build_query($params);
|
2017-02-07 13:11:42 -05:00
|
|
|
return $this;
|
|
|
|
}
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
/**
|
|
|
|
* Return the promise for the current request
|
|
|
|
*
|
|
|
|
* @return \Amp\Promise
|
|
|
|
*/
|
|
|
|
public function getFullRequest()
|
|
|
|
{
|
|
|
|
$this->buildUri();
|
2017-02-08 00:44:57 -05:00
|
|
|
|
|
|
|
if ($this->logger)
|
|
|
|
{
|
|
|
|
$this->logger->debug('API Request', [
|
|
|
|
'request_url' => $this->request->getUri(),
|
|
|
|
'request_headers' => $this->request->getAllHeaders(),
|
|
|
|
'request_body' => $this->request->getBody()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
return $this->request;
|
|
|
|
}
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
/**
|
|
|
|
* Create a new http request
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param string $uri
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function newRequest(string $type, string $uri): self
|
|
|
|
{
|
|
|
|
if ( ! in_array($type, $this->validMethods))
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException('Invalid HTTP methods');
|
|
|
|
}
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
$this->resetState();
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
$this->request
|
|
|
|
->setMethod($type)
|
|
|
|
->setProtocol('1.1');
|
|
|
|
|
2017-02-08 00:44:57 -05:00
|
|
|
$this->path = $uri;
|
|
|
|
|
|
|
|
if ( ! empty($this->defaultHeaders))
|
|
|
|
{
|
|
|
|
$this->setHeaders($this->defaultHeaders);
|
|
|
|
}
|
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
return $this;
|
|
|
|
}
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
/**
|
|
|
|
* Create the full request url
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function buildUri()
|
|
|
|
{
|
|
|
|
$url = (strpos($this->path, '//') !== FALSE)
|
|
|
|
? $this->path
|
2017-02-08 00:44:57 -05:00
|
|
|
: $this->baseUrl . $this->path;
|
2017-02-07 13:11:42 -05:00
|
|
|
|
|
|
|
if ( ! empty($this->query))
|
|
|
|
{
|
|
|
|
$url .= '?' . $this->query;
|
|
|
|
}
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
$this->request->setUri($url);
|
|
|
|
}
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
/**
|
|
|
|
* Unencode the dual-encoded ampersands in the body
|
|
|
|
*
|
|
|
|
* This is a dirty hack until I can fully track down where
|
|
|
|
* the dual-encoding happens
|
|
|
|
*
|
|
|
|
* @param FormBody $formBody The form builder object to fix
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function fixBody(FormBody $formBody): string
|
|
|
|
{
|
|
|
|
$rawBody = \Amp\wait($formBody->getBody());
|
|
|
|
return html_entity_decode($rawBody, \ENT_HTML5, 'UTF-8');
|
|
|
|
}
|
2017-02-08 00:44:57 -05:00
|
|
|
|
2017-02-07 13:11:42 -05:00
|
|
|
/**
|
|
|
|
* Reset the class state for a new request
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function resetState()
|
|
|
|
{
|
|
|
|
$this->path = '';
|
|
|
|
$this->query = '';
|
|
|
|
$this->request = new Request();
|
|
|
|
}
|
|
|
|
}
|