HummingBirdAnimeClient/src/Aviat/AnimeClient/Model/API.php

86 lines
1.6 KiB
PHP
Raw Normal View History

<?php
/**
* Base API Model
*/
2015-09-15 13:19:29 -04:00
namespace Aviat\AnimeClient\Model;
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Psr7\Request;
2015-09-17 23:11:18 -04:00
use Aviat\Ion\Di\ContainerInterface;
use Aviat\AnimeClient\Model as BaseModel;
/**
* Base model for api interaction
*/
2015-09-17 23:11:18 -04:00
class API extends BaseModel {
/**
* Base url for making api requests
* @var string
*/
protected $base_url = '';
/**
* The Guzzle http client object
* @var object
*/
protected $client;
/**
* Cookie jar object for api requests
* @var object
*/
protected $cookieJar;
/**
* Constructor
*
* @param ContainerInterface $container
*/
2015-09-17 23:11:18 -04:00
public function __construct(ContainerInterface $container)
{
parent::__construct($container);
$this->cookieJar = new CookieJar();
$this->client = new Client([
'base_uri' => $this->base_url,
'cookies' => TRUE,
'defaults' => [
'cookies' => $this->cookieJar,
'headers' => [
'User-Agent' => $_SERVER['HTTP_USER_AGENT'],
'Accept-Encoding' => 'application/json'
],
'timeout' => 5,
'connect_timeout' => 5
]
]);
}
/**
* Attempt login via the api
*
* @codeCoverageIgnore
* @param string $username
* @param string $password
2015-10-14 09:20:52 -04:00
* @return string|false
*/
public function authenticate($username, $password)
{
$result = $this->client->post('https://hummingbird.me/api/v1/users/authenticate', [
'body' => [
2015-06-26 16:39:10 -04:00
'username' => $username,
'password' => $password
]
]);
if ($result->getStatusCode() === 201)
{
2015-10-14 09:20:52 -04:00
return json_decode($result->getBody(), TRUE);
}
return FALSE;
}
}
// End of BaseApiModel.php