1
|
|
|
<?php |
2
|
|
|
namespace Datatrics\API; |
3
|
|
|
|
4
|
|
|
use GuzzleHttp\Client as HttpClient; |
5
|
|
|
use Datatrics\API\Modules\Apikey; |
6
|
|
|
use Datatrics\API\Modules\Behavior; |
7
|
|
|
use Datatrics\API\Modules\Billing; |
8
|
|
|
use Datatrics\API\Modules\Box; |
9
|
|
|
use Datatrics\API\Modules\Bucket; |
10
|
|
|
use Datatrics\API\Modules\Campaign; |
11
|
|
|
use Datatrics\API\Modules\Card; |
12
|
|
|
use Datatrics\API\Modules\Channel; |
13
|
|
|
use Datatrics\API\Modules\Content; |
14
|
|
|
use Datatrics\API\Modules\Geo; |
15
|
|
|
use Datatrics\API\Modules\Goal; |
16
|
|
|
use Datatrics\API\Modules\Interaction; |
17
|
|
|
use Datatrics\API\Modules\Journey; |
18
|
|
|
use Datatrics\API\Modules\Link; |
19
|
|
|
use Datatrics\API\Modules\NextBestAction; |
20
|
|
|
use Datatrics\API\Modules\Profile; |
21
|
|
|
use Datatrics\API\Modules\Project; |
22
|
|
|
use Datatrics\API\Modules\Sale; |
23
|
|
|
use Datatrics\API\Modules\Scorecard; |
24
|
|
|
use Datatrics\API\Modules\Segment; |
25
|
|
|
use Datatrics\API\Modules\Subscription; |
26
|
|
|
use Datatrics\API\Modules\Template; |
27
|
|
|
use Datatrics\API\Modules\Theme; |
28
|
|
|
use Datatrics\API\Modules\Touchpoint; |
29
|
|
|
use Datatrics\API\Modules\Tracker; |
30
|
|
|
use Datatrics\API\Modules\Tric; |
31
|
|
|
use Datatrics\API\Modules\Trigger; |
32
|
|
|
use Datatrics\API\Modules\User; |
33
|
|
|
use Datatrics\API\Modules\Webhook; |
34
|
|
|
use GuzzleHttp\Psr7\Request; |
35
|
|
|
|
36
|
|
|
class Client |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @const Version of our client. |
40
|
|
|
*/ |
41
|
|
|
const CLIENT_VERSION = '2.0'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @const HTTP Method GET |
45
|
|
|
*/ |
46
|
|
|
const HTTP_GET = 'GET'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @const HTTP Method POST |
50
|
|
|
*/ |
51
|
|
|
const HTTP_POST = 'POST'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @const HTTP Method PUT |
55
|
|
|
*/ |
56
|
|
|
const HTTP_PUT = 'PUT'; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @const HTTP Method DELETE |
60
|
|
|
*/ |
61
|
|
|
const HTTP_DELETE = 'DELETE'; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Version of the remote API. |
65
|
|
|
* |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
private $_api_version = '2.0'; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
private $_api_endpoint = 'https://api.datatrics.com'; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var string |
77
|
|
|
*/ |
78
|
|
|
private $_api_key; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var HttpClient |
82
|
|
|
*/ |
83
|
|
|
private $_http_client; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var string |
87
|
|
|
*/ |
88
|
|
|
private $_projectId; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var Apikey |
92
|
|
|
*/ |
93
|
|
|
public $Apikey; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @var Behavior |
97
|
|
|
*/ |
98
|
|
|
public $Behavior; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @var Billing |
102
|
|
|
*/ |
103
|
|
|
public $Billing; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @var Box |
107
|
|
|
*/ |
108
|
|
|
public $Box; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @var Bucket |
112
|
|
|
*/ |
113
|
|
|
public $Bucket; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @var Campaign |
117
|
|
|
*/ |
118
|
|
|
public $Campaign; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @var Card |
122
|
|
|
*/ |
123
|
|
|
public $Card; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @var Channel |
127
|
|
|
*/ |
128
|
|
|
public $Channel; |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @var Content |
132
|
|
|
*/ |
133
|
|
|
public $Content; |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @var Geo |
137
|
|
|
*/ |
138
|
|
|
public $Geo; |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @var Goal |
142
|
|
|
*/ |
143
|
|
|
public $Goal; |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @var Interaction |
147
|
|
|
*/ |
148
|
|
|
public $Interaction; |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @var Journey |
152
|
|
|
*/ |
153
|
|
|
public $Journey; |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @var Link |
157
|
|
|
*/ |
158
|
|
|
public $Link; |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @var NextBestAction |
162
|
|
|
*/ |
163
|
|
|
public $NextBestAction; |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @var Profile |
167
|
|
|
*/ |
168
|
|
|
public $Profile; |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @var Project |
172
|
|
|
*/ |
173
|
|
|
public $Project; |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @var Sale |
177
|
|
|
*/ |
178
|
|
|
public $Sale; |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @var Scorecard |
182
|
|
|
*/ |
183
|
|
|
public $Scorecard; |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @var Segment |
187
|
|
|
*/ |
188
|
|
|
public $Segment; |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @var Subscription |
192
|
|
|
*/ |
193
|
|
|
public $Subscription; |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @var Template |
197
|
|
|
*/ |
198
|
|
|
public $Template; |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @var Theme |
202
|
|
|
*/ |
203
|
|
|
public $Theme; |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @var Touchpoint |
207
|
|
|
*/ |
208
|
|
|
public $Touchpoint; |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @var Tracker |
212
|
|
|
*/ |
213
|
|
|
public $Tracker; |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @var Tric |
217
|
|
|
*/ |
218
|
|
|
public $Tric; |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @var Trigger |
222
|
|
|
*/ |
223
|
|
|
public $Trigger; |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @var User |
227
|
|
|
*/ |
228
|
|
|
public $User; |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @var Webhook |
232
|
|
|
*/ |
233
|
|
|
public $Webhook; |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Create a new API instance |
237
|
|
|
* |
238
|
|
|
* @param string $apiKey The API key |
239
|
|
|
* @param string $projectId The Project id |
240
|
|
|
*/ |
241
|
20 |
|
public function __construct($apiKey, $projectId = null) |
242
|
|
|
{ |
243
|
20 |
|
$this->SetApiKey($apiKey); |
244
|
20 |
|
$this->SetProjectId($projectId); |
245
|
20 |
|
$this->SetHttpClient(); |
246
|
20 |
|
$this->_RegisterModules(); |
247
|
20 |
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Get the current API version |
251
|
|
|
* |
252
|
|
|
* @return string $api_version |
253
|
|
|
*/ |
254
|
12 |
|
public function GetApiVersion() |
255
|
|
|
{ |
256
|
12 |
|
return $this->_api_version; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Get the API endpoint |
261
|
|
|
* |
262
|
|
|
* @return string |
263
|
|
|
*/ |
264
|
20 |
|
public function GetApiEndpoint() |
265
|
|
|
{ |
266
|
20 |
|
return $this->_api_endpoint; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Set the API endpoint |
271
|
|
|
* |
272
|
|
|
* @param string $api_endpoint |
273
|
|
|
* @return Client |
274
|
|
|
*/ |
275
|
1 |
|
public function SetApiEndpoint($api_endpoint) |
276
|
|
|
{ |
277
|
1 |
|
$this->_api_endpoint = $api_endpoint; |
278
|
1 |
|
$this->SetHttpClient(); |
279
|
1 |
|
$this->_RegisterModules(); |
280
|
1 |
|
return $this; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Get the API key |
285
|
|
|
* |
286
|
|
|
* @return string $api_key |
287
|
|
|
*/ |
288
|
20 |
|
public function GetApiKey() |
289
|
|
|
{ |
290
|
20 |
|
return $this->_api_key; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Get the API key |
295
|
|
|
* |
296
|
|
|
* @param string $api_key |
297
|
|
|
* @return Client |
298
|
|
|
*/ |
299
|
20 |
|
public function SetApiKey($api_key) |
300
|
|
|
{ |
301
|
20 |
|
$this->_api_key = $api_key; |
302
|
20 |
|
$this->SetHttpClient(); |
303
|
20 |
|
$this->_RegisterModules(); |
304
|
20 |
|
return $this; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Get the API project id |
309
|
|
|
* |
310
|
|
|
* @return string $projectId |
311
|
|
|
*/ |
312
|
20 |
|
public function GetProjectId() |
313
|
|
|
{ |
314
|
20 |
|
return $this->_projectId; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Set the API project id |
319
|
|
|
* |
320
|
|
|
* @param string $projectId |
321
|
|
|
* @return Client |
322
|
|
|
*/ |
323
|
20 |
|
public function SetProjectId($projectId) |
324
|
|
|
{ |
325
|
20 |
|
$this->_projectId = $projectId; |
326
|
20 |
|
$this->SetHttpClient(); |
327
|
20 |
|
$this->_RegisterModules(); |
328
|
20 |
|
return $this; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Register Modules |
333
|
|
|
* |
334
|
|
|
* @return void |
335
|
|
|
*/ |
336
|
20 |
|
private function _RegisterModules() |
337
|
|
|
{ |
338
|
20 |
|
$this->Apikey = new Apikey($this); |
339
|
20 |
|
$this->Behavior = new Behavior($this); |
340
|
20 |
|
$this->Billing = new Billing($this); |
341
|
20 |
|
$this->Box = new Box($this); |
342
|
20 |
|
$this->Bucket = new Bucket($this); |
343
|
20 |
|
$this->Campaign = new Campaign($this); |
344
|
20 |
|
$this->Card = new Card($this); |
345
|
20 |
|
$this->Channel = new Channel($this); |
346
|
20 |
|
$this->Content = new Content($this); |
347
|
20 |
|
$this->Geo = new Geo($this); |
348
|
20 |
|
$this->Goal = new Goal($this); |
349
|
20 |
|
$this->Interaction = new Interaction($this); |
350
|
20 |
|
$this->Journey = new Journey($this); |
351
|
20 |
|
$this->Link = new Link($this); |
352
|
20 |
|
$this->NextBestAction = new NextBestAction($this); |
353
|
20 |
|
$this->Profile = new Profile($this); |
354
|
20 |
|
$this->Project = new Project($this); |
355
|
20 |
|
$this->Sale = new Sale($this); |
356
|
20 |
|
$this->Scorecard = new Scorecard($this); |
357
|
20 |
|
$this->Segment = new Segment($this); |
358
|
20 |
|
$this->Subscription = new Subscription($this); |
359
|
20 |
|
$this->Template = new Template($this); |
360
|
20 |
|
$this->Theme = new Theme($this); |
361
|
20 |
|
$this->Touchpoint = new Touchpoint($this); |
362
|
20 |
|
$this->Tracker = new Tracker($this); |
363
|
20 |
|
$this->Tric = new Tric($this); |
364
|
20 |
|
$this->Trigger = new Trigger($this); |
365
|
20 |
|
$this->User = new User($this); |
366
|
20 |
|
$this->Webhook = new Webhook($this); |
367
|
20 |
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Setup the HTTP Client |
371
|
|
|
* |
372
|
|
|
* @return Client |
373
|
|
|
*/ |
374
|
20 |
|
private function SetHttpClient() |
375
|
|
|
{ |
376
|
|
|
$config = [ |
377
|
20 |
|
'base-uri' => $this->GetApiEndpoint(), |
378
|
20 |
|
'headers' => $this->_GetHttpClientHeaders() |
379
|
|
|
]; |
380
|
20 |
|
$this->_http_client = new HttpClient($config); |
381
|
20 |
|
return $this; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* @return HttpClient |
386
|
|
|
*/ |
387
|
9 |
|
private function GetHttpClient() |
388
|
|
|
{ |
389
|
9 |
|
return $this->_http_client; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* Define the HTTP headers |
394
|
|
|
* |
395
|
|
|
* @return array |
396
|
|
|
*/ |
397
|
20 |
|
private function _GetHttpClientHeaders() |
398
|
|
|
{ |
399
|
20 |
|
$user_agent = 'Datatrics/API '.self::CLIENT_VERSION; |
400
|
|
|
return [ |
401
|
20 |
|
'accept' => 'application/json', |
402
|
20 |
|
'content-type' => 'application/json', |
403
|
20 |
|
'user-agent' => $user_agent, |
404
|
20 |
|
'x-apikey' => $this->GetApiKey(), |
405
|
20 |
|
'x-client-name' => $user_agent, |
406
|
20 |
|
'x-datatrics-client-info' => php_uname() |
407
|
|
|
]; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* @throws \Exception |
412
|
|
|
* @return boolean |
413
|
|
|
*/ |
414
|
9 |
|
public function CheckApiKey() |
415
|
|
|
{ |
416
|
9 |
|
if (empty($this->GetApiKey())) { |
417
|
|
|
throw new \Exception('You have not set an api key. Please use setApiKey() to set the API key.'); |
418
|
|
|
} |
419
|
9 |
|
return true; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* @param $url |
424
|
|
|
* @param null|array $payload |
425
|
|
|
* @return string |
426
|
|
|
*/ |
427
|
11 |
|
public function GetUrl($url, $payload = []) |
428
|
|
|
{ |
429
|
11 |
|
$url = $this->GetApiEndpoint()."/".$this->GetApiVersion().$url; |
430
|
11 |
|
if (count($payload)) { |
431
|
2 |
|
$url .= "?".http_build_query($payload); |
432
|
|
|
} |
433
|
11 |
|
return $url; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* @param string $method HTTP Method |
438
|
|
|
* @param string $url The url |
439
|
|
|
* @param array $payload The Payload |
440
|
|
|
* @return Request |
441
|
|
|
*/ |
442
|
10 |
|
public function BuildRequest($method, $url, $payload = []) |
443
|
|
|
{ |
444
|
10 |
|
$body = null; |
445
|
10 |
|
if ($method == self::HTTP_GET) { |
446
|
5 |
|
$url = $this->GetUrl($url, $payload); |
447
|
5 |
|
} elseif ($method == self::HTTP_DELETE) { |
448
|
1 |
|
$url = $this->GetUrl($url, $payload); |
449
|
|
|
} else { |
450
|
4 |
|
$url = $this->GetUrl($url); |
451
|
4 |
|
if (count($payload)) { |
452
|
|
|
$body = json_encode($payload); |
453
|
|
|
} |
454
|
|
|
} |
455
|
10 |
|
return new Request($method, $url, $this->_GetHttpClientHeaders(), $body); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* @param string $method HTTP Method |
460
|
|
|
* @param string $url The url |
461
|
|
|
* @param array $payload The Payload |
462
|
|
|
* @return mixed |
463
|
|
|
* @throws \Exception |
464
|
|
|
*/ |
465
|
9 |
|
public function SendRequest($method, $url, $payload = []) |
466
|
|
|
{ |
467
|
9 |
|
$this->CheckApiKey(); |
468
|
9 |
|
$request = $this->BuildRequest($method, $url, $payload); |
469
|
|
|
try { |
470
|
9 |
|
$response = $this->GetHttpClient()->send($request); |
471
|
7 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
472
|
5 |
|
if ($e->hasResponse()) { |
473
|
5 |
|
$body = json_decode($e->getResponse()->getBody(), true); |
474
|
5 |
|
if (isset($body['error']['message'])) { |
475
|
|
|
throw new \Exception($body['error']['message'], $e->getResponse()->getStatusCode()); |
476
|
|
|
} |
477
|
5 |
|
throw new \Exception($body['message'], $e->getResponse()->getStatusCode()); |
478
|
|
|
} |
479
|
|
|
throw $e; |
480
|
2 |
|
} catch (\Exception $e) { |
481
|
2 |
|
throw $e; |
482
|
|
|
} |
483
|
2 |
|
$body = json_decode($response->getBody(), true); |
484
|
2 |
|
if (json_last_error() !== JSON_ERROR_NONE) { |
485
|
|
|
throw new \Exception(json_last_error_msg()); |
486
|
|
|
} |
487
|
2 |
|
return $body; |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
/** |
491
|
|
|
* @param string $url |
492
|
|
|
* @param array $payload |
493
|
|
|
* @return mixed |
494
|
|
|
*/ |
495
|
3 |
|
public function Post($url, $payload = []) |
496
|
|
|
{ |
497
|
3 |
|
return $this->SendRequest(self::HTTP_POST, $url, $payload); |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* @param string $url |
502
|
|
|
* @param array $payload |
503
|
|
|
* @return mixed |
504
|
|
|
*/ |
505
|
3 |
|
public function Get($url, $payload = []) |
506
|
|
|
{ |
507
|
3 |
|
return $this->SendRequest(self::HTTP_GET, $url, $payload); |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
/** |
511
|
|
|
* @param string $url |
512
|
|
|
* @param array $payload |
513
|
|
|
* @return mixed |
514
|
|
|
*/ |
515
|
1 |
|
public function Put($url, $payload = []) |
516
|
|
|
{ |
517
|
1 |
|
return $this->SendRequest(self::HTTP_PUT, $url, $payload); |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
/** |
521
|
|
|
* @param string $url |
522
|
|
|
* @param array $payload |
523
|
|
|
* @return mixed |
524
|
|
|
*/ |
525
|
1 |
|
public function Delete($url, $payload = []) |
526
|
|
|
{ |
527
|
1 |
|
return $this->SendRequest(self::HTTP_DELETE, $url, $payload); |
528
|
|
|
} |
529
|
|
|
} |
530
|
|
|
|