|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Jaeger - REST Client |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2016, mithra62, Eric Lamb. |
|
6
|
|
|
* @link http://jaegerapp.net/ |
|
7
|
|
|
* @version 1.0 |
|
8
|
|
|
* @filesource ./jaeger-app/Rest/Client.php |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace JaegerApp\Rest; |
|
12
|
|
|
|
|
13
|
|
|
use PhilipBrown\Signature\Token; |
|
14
|
|
|
use PhilipBrown\Signature\Request; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Rest Client Object |
|
18
|
|
|
* |
|
19
|
|
|
* Simple object to interact with a Jaeger installation |
|
20
|
|
|
* |
|
21
|
|
|
* @package Rest\Client |
|
22
|
|
|
* @author Eric Lamb <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class Client |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* The configuration details for connection |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $config = array(); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The API Key to use |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $api_key = null; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The API Secret to use |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $api_secret = null; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The URL to the Backup Pro API endpoint |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $site_url = null; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* The debug information |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $debug_info = array(); |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* The Curl handle |
|
58
|
|
|
* @var resource |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $curl_handle = null; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* The HTTP verb names |
|
64
|
|
|
* @var string |
|
65
|
|
|
*/ |
|
66
|
|
|
const HTTP_METHOD_GET = 'GET'; |
|
67
|
|
|
const HTTP_METHOD_POST = 'POST'; |
|
68
|
|
|
const HTTP_METHOD_PUT = 'PUT'; |
|
69
|
|
|
const HTTP_METHOD_DELETE = 'DELETE'; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Sets it up |
|
73
|
|
|
* @param array $config |
|
74
|
|
|
*/ |
|
75
|
|
|
public function __construct(array $config = array()) |
|
76
|
|
|
{ |
|
77
|
|
|
if(isset($config['api_key'])) { |
|
78
|
|
|
$this->api_key = $config['api_key']; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if(isset($config['api_secret'])) { |
|
82
|
|
|
$this->api_secret = $config['api_secret']; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if(isset($config['site_url'])) { |
|
86
|
|
|
$this->site_url = $config['site_url']; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->config = $config; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Returns the config |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getConfig() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->config; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Sets the API key to use for authentication |
|
103
|
|
|
* @param string $key |
|
104
|
|
|
* @return \JargerApp\Rest\Client |
|
105
|
|
|
*/ |
|
106
|
|
|
public function setApiKey($key) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->api_key = $key; |
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Returns the API key |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getApiKey() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->api_key; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Sets the API secret to use for authentication |
|
123
|
|
|
* @param string $secret |
|
124
|
|
|
* @return \JargerApp\Rest\Client |
|
125
|
|
|
*/ |
|
126
|
|
|
public function setApiSecret($secret) |
|
127
|
|
|
{ |
|
128
|
|
|
$this->api_secret = $secret; |
|
129
|
|
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Returns the API secret |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getApiSecret() |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->api_secret; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Sets the Backup Pro REST API site URL |
|
143
|
|
|
* @param string $site_url |
|
144
|
|
|
* @return \JargerApp\Rest\Client |
|
145
|
|
|
*/ |
|
146
|
|
|
public function setSiteUrl($site_url) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->site_url = $site_url; |
|
149
|
|
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Returns the Backup Pro REST API site URL |
|
154
|
|
|
* @param string $endpoint |
|
155
|
|
|
* @return string |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getSiteUrl($endpoint = '', array $query = array()) |
|
158
|
|
|
{ |
|
159
|
|
|
if(count($query) != '0') { |
|
160
|
|
|
$endpoint .= '&'.http_build_query($query); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $this->site_url.$endpoint; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Send a POST request |
|
168
|
|
|
* @param string $endpoint The API endpoint |
|
169
|
|
|
* @param array $payload Data to submit |
|
170
|
|
|
*/ |
|
171
|
|
|
public function post($endpoint, array $payload = array()) |
|
172
|
|
|
{ |
|
173
|
|
|
return $this->fetch($endpoint, $payload, self::HTTP_METHOD_POST); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Sends a GET request |
|
178
|
|
|
* @param string $endpoint The API endpoint |
|
179
|
|
|
* @param array $payload |
|
180
|
|
|
*/ |
|
181
|
|
|
public function get($endpoint, array $payload = array()) |
|
182
|
|
|
{ |
|
183
|
|
|
return $this->fetch($endpoint, $payload); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* PUT to an authenciated API endpoint w/ payload |
|
188
|
|
|
* |
|
189
|
|
|
* @param string $endpoint |
|
190
|
|
|
* @param array $payload |
|
191
|
|
|
* @return array |
|
192
|
|
|
*/ |
|
193
|
|
|
public function put($endpoint, array $payload = array()) |
|
194
|
|
|
{ |
|
195
|
|
|
return $this->fetch($endpoint, $payload, self::HTTP_METHOD_PUT); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Performs a DELETE request |
|
200
|
|
|
* @param string $endpoint |
|
201
|
|
|
* @param array $payload |
|
202
|
|
|
* @return bool |
|
203
|
|
|
*/ |
|
204
|
|
|
public function delete($endpoint, array $payload = array()) |
|
205
|
|
|
{ |
|
206
|
|
|
return $this->fetch($endpoint, $payload, self::HTTP_METHOD_DELETE); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Sets up the Hmac authentication headers and dispatches the request |
|
211
|
|
|
* @param string $endpoint The API endpoing we want |
|
212
|
|
|
* @param array $payload Any data to send along |
|
213
|
|
|
* @param string $method The HTTP method |
|
214
|
|
|
* @return bool |
|
215
|
|
|
*/ |
|
216
|
|
|
public function fetch($endpoint, array $payload = array(), $method = 'GET') |
|
217
|
|
|
{ |
|
218
|
|
|
//so we don't want to use the query params for HMAC since we won't know what's |
|
219
|
|
|
//being sent versus what's expected to be sent. So we remove them and prepare for |
|
220
|
|
|
//query usage |
|
221
|
|
|
$query = array(); |
|
222
|
|
|
if(strtolower($method)== 'get') { |
|
223
|
|
|
$query = $payload; |
|
224
|
|
|
$payload = array(); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
$token = new Token($this->getApiKey(), $this->getApiSecret()); |
|
228
|
|
|
$request = new Request($method, $endpoint, $payload); |
|
229
|
|
|
$headers = $request->sign($token, 'm62_auth_'); |
|
230
|
|
|
|
|
231
|
|
|
if(strtolower($method)== 'get') { |
|
232
|
|
|
$payload = array(); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
$endpoint = $this->getSiteUrl($endpoint, $query); |
|
236
|
|
|
return $this->request($endpoint, $payload, $method, $headers); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Returns the debug data |
|
241
|
|
|
* @return array |
|
242
|
|
|
*/ |
|
243
|
|
|
public function getDebugInfo() |
|
244
|
|
|
{ |
|
245
|
|
|
return $this->debug_info; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Make a CURL request |
|
250
|
|
|
* |
|
251
|
|
|
* @param string $url |
|
252
|
|
|
* @param array $payload |
|
253
|
|
|
* @param string $method |
|
254
|
|
|
* @param array $headers |
|
255
|
|
|
* @param array $curl_options |
|
256
|
|
|
* @throws \RuntimeException |
|
257
|
|
|
* @return array |
|
258
|
|
|
*/ |
|
259
|
|
|
protected function request($url, array $payload = array(), $method = 'GET', array $headers = array(), array $curl_options = array()) |
|
260
|
|
|
{ |
|
261
|
|
|
$ch = $this->getCurlHandle(); |
|
262
|
|
|
$parsed_headers = array(); |
|
263
|
|
|
foreach($headers AS $key => $value) { |
|
264
|
|
|
$parsed_headers[] = $key.': '.$value; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
$options = array( |
|
268
|
|
|
CURLOPT_CUSTOMREQUEST => strtoupper($method), |
|
269
|
|
|
CURLOPT_RETURNTRANSFER => true, |
|
270
|
|
|
CURLOPT_URL => $url, |
|
271
|
|
|
CURLOPT_HTTPHEADER => $parsed_headers, |
|
272
|
|
|
CURLOPT_SSL_VERIFYPEER => false, |
|
273
|
|
|
CURLOPT_FOLLOWLOCATION => true |
|
274
|
|
|
); |
|
275
|
|
|
if (!empty($payload)) { |
|
276
|
|
|
if ($options[CURLOPT_CUSTOMREQUEST] == self::HTTP_METHOD_POST || |
|
277
|
|
|
$options[CURLOPT_CUSTOMREQUEST] == self::HTTP_METHOD_PUT || |
|
278
|
|
|
$options[CURLOPT_CUSTOMREQUEST] == self::HTTP_METHOD_DELETE) { |
|
279
|
|
|
$json_payload = json_encode($payload); |
|
280
|
|
|
|
|
281
|
|
|
$options[CURLOPT_POSTFIELDS] = $json_payload; |
|
282
|
|
|
$parsed_headers[] = 'Content-Length: ' . strlen($json_payload); |
|
283
|
|
|
$parsed_headers[] = 'Content-Type: application/json'; |
|
284
|
|
|
$options[CURLOPT_HTTPHEADER] = $parsed_headers; |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
if (!empty($curl_options)) { |
|
288
|
|
|
$options = array_replace($options, $curl_options); |
|
289
|
|
|
} |
|
290
|
|
|
if (isset($this->config['curl_options']) && !empty($this->config['curl_options'])) { |
|
291
|
|
|
$options = array_replace($options, $this->config['curl_options']); |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
curl_setopt_array($ch, $options); |
|
295
|
|
|
$response_raw = curl_exec($ch); |
|
296
|
|
|
$this->debug_info = curl_getinfo($ch); |
|
297
|
|
|
|
|
298
|
|
|
if ($response_raw === false) { |
|
299
|
|
|
throw new \RuntimeException('Request Error: ' . curl_error($ch)); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
303
|
|
|
$response = json_decode($response_raw, true); |
|
304
|
|
|
if (isset($response['status']) && ($response['status'] < 200 || $response['status'] > 300)) { |
|
305
|
|
|
return Client\ApiProblem::fromJson($response_raw); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
return Client\Hal::fromJson($response_raw, 3); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
|
|
312
|
|
|
protected function getCurlHandle() |
|
313
|
|
|
{ |
|
314
|
|
|
if (!$this->curl_handle) { |
|
315
|
|
|
$this->curl_handle = curl_init(); |
|
316
|
|
|
} |
|
317
|
|
|
return $this->curl_handle; |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
public function __destruct() |
|
321
|
|
|
{ |
|
322
|
|
|
if ($this->curl_handle) { |
|
323
|
|
|
curl_close($this->curl_handle); |
|
324
|
|
|
} |
|
325
|
|
|
} |
|
326
|
|
|
} |