Completed
Branch master (77709c)
by Timothy
06:48 queued 04:06
created

API::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 18
rs 9.4286
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
/**
3
 * Hummingbird Anime Client
4
 *
5
 * An API client for Hummingbird to manage anime and manga watch lists
6
 *
7
 * @package     HummingbirdAnimeClient
8
 * @author      Timothy J. Warren
9
 * @copyright   Copyright (c) 2015
10
 * @link        https://github.com/timw4mail/HummingBirdAnimeClient
11
 * @license     MIT
12
 */
13
namespace Aviat\AnimeClient\Model;
14
15
use GuzzleHttp\Client;
16
use GuzzleHttp\Cookie\CookieJar;
17
use GuzzleHttp\Psr7\Request;
18
use GuzzleHttp\Psr7\ResponseInterface;
19
use GuzzleHttp\Exception\ClientException;
20
21
use Aviat\Ion\Di\ContainerInterface;
22
use Aviat\AnimeClient\Model as BaseModel;
23
24
/**
25
 * Base model for api interaction
26
 *
27
 * @method ResponseInterface get(string $uri, array $options);
28
 * @method ResponseInterface delete(string $uri, array $options);
29
 * @method ResponseInterface head(string $uri, array $options);
30
 * @method ResponseInterface options(string $uri, array $options);
31
 * @method ResponseInterface patch(string $uri, array $options);
32
 * @method ResponseInterface post(string $uri, array $options);
33
 * @method ResponseInterface put(string $uri, array $options);
34
 */
35
class API extends BaseModel {
36
37
	/**
38
	 * Base url for making api requests
39
	 * @var string
40
	 */
41
	protected $base_url = '';
42
43
	/**
44
	 * The Guzzle http client object
45
	 * @var object
46
	 */
47
	protected $client;
48
49
	/**
50
	 * Cookie jar object for api requests
51
	 * @var object
52
	 */
53
	protected $cookieJar;
54
55
	/**
56
	 * Constructor
57
	 *
58
	 * @param ContainerInterface $container
59
	 */
60
	public function __construct(ContainerInterface $container)
61
	{
62
		parent::__construct($container);
63
		$this->init();
64
	}
65
66
	/**
67
	 * Set up the class properties
68
	 *
69
	 * @return void
70
	 */
71
	protected function init()
72
	{
73
		$this->cookieJar = new CookieJar();
74
		$this->client = new Client([
75
			'base_uri' => $this->base_url,
76
			'cookies' => TRUE,
77
			'http_errors' => FALSE,
78
			'defaults' => [
79
				'cookies' => $this->cookieJar,
80
				'headers' => [
81
					'User-Agent' => "Tim's Anime Client/2.0",
82
					'Accept-Encoding' => 'application/json'
83
				],
84
				'timeout' => 25,
85
				'connect_timeout' => 25
86
			]
87
		]);
88
	}
89
90
	/**
91
	 * Magic methods to call guzzle api client
92
	 *
93
	 * @param  string $method
94
	 * @param  array $args
95
	 * @return ResponseInterface|null
96
	 */
97
	public function __call($method, $args)
98
	{
99
		$valid_methods = [
100
			'get',
101
			'delete',
102
			'head',
103
			'options',
104
			'patch',
105
			'post',
106
			'put'
107
		];
108
109
		if ( ! in_array($method, $valid_methods))
110
		{
111
			return NULL;
112
		}
113
114
		array_unshift($args, strtoupper($method));
115
		return call_user_func_array([$this->client, 'request'], $args);
116
	}
117
118
	/**
119
	 * Attempt login via the api
120
	 *
121
	 * @codeCoverageIgnore
122
	 * @param string $username
123
	 * @param string $password
124
	 * @return string|false
125
	 */
126
	public function authenticate($username, $password)
127
	{
128
		$response = $this->post('https://hummingbird.me/api/v1/users/authenticate', [
129
			'form_params' => [
130
				'username' => $username,
131
				'password' => $password
132
			]
133
		]);
134
135
		if ($response->getStatusCode() === 201)
136
		{
137
			return json_decode($response->getBody(), TRUE);
138
		}
139
140
		return FALSE;
141
	}
142
}
143
// End of BaseApiModel.php