-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClient.php
More file actions
134 lines (115 loc) · 3.14 KB
/
Client.php
File metadata and controls
134 lines (115 loc) · 3.14 KB
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
<?php
namespace ThisData\Api;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\RequestInterface;
/**
* An extension of the Guzzle HTTP client that lets you communicate with the
* ThisData REST API.
*/
class Client extends GuzzleClient
{
const API_PROTOCOL = 'https';
const API_HOST = 'api.thisdata.com';
const PARAM_API_KEY = 'api_key';
const HEADER_USER_AGENT = 'com.thisdata.api/php';
const HEADER_CONTENT_TYPE = 'application/json';
/**
* @var string
*/
private $apiKey;
/**
* @var int
*/
private $version;
/**
* @param string $apiKey The API key for your ThisData account
* @param int $version The version of the ThisData API to use
* @param array $options Extra options for the GuzzleClient
*/
public function __construct($apiKey, $version = 1, array $options = [])
{
$this->apiKey = $apiKey;
$this->version = $version;
$options = array_merge($this->getDefaultConfiguration(), $options);
parent::__construct($options);
}
/**
* Get the configuration for the Guzzle HTTP client.
*
* @return array
*/
protected function getDefaultConfiguration()
{
return [
'handler' => $this->getHandlerStack(),
'base_uri' => $this->getBaseUri(),
'timeout' => 2,
'headers' => [
'User-Agent' => self::HEADER_USER_AGENT,
'Content-Type' => self::HEADER_CONTENT_TYPE,
]
];
}
/**
* @return null|callable
*/
protected function getHandler()
{
return null;
}
/**
* Return the stack of handlers and middlewares responsible for processing
* requests.
*
* @return HandlerStack
*/
protected function getHandlerStack()
{
$handlerStack = HandlerStack::create($this->getHandler());
$this->configureHandlerStack($handlerStack);
return $handlerStack;
}
/**
* Add any middleware required.
*
* @param HandlerStack $handlerStack
*/
protected function configureHandlerStack(HandlerStack $handlerStack)
{
$handlerStack->unshift($this->getApiKeyMiddleware());
}
/**
* Return the Guzzle Middleware responsible for ensuring the API key is
* always present in a request.
*
* @return callable
*/
protected function getApiKeyMiddleware()
{
$handleRequest = function (RequestInterface $request) {
return $request->withUri(Uri::withQueryValue(
$request->getUri(),
static::PARAM_API_KEY,
$this->apiKey
));
};
return Middleware::mapRequest($handleRequest);
}
/**
* Return the common URI upon which all other endpoints are called.
*
* @return string
*/
protected function getBaseUri()
{
return sprintf(
'%s://%s/v%s/',
static::API_PROTOCOL,
static::API_HOST,
$this->version
);
}
}