-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAPIController.php
More file actions
353 lines (297 loc) · 12.9 KB
/
APIController.php
File metadata and controls
353 lines (297 loc) · 12.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
<?php
/*
* BandwidthLib
*
* This file was automatically generated by APIMATIC v3.0 ( https://www.apimatic.io ).
*/
namespace BandwidthLib\PhoneNumberLookup\Controllers;
use BandwidthLib\APIException;
use BandwidthLib\APIHelper;
use BandwidthLib\PhoneNumberLookup\Exceptions;
use BandwidthLib\PhoneNumberLookup\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);
}
/**
* Create a Bulk Async TN Lookup Order.
*
* @param string $accountId The ID of the Bandwidth account that the user belongs to.
* @param Models\CreateLookupRequest $body
* @return ApiResponse response from the API call
* @throws APIException Thrown if API call fails
*/
public function createAsyncBulkLookupRequest(
string $accountId,
Models\CreateLookupRequest $body
) {
//prepare query string for API call
$_queryBuilder = '/accounts/{accountId}/phoneNumberLookup/bulk';
//process optional query parameters
$_queryBuilder = APIHelper::appendUrlWithTemplateParameters($_queryBuilder, array (
'accountId' => $accountId,
));
//validate and preprocess url
$_queryUrl = APIHelper::cleanUrl($this->config->getBaseUri(Servers::PHONENUMBERLOOKUPDEFAULT) . $_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 authentication
$this->configureAuth($_headers, 'phoneNumberLookup');
$_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\AccountsTnlookup400ErrorException(
'Bad Request. Ensure that your request payload is properly formatted and that the telephone numbers ' .
'used are valid.',
$_httpContext
);
}
if ($response->code == 401) {
throw new APIException(
'Unauthorized. Ensure that you are using the proper credentials for the environment you are ' .
'accessing, your user has the proper role assigned to it, and that your Bandwidth account is' .
'enabled for TN Lookup access.',
$_httpContext
);
}
if ($response->code == 415) {
throw new APIException(
'Invalid content-type. Ensure that your content-type header is set to application/json.',
$_httpContext
);
}
if ($response->code == 429) {
throw new APIException(
'Too Many Requests. Reduce the amount of requests that you are sending in order to avoid receiving ' .
'this status code.',
$_httpContext
);
}
if ($response->code >= 500) {
throw new APIException(
'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' .
'for an extended period of time.',
$_httpContext
);
}
//handle errors defined at the API level
$this->validateResponse($_httpResponse, $_httpContext);
$mapper = $this->getJsonMapper();
$deserializedResponse = $mapper->mapClass(
$response->body,
'BandwidthLib\\PhoneNumberLookup\\Models\\CreateAsyncBulkResponse'
);
return new ApiResponse($response->code, $response->headers, $deserializedResponse);
}
/**
* Get a Bulk Async TN Lookup Order.
*
* @param string $accountId The ID of the Bandwidth account that the user belongs to.
* @param string $requestId The phone number lookup request ID from Bandwidth.
* @return ApiResponse response from the API call
* @throws APIException Thrown if API call fails
*/
public function getAsyncLookupRequestStatus(
string $accountId,
string $requestId
) {
//prepare query string for API call
$_queryBuilder = '/accounts/{accountId}/phoneNumberLookup/bulk/{requestId}';
//process optional query parameters
$_queryBuilder = APIHelper::appendUrlWithTemplateParameters($_queryBuilder, array (
'accountId' => $accountId,
'requestId' => $requestId,
));
//validate and preprocess url
$_queryUrl = APIHelper::cleanUrl($this->config->getBaseUri(Servers::PHONENUMBERLOOKUPDEFAULT) . $_queryBuilder);
//prepare headers
$_headers = array (
'user-agent' => BaseController::USER_AGENT,
'Accept' => 'application/json'
);
//set authentication
$this->configureAuth($_headers, 'phoneNumberLookup');
$_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 APIException(
'Bad Request. Ensure that you have set the requestId as a URL path parameter.',
$_httpContext
);
}
if ($response->code == 401) {
throw new APIException(
'Unauthorized. Ensure that you are using the proper credentials for the environment you are ' .
'accessing, your user has the proper role assigned to it, and that your Bandwidth account is' .
'enabled for TN Lookup access.',
$_httpContext
);
}
if ($response->code == 404) {
throw new APIException(
'RequestId not found. Ensure that the requestId used in the URL path is valid and maps to a ' .
'previous request that was submitted.',
$_httpContext
);
}
if ($response->code == 429) {
throw new APIException(
'Too Many Requests. Reduce the amount of requests that you are sending in order to avoid receiving ' .
'this status code.',
$_httpContext
);
}
if ($response->code >= 500) {
throw new APIException(
'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' .
'for an extended period of time.',
$_httpContext
);
}
//handle errors defined at the API level
$this->validateResponse($_httpResponse, $_httpContext);
$mapper = $this->getJsonMapper();
$deserializedResponse = $mapper->mapClass(
$response->body,
'BandwidthLib\\PhoneNumberLookup\\Models\\LookupResponse'
);
return new ApiResponse($response->code, $response->headers, $deserializedResponse);
}
/**
* Create a Sync TN Lookup Order.
*
* @param string $accountId The ID of the Bandwidth account that the user belongs to.
* @param Models\CreateLookupRequest $body
* @return ApiResponse response from the API call
* @throws APIException Thrown if API call fails
*/
public function createSyncLookupRequest(
string $accountId,
Models\CreateLookupRequest $body
) {
//prepare query string for API call
$_queryBuilder = '/accounts/{accountId}/phoneNumberLookup';
//process optional query parameters
$_queryBuilder = APIHelper::appendUrlWithTemplateParameters($_queryBuilder, array (
'accountId' => $accountId,
));
//validate and preprocess url
$_queryUrl = APIHelper::cleanUrl($this->config->getBaseUri(Servers::PHONENUMBERLOOKUPDEFAULT) . $_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 authentication
$this->configureAuth($_headers, 'phoneNumberLookup');
$_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\AccountsTnlookup400ErrorException(
'Bad Request. Ensure that your request payload is properly formatted and that the telephone numbers ' .
'used are valid.',
$_httpContext
);
}
if ($response->code == 401) {
throw new APIException(
'Unauthorized. Ensure that you are using the proper credentials for the environment you are ' .
'accessing, your user has the proper role assigned to it, and that your Bandwidth account is' .
'enabled for TN Lookup access.',
$_httpContext
);
}
if ($response->code == 415) {
throw new APIException(
'Invalid content-type. Ensure that your content-type header is set to application/json.',
$_httpContext
);
}
if ($response->code == 429) {
throw new APIException(
'Too Many Requests. Reduce the amount of requests that you are sending in order to avoid receiving ' .
'this status code.',
$_httpContext
);
}
if ($response->code >= 500) {
throw new APIException(
'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' .
'for an extended period of time.',
$_httpContext
);
}
//handle errors defined at the API level
$this->validateResponse($_httpResponse, $_httpContext);
$mapper = $this->getJsonMapper();
$deserializedResponse = $mapper->mapClass(
$response->body,
'BandwidthLib\\PhoneNumberLookup\\Models\\LookupResponse'
);
return new ApiResponse($response->code, $response->headers, $deserializedResponse);
}
}