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