1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
<?php
namespace APWG\API;
use APWG\API\ClientInterface as APIClientInterface;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use Psr\Http\Message\ResponseInterface;
/**
* The abstract client interface implementation
*
* Class AbstractClient
* @package APWG\API
* @author Andrew Breksa <andrew@apwg.org>
* @copyright Copyright (c) 2017 The Anti-Phishing Working Group
*/
abstract class AbstractClient implements APIClientInterface {
/**
* The HTTP Client (GuzzleHTTP)
*
* @var ClientInterface
*/
protected $client;
/**
* The API Key
*
* @var string
*/
protected $apiKey;
/**
* The options to be merged into every request
*
* @var array
*/
protected $globalOptions = [];
/**
* The API base URI
*
* @var string
*/
protected $base_uri;
/**
* The default GuzzleHTTP options
*
* @var array
*/
protected $guzzleOptions = [
'timeout' => 5,
'curl' => [
CURLOPT_TIMEOUT => 0,
CURLOPT_TIMEOUT_MS => 0,
CURLOPT_CONNECTTIMEOUT => 0,
],
];
/**
* The request cache, the most recent request
*
* @var ResponseInterface
*/
protected $cache;
/**
* AbstractAPI constructor.
*
* @param string $apiKey access token
* @param string $base_uri the base uri of the api
* @param array $guzzleOptions guzzle http options, overrides defaults
*/
public function __construct($base_uri, $apiKey, $guzzleOptions = []) {
$this->setBaseUri($base_uri);
$this->setApiKey($apiKey);
$this->setGuzzleOptions($guzzleOptions);
$this->setClient(new Client(array_merge($this->getGuzzleOptions(), ['base_uri' => $this->getBaseUri()])));
}
/**
* Get the default GuzzleHTTP options
*
* @return array
*/
public function getGuzzleOptions() {
return $this->guzzleOptions;
}
/**
* Set the default GuzzleHTTP options
*
* @param array $guzzleOptions
*
* @return AbstractClient
*/
public function setGuzzleOptions($guzzleOptions) {
if( ! empty($guzzleOptions)) {
$this->guzzleOptions = array_replace($this->getGuzzleOptions(), $guzzleOptions);
}
return $this;
}
/**
* Get the API base URI
*
* @return string
*/
public function getBaseUri() {
return $this->base_uri;
}
/**
* Set the base API URI
*
* @param string $base_uri
*
* @return AbstractClient
*/
public function setBaseUri($base_uri) {
$this->base_uri = $base_uri;
return $this;
}
/**
* Internal method to call the API
*
* @param $method
* @param $path
* @param array $options
*
* @return ResponseInterface
*/
public function _call($method, $path, $options = []) {
$this->setCache($this->getClient()->request(
$method,
$path,
array_merge_recursive(
$options,
[
'headers' => [
'Authorization' => $this->getApiKey(),
],
],
(array_key_exists('headers', $options) || array_key_exists('multipart',
$options)) ? [] : ['headers' => ['Content-Type' => 'application/json']],
$this->getGlobalOptions()
)
));
return $this->cache;
}
/**
* Get the HTTP Client
*
* @return ClientInterface
*/
public function getClient() {
return $this->client;
}
/**
* Set the HTTP Client
*
* @param ClientInterface $client
*
* @return AbstractClient
*/
public function setClient($client) {
$this->client = $client;
return $this;
}
/**
* Return the API Key
*
* @return string
*/
public function getApiKey() {
return $this->apiKey;
}
/**
* Set the API key
*
* @param string $apiKey
*
* @return AbstractClient
*/
public function setApiKey($apiKey) {
$this->apiKey = $apiKey;
return $this;
}
/**
* Get the global options that are merged with every request
*
* @return array
*/
public function getGlobalOptions() {
return $this->globalOptions;
}
/**
* Set the global options that are merged with every request
*
* @param array $globalOptions
*
* @return AbstractClient
*/
public function setGlobalOptions($globalOptions) {
$this->globalOptions = $globalOptions;
return $this;
}
/**
* Get the most recent request
*
* @return ResponseInterface
*/
public function getCache() {
return $this->cache;
}
/**
* Set the most recent request
*
* @param ResponseInterface $cache
*/
public function setCache($cache) {
$this->cache = $cache;
}
}