forked from Bandwidth/php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPIController.php
More file actions
615 lines (501 loc) · 23.9 KB
/
APIController.php
File metadata and controls
615 lines (501 loc) · 23.9 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
<?php
/*
* BandwidthLib
*
* This file was automatically generated by APIMATIC v3.0 ( https://www.apimatic.io ).
*/
namespace BandwidthLib\Messaging\Controllers;
use BandwidthLib\APIException;
use BandwidthLib\APIHelper;
use BandwidthLib\Messaging\Exceptions;
use BandwidthLib\Messaging\Models;
use BandwidthLib\Controllers\BaseController;
use BandwidthLib\Http\ApiResponse;
use BandwidthLib\Http\HttpRequest;
use BandwidthLib\Http\HttpResponse;
use BandwidthLib\Http\HttpMethod;
use BandwidthLib\Http\HttpContext;
use BandwidthLib\Servers;
use Unirest\Request;
/**
* @todo Add a general description for this controller.
*/
class APIController extends BaseController
{
public function __construct($config, $httpCallBack = null)
{
parent::__construct($config, $httpCallBack);
}
/**
* Gets a list of your media files. No query parameters are supported.
*
* @param string $accountId User's account ID
* @param string|null $continuationToken (optional) Continuation token used to retrieve subsequent media.
* @return ApiResponse response from the API call
* @throws APIException Thrown if API call fails
*/
public function listMedia(
string $accountId,
?string $continuationToken = null
) {
//prepare query string for API call
$_queryBuilder = '/users/{accountId}/media';
//process optional query parameters
$_queryBuilder = APIHelper::appendUrlWithTemplateParameters($_queryBuilder, array (
'accountId' => $accountId,
));
//validate and preprocess url
$_queryUrl = APIHelper::cleanUrl($this->config->getBaseUri(Servers::MESSAGINGDEFAULT) . $_queryBuilder);
//prepare headers
$_headers = array (
'user-agent' => BaseController::USER_AGENT,
'Accept' => 'application/json',
'Continuation-Token' => $continuationToken
);
//set HTTP basic auth parameters
Request::auth($this->config->getMessagingBasicAuthUserName(), $this->config->getMessagingBasicAuthPassword());
$_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl);
//call on-before Http callback
if ($this->getHttpCallBack() != null) {
$this->getHttpCallBack()->callOnBeforeRequest($_httpRequest);
}
// Set request timeout
Request::timeout($this->config->getTimeout());
// and invoke the API call request to fetch the response
$response = Request::get($_queryUrl, $_headers);
$_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body);
$_httpContext = new HttpContext($_httpRequest, $_httpResponse);
//call on-after Http callback
if ($this->getHttpCallBack() != null) {
$this->getHttpCallBack()->callOnAfterRequest($_httpContext);
}
//Error handling using HTTP status codes
if ($response->code == 400) {
throw new Exceptions\MessagingException('400 Request is malformed or invalid', $_httpContext);
}
if ($response->code == 401) {
throw new Exceptions\MessagingException(
'401 The specified user does not have access to the account',
$_httpContext
);
}
if ($response->code == 403) {
throw new Exceptions\MessagingException('403 The user does not have access to this API', $_httpContext);
}
if ($response->code == 404) {
throw new Exceptions\MessagingException('404 Path not found', $_httpContext);
}
if ($response->code == 415) {
throw new Exceptions\MessagingException('415 The content-type of the request is incorrect', $_httpContext);
}
if ($response->code == 429) {
throw new Exceptions\MessagingException('429 The rate limit has been reached', $_httpContext);
}
//handle errors defined at the API level
$this->validateResponse($_httpResponse, $_httpContext);
$mapper = $this->getJsonMapper();
$deserializedResponse = $mapper->mapClassArray($response->body, 'BandwidthLib\\Messaging\\Models\\Media');
return new ApiResponse($response->code, $response->headers, $deserializedResponse);
}
/**
* Downloads a media file you previously uploaded.
*
* @param string $accountId User's account ID
* @param string $mediaId Media ID to retrieve
* @return ApiResponse response from the API call
* @throws APIException Thrown if API call fails
*/
public function getMedia(
string $accountId,
string $mediaId
) {
//prepare query string for API call
$_queryBuilder = '/users/{accountId}/media/{mediaId}';
//process optional query parameters
$_queryBuilder = APIHelper::appendUrlWithTemplateParameters($_queryBuilder, array (
'accountId' => $accountId,
'mediaId' => $mediaId,
), false
);
//validate and preprocess url
$_queryUrl = APIHelper::cleanUrl($this->config->getBaseUri(Servers::MESSAGINGDEFAULT) . $_queryBuilder);
//prepare headers
$_headers = array (
'user-agent' => BaseController::USER_AGENT
);
//set HTTP basic auth parameters
Request::auth($this->config->getMessagingBasicAuthUserName(), $this->config->getMessagingBasicAuthPassword());
$_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl);
//call on-before Http callback
if ($this->getHttpCallBack() != null) {
$this->getHttpCallBack()->callOnBeforeRequest($_httpRequest);
}
// Set request timeout
Request::timeout($this->config->getTimeout());
// and invoke the API call request to fetch the response
$response = Request::get($_queryUrl, $_headers);
$_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body);
$_httpContext = new HttpContext($_httpRequest, $_httpResponse);
//call on-after Http callback
if ($this->getHttpCallBack() != null) {
$this->getHttpCallBack()->callOnAfterRequest($_httpContext);
}
//Error handling using HTTP status codes
if ($response->code == 400) {
throw new Exceptions\MessagingException('400 Request is malformed or invalid', $_httpContext);
}
if ($response->code == 401) {
throw new Exceptions\MessagingException(
'401 The specified user does not have access to the account',
$_httpContext
);
}
if ($response->code == 403) {
throw new Exceptions\MessagingException('403 The user does not have access to this API', $_httpContext);
}
if ($response->code == 404) {
throw new Exceptions\MessagingException('404 Path not found', $_httpContext);
}
if ($response->code == 415) {
throw new Exceptions\MessagingException('415 The content-type of the request is incorrect', $_httpContext);
}
if ($response->code == 429) {
throw new Exceptions\MessagingException('429 The rate limit has been reached', $_httpContext);
}
//handle errors defined at the API level
$this->validateResponse($_httpResponse, $_httpContext);
$deserializedResponse = $response->body;
return new ApiResponse($response->code, $response->headers, $deserializedResponse);
}
/**
* Uploads a file the normal HTTP way. You may add headers to the request in order to provide some
* control to your media-file.
*
* @param string $accountId User's account ID
* @param string $mediaId The user supplied custom media ID
* @param string $body TODO: type description here
* @param string $contentType (optional) The media type of the entity-body
* @param string|null $cacheControl (optional) General-header field is used to specify directives that MUST be obeyed
* by all caching mechanisms along the request/response chain.
* @return ApiResponse response from the API call
* @throws APIException Thrown if API call fails
*/
public function uploadMedia(
string $accountId,
string $mediaId,
string $body,
string $contentType = 'application/octet-stream',
?string $cacheControl = null
) {
//prepare query string for API call
$_queryBuilder = '/users/{accountId}/media/{mediaId}';
//process optional query parameters
$_queryBuilder = APIHelper::appendUrlWithTemplateParameters($_queryBuilder, array (
'accountId' => $accountId,
'mediaId' => $mediaId,
), false
);
//validate and preprocess url
$_queryUrl = APIHelper::cleanUrl($this->config->getBaseUri(Servers::MESSAGINGDEFAULT) . $_queryBuilder);
//prepare headers
$_headers = array (
'user-agent' => BaseController::USER_AGENT,
'Content-Type' => (null != $contentType) ? $contentType : 'application/octet-stream',
'Cache-Control' => $cacheControl
);
//json encode body
$_bodyJson = $body;
//set HTTP basic auth parameters
Request::auth($this->config->getMessagingBasicAuthUserName(), $this->config->getMessagingBasicAuthPassword());
$_httpRequest = new HttpRequest(HttpMethod::PUT, $_headers, $_queryUrl);
//call on-before Http callback
if ($this->getHttpCallBack() != null) {
$this->getHttpCallBack()->callOnBeforeRequest($_httpRequest);
}
// Set request timeout
Request::timeout($this->config->getTimeout());
// and invoke the API call request to fetch the response
$response = Request::put($_queryUrl, $_headers, $_bodyJson);
$_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body);
$_httpContext = new HttpContext($_httpRequest, $_httpResponse);
//call on-after Http callback
if ($this->getHttpCallBack() != null) {
$this->getHttpCallBack()->callOnAfterRequest($_httpContext);
}
//Error handling using HTTP status codes
if ($response->code == 400) {
throw new Exceptions\MessagingException('400 Request is malformed or invalid', $_httpContext);
}
if ($response->code == 401) {
throw new Exceptions\MessagingException(
'401 The specified user does not have access to the account',
$_httpContext
);
}
if ($response->code == 403) {
throw new Exceptions\MessagingException('403 The user does not have access to this API', $_httpContext);
}
if ($response->code == 404) {
throw new Exceptions\MessagingException('404 Path not found', $_httpContext);
}
if ($response->code == 415) {
throw new Exceptions\MessagingException('415 The content-type of the request is incorrect', $_httpContext);
}
if ($response->code == 429) {
throw new Exceptions\MessagingException('429 The rate limit has been reached', $_httpContext);
}
//handle errors defined at the API level
$this->validateResponse($_httpResponse, $_httpContext);
return new ApiResponse($response->code, $response->headers, null);
}
/**
* Deletes a media file from Bandwidth API server. Make sure you don't have any application scripts
* still using the media before you delete. If you accidentally delete a media file, you can
* immediately upload a new file with the same name.
*
* @param string $accountId User's account ID
* @param string $mediaId The media ID to delete
* @return ApiResponse response from the API call
* @throws APIException Thrown if API call fails
*/
public function deleteMedia(
string $accountId,
string $mediaId
) {
//prepare query string for API call
$_queryBuilder = '/users/{accountId}/media/{mediaId}';
//process optional query parameters
$_queryBuilder = APIHelper::appendUrlWithTemplateParameters($_queryBuilder, array (
'accountId' => $accountId,
'mediaId' => $mediaId,
), false
);
//validate and preprocess url
$_queryUrl = APIHelper::cleanUrl($this->config->getBaseUri(Servers::MESSAGINGDEFAULT) . $_queryBuilder);
//prepare headers
$_headers = array (
'user-agent' => BaseController::USER_AGENT
);
//set HTTP basic auth parameters
Request::auth($this->config->getMessagingBasicAuthUserName(), $this->config->getMessagingBasicAuthPassword());
$_httpRequest = new HttpRequest(HttpMethod::DELETE, $_headers, $_queryUrl);
//call on-before Http callback
if ($this->getHttpCallBack() != null) {
$this->getHttpCallBack()->callOnBeforeRequest($_httpRequest);
}
// Set request timeout
Request::timeout($this->config->getTimeout());
// and invoke the API call request to fetch the response
$response = Request::delete($_queryUrl, $_headers);
$_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body);
$_httpContext = new HttpContext($_httpRequest, $_httpResponse);
//call on-after Http callback
if ($this->getHttpCallBack() != null) {
$this->getHttpCallBack()->callOnAfterRequest($_httpContext);
}
//Error handling using HTTP status codes
if ($response->code == 400) {
throw new Exceptions\MessagingException('400 Request is malformed or invalid', $_httpContext);
}
if ($response->code == 401) {
throw new Exceptions\MessagingException(
'401 The specified user does not have access to the account',
$_httpContext
);
}
if ($response->code == 403) {
throw new Exceptions\MessagingException('403 The user does not have access to this API', $_httpContext);
}
if ($response->code == 404) {
throw new Exceptions\MessagingException('404 Path not found', $_httpContext);
}
if ($response->code == 415) {
throw new Exceptions\MessagingException('415 The content-type of the request is incorrect', $_httpContext);
}
if ($response->code == 429) {
throw new Exceptions\MessagingException('429 The rate limit has been reached', $_httpContext);
}
//handle errors defined at the API level
$this->validateResponse($_httpResponse, $_httpContext);
return new ApiResponse($response->code, $response->headers, null);
}
/**
* Gets a list of messages based on query parameters.
*
* @param string $accountId User's account ID
* @param string|null $messageId (optional) The ID of the message to search for. Special characters need to be
* encoded using URL encoding
* @param string|null $sourceTn (optional) The phone number that sent the message
* @param string|null $destinationTn (optional) The phone number that received the message
* @param string|null $messageStatus (optional) The status of the message. One of RECEIVED, QUEUED, SENDING, SENT,
* FAILED, DELIVERED, ACCEPTED, UNDELIVERED
* @param integer|null $errorCode (optional) The error code of the message
* @param string|null $fromDateTime (optional) The start of the date range to search in ISO 8601 format. Uses the
* message receive time. The date range to search in is currently 14 days.
* @param string|null $toDateTime (optional) The end of the date range to search in ISO 8601 format. Uses the
* message receive time. The date range to search in is currently 14 days.
* @param string|null $pageToken (optional) A base64 encoded value used for pagination of results
* @param integer|null $limit (optional) The maximum records requested in search result. Default 100. The sum of
* limit and after cannot be more than 10000
* @return ApiResponse response from the API call
* @throws APIException Thrown if API call fails
*/
public function getMessages(
string $accountId,
?string $messageId = null,
?string $sourceTn = null,
?string $destinationTn = null,
?string $messageStatus = null,
?int $errorCode = null,
?string $fromDateTime = null,
?string $toDateTime = null,
?string $pageToken = null,
?int $limit = null
) {
//prepare query string for API call
$_queryBuilder = '/users/{accountId}/messages';
//process optional query parameters
$_queryBuilder = APIHelper::appendUrlWithTemplateParameters($_queryBuilder, array (
'accountId' => $accountId,
));
//process optional query parameters
APIHelper::appendUrlWithQueryParameters($_queryBuilder, array (
'messageId' => $messageId,
'sourceTn' => $sourceTn,
'destinationTn' => $destinationTn,
'messageStatus' => $messageStatus,
'errorCode' => $errorCode,
'fromDateTime' => $fromDateTime,
'toDateTime' => $toDateTime,
'pageToken' => $pageToken,
'limit' => $limit,
));
//validate and preprocess url
$_queryUrl = APIHelper::cleanUrl($this->config->getBaseUri(Servers::MESSAGINGDEFAULT) . $_queryBuilder);
//prepare headers
$_headers = array (
'user-agent' => BaseController::USER_AGENT,
'Accept' => 'application/json'
);
//set HTTP basic auth parameters
Request::auth($this->config->getMessagingBasicAuthUserName(), $this->config->getMessagingBasicAuthPassword());
$_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl);
//call on-before Http callback
if ($this->getHttpCallBack() != null) {
$this->getHttpCallBack()->callOnBeforeRequest($_httpRequest);
}
// Set request timeout
Request::timeout($this->config->getTimeout());
// and invoke the API call request to fetch the response
$response = Request::get($_queryUrl, $_headers);
$_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body);
$_httpContext = new HttpContext($_httpRequest, $_httpResponse);
//call on-after Http callback
if ($this->getHttpCallBack() != null) {
$this->getHttpCallBack()->callOnAfterRequest($_httpContext);
}
//Error handling using HTTP status codes
if ($response->code == 400) {
throw new Exceptions\MessagingException('400 Request is malformed or invalid', $_httpContext);
}
if ($response->code == 401) {
throw new Exceptions\MessagingException(
'401 The specified user does not have access to the account',
$_httpContext
);
}
if ($response->code == 403) {
throw new Exceptions\MessagingException('403 The user does not have access to this API', $_httpContext);
}
if ($response->code == 404) {
throw new Exceptions\MessagingException('404 Path not found', $_httpContext);
}
if ($response->code == 415) {
throw new Exceptions\MessagingException('415 The content-type of the request is incorrect', $_httpContext);
}
if ($response->code == 429) {
throw new Exceptions\MessagingException('429 The rate limit has been reached', $_httpContext);
}
//handle errors defined at the API level
$this->validateResponse($_httpResponse, $_httpContext);
$mapper = $this->getJsonMapper();
$deserializedResponse = $mapper->mapClass(
$response->body,
'BandwidthLib\\Messaging\\Models\\BandwidthMessagesList'
);
return new ApiResponse($response->code, $response->headers, $deserializedResponse);
}
/**
* Endpoint for sending text messages and picture messages using V2 messaging.
*
* @param string $accountId User's account ID
* @param Models\MessageRequest $body TODO: type description here
* @return ApiResponse response from the API call
* @throws APIException Thrown if API call fails
*/
public function createMessage(
string $accountId,
Models\MessageRequest $body
) {
//prepare query string for API call
$_queryBuilder = '/users/{accountId}/messages';
//process optional query parameters
$_queryBuilder = APIHelper::appendUrlWithTemplateParameters($_queryBuilder, array (
'accountId' => $accountId,
));
//validate and preprocess url
$_queryUrl = APIHelper::cleanUrl($this->config->getBaseUri(Servers::MESSAGINGDEFAULT) . $_queryBuilder);
//prepare headers
$_headers = array (
'user-agent' => BaseController::USER_AGENT,
'Accept' => 'application/json',
'content-type' => 'application/json; charset=utf-8'
);
//json encode body
$_bodyJson = Request\Body::Json($body);
//set HTTP basic auth parameters
Request::auth($this->config->getMessagingBasicAuthUserName(), $this->config->getMessagingBasicAuthPassword());
$_httpRequest = new HttpRequest(HttpMethod::POST, $_headers, $_queryUrl);
//call on-before Http callback
if ($this->getHttpCallBack() != null) {
$this->getHttpCallBack()->callOnBeforeRequest($_httpRequest);
}
// Set request timeout
Request::timeout($this->config->getTimeout());
// and invoke the API call request to fetch the response
$response = Request::post($_queryUrl, $_headers, $_bodyJson);
$_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body);
$_httpContext = new HttpContext($_httpRequest, $_httpResponse);
//call on-after Http callback
if ($this->getHttpCallBack() != null) {
$this->getHttpCallBack()->callOnAfterRequest($_httpContext);
}
//Error handling using HTTP status codes
if ($response->code == 400) {
throw new Exceptions\MessagingException('400 Request is malformed or invalid', $_httpContext);
}
if ($response->code == 401) {
throw new Exceptions\MessagingException(
'401 The specified user does not have access to the account',
$_httpContext
);
}
if ($response->code == 403) {
throw new Exceptions\MessagingException('403 The user does not have access to this API', $_httpContext);
}
if ($response->code == 404) {
throw new Exceptions\MessagingException('404 Path not found', $_httpContext);
}
if ($response->code == 415) {
throw new Exceptions\MessagingException('415 The content-type of the request is incorrect', $_httpContext);
}
if ($response->code == 429) {
throw new Exceptions\MessagingException('429 The rate limit has been reached', $_httpContext);
}
//handle errors defined at the API level
$this->validateResponse($_httpResponse, $_httpContext);
$mapper = $this->getJsonMapper();
$deserializedResponse = $mapper->mapClass($response->body, 'BandwidthLib\\Messaging\\Models\\BandwidthMessage');
return new ApiResponse($response->code, $response->headers, $deserializedResponse);
}
}