1 | <?php |
||
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 | 16 | public function __construct($apiKey, $projectId = null) |
|
242 | { |
||
243 | 16 | $this->SetApiKey($apiKey); |
|
244 | 16 | $this->SetProjectId($projectId); |
|
245 | 16 | $this->SetHttpClient(); |
|
246 | 16 | $this->_RegisterModules(); |
|
247 | 16 | } |
|
248 | |||
249 | /** |
||
250 | * Get the current API version |
||
251 | * |
||
252 | * @return string $api_version |
||
253 | */ |
||
254 | 8 | public function GetApiVersion() |
|
255 | { |
||
256 | 8 | return $this->_api_version; |
|
257 | } |
||
258 | |||
259 | /** |
||
260 | * Get the API endpoint |
||
261 | * |
||
262 | * @return string |
||
263 | */ |
||
264 | 16 | public function GetApiEndpoint() |
|
265 | { |
||
266 | 16 | 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 | 16 | public function GetApiKey() |
|
289 | { |
||
290 | 16 | return $this->_api_key; |
|
291 | } |
||
292 | |||
293 | /** |
||
294 | * Get the API key |
||
295 | * |
||
296 | * @param string $api_key |
||
297 | * @return Client |
||
298 | */ |
||
299 | 16 | public function SetApiKey($api_key) |
|
300 | { |
||
301 | 16 | $this->_api_key = $api_key; |
|
302 | 16 | $this->SetHttpClient(); |
|
303 | 16 | $this->_RegisterModules(); |
|
304 | 16 | return $this; |
|
305 | } |
||
306 | |||
307 | /** |
||
308 | * Get the API project id |
||
309 | * |
||
310 | * @return string $projectId |
||
311 | */ |
||
312 | 16 | public function GetProjectId() |
|
313 | { |
||
314 | 16 | return $this->_projectId; |
|
315 | } |
||
316 | |||
317 | /** |
||
318 | * Set the API project id |
||
319 | * |
||
320 | * @param string $projectId |
||
321 | * @return Client |
||
322 | */ |
||
323 | 16 | public function SetProjectId($projectId) |
|
330 | |||
331 | /** |
||
332 | * Register Modules |
||
333 | * |
||
334 | * @return void |
||
335 | */ |
||
336 | 16 | private function _RegisterModules() |
|
337 | { |
||
368 | |||
369 | /** |
||
370 | * Setup the HTTP Client |
||
371 | * |
||
372 | * @return Client |
||
373 | */ |
||
374 | 16 | private function SetHttpClient() |
|
383 | |||
384 | /** |
||
385 | * @return HttpClient |
||
386 | */ |
||
387 | 5 | private function GetHttpClient() |
|
391 | |||
392 | /** |
||
393 | * Define the HTTP headers |
||
394 | * |
||
395 | * @return array |
||
396 | */ |
||
397 | 16 | private function _GetHttpClientHeaders() |
|
409 | |||
410 | /** |
||
411 | * @throws \Exception |
||
412 | * @return boolean |
||
413 | */ |
||
414 | 5 | public function CheckApiKey() |
|
421 | |||
422 | /** |
||
423 | * @param $url |
||
424 | * @param null|array $payload |
||
425 | * @return string |
||
426 | */ |
||
427 | 7 | public function GetUrl($url, $payload = []) |
|
428 | { |
||
429 | 7 | $url = $this->GetApiEndpoint()."/".$this->GetApiVersion().$url; |
|
430 | 7 | if (count($payload)) { |
|
431 | $url .= "?".http_build_query($payload); |
||
432 | } |
||
433 | 7 | 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 | 6 | public function BuildRequest($method, $url, $payload = []) |
|
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 | 5 | public function SendRequest($method, $url, $payload = []) |
|
466 | { |
||
467 | 5 | $this->CheckApiKey(); |
|
468 | 5 | $request = $this->BuildRequest($method, $url, $payload); |
|
469 | try { |
||
470 | 5 | $response = $this->GetHttpClient()->send($request); |
|
471 | 5 | } 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 | } catch (\Exception $e) { |
||
481 | throw $e; |
||
482 | } |
||
483 | $body = json_decode($response->getBody(), true); |
||
484 | if (json_last_error() !== JSON_ERROR_NONE) { |
||
485 | throw new \Exception(json_last_error_msg()); |
||
486 | } |
||
487 | return $body; |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * @param string $url |
||
492 | * @param array $payload |
||
493 | * @return mixed |
||
494 | */ |
||
495 | 1 | public function Post($url, $payload = []) |
|
499 | |||
500 | /** |
||
501 | * @param string $url |
||
502 | * @param array $payload |
||
503 | * @return mixed |
||
504 | */ |
||
505 | 1 | public function Get($url, $payload = []) |
|
509 | |||
510 | /** |
||
511 | * @param string $url |
||
512 | * @param array $payload |
||
513 | * @return mixed |
||
514 | */ |
||
515 | 1 | public function Put($url, $payload = []) |
|
519 | |||
520 | /** |
||
521 | * @param string $url |
||
522 | * @param array $payload |
||
523 | * @return mixed |
||
524 | */ |
||
525 | 1 | public function Delete($url, $payload = []) |
|
529 | } |
||
530 |