diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 93b4532..9e503de 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,7 +20,7 @@ jobs: php-version: [8.0, 8.1, 8.2, 8.3] steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v5 - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -41,10 +41,3 @@ jobs: USER_NUMBER: ${{ secrets.USER_NUMBER }} BASE_CALLBACK_URL: ${{ secrets.BASE_CALLBACK_URL }} run: ./vendor/bin/phpunit tests - - - uses: Bandwidth/build-notify-slack-action@v2 - if: failure() && !github.event.pull_request.draft - with: - job-status: ${{ job.status }} - slack-bot-token: ${{ secrets.SLACK_BOT_TOKEN }} - slack-channel: ${{ secrets.SLACK_CHANNEL }} diff --git a/src/Configuration.php b/src/Configuration.php index 77886ba..347ca21 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -284,7 +284,7 @@ public function getBaseUri(string $server = Servers::DEFAULT_) Servers::DEFAULT_ => 'api.bandwidth.com', Servers::MESSAGINGDEFAULT => 'https://messaging.bandwidth.com/api/v2', Servers::MULTIFACTORAUTHDEFAULT => 'https://mfa.bandwidth.com/api/v1', - Servers::PHONENUMBERLOOKUPDEFAULT => 'https://numbers.bandwidth.com/api/v1', + Servers::PHONENUMBERLOOKUPDEFAULT => 'https://api.bandwidth.com/v2', Servers::VOICEDEFAULT => 'https://voice.bandwidth.com', Servers::WEBRTCDEFAULT => 'https://api.webrtc.bandwidth.com/v1', ), diff --git a/src/PhoneNumberLookup/Controllers/APIController.php b/src/PhoneNumberLookup/Controllers/APIController.php index 6ab00ee..4f9dc8d 100644 --- a/src/PhoneNumberLookup/Controllers/APIController.php +++ b/src/PhoneNumberLookup/Controllers/APIController.php @@ -31,20 +31,20 @@ public function __construct($config, $httpCallBack = null) } /** - * Create a TN Lookup Order. + * Create a Bulk Async TN Lookup Order. * * @param string $accountId The ID of the Bandwidth account that the user belongs to. - * @param Models\OrderRequest $body TODO: type description here + * @param Models\CreateLookupRequest $body * @return ApiResponse response from the API call * @throws APIException Thrown if API call fails */ - public function createLookupRequest( + public function createAsyncBulkLookupRequest( string $accountId, - Models\OrderRequest $body + Models\CreateLookupRequest $body ) { //prepare query string for API call - $_queryBuilder = '/accounts/{accountId}/tnlookup'; + $_queryBuilder = '/accounts/{accountId}/phoneNumberLookup/bulk'; //process optional query parameters $_queryBuilder = APIHelper::appendUrlWithTemplateParameters($_queryBuilder, array ( @@ -120,7 +120,7 @@ public function createLookupRequest( ); } - if ($response->code == 500) { + 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.', @@ -128,1544 +128,104 @@ public function createLookupRequest( ); } - if ($response->code == 501) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 502) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 503) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 504) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 505) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 506) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 507) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 508) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 509) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 510) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 511) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 512) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 513) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 514) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 515) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 516) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 517) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 518) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 519) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 520) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 521) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 522) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 523) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 524) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 525) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 526) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 527) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 528) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 529) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 530) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 531) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 532) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 533) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 534) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 535) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 536) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 537) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 538) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 539) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 540) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 541) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 542) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 543) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 544) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 545) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 546) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 547) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 548) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 549) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 550) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 551) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 552) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 553) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 554) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 555) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 556) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 557) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 558) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 559) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 560) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 561) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 562) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 563) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 564) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 565) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 566) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 567) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 568) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 569) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 570) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 571) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 572) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 573) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 574) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 575) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 576) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 577) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 578) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 579) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 580) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 581) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 582) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 583) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 584) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 585) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 586) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 587) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 588) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 589) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 590) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 591) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 592) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 593) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 594) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 595) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 596) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 597) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 598) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 599) { - 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\\OrderResponse' - ); - return new ApiResponse($response->code, $response->headers, $deserializedResponse); - } - - /** - * Query an existing TN Lookup Order. - * - * @param string $accountId The ID of the Bandwidth account that the user belongs to. - * @param string $requestId TODO: type description here - * @return ApiResponse response from the API call - * @throws APIException Thrown if API call fails - */ - public function getLookupRequestStatus( - string $accountId, - string $requestId - ) { - - //prepare query string for API call - $_queryBuilder = '/accounts/{accountId}/tnlookup/{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 HTTP basic auth parameters - Request::auth($this->config->getPhoneNumberLookupBasicAuthUserName(), $this->config->getPhoneNumberLookupBasicAuthPassword()); - - $_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 - ); - } - - if ($response->code == 501) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 502) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 503) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 504) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 505) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 506) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 507) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 508) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 509) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 510) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 511) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 512) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 513) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 514) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 515) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 516) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 517) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 518) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 519) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 520) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 521) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 522) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 523) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 524) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 525) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 526) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 527) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 528) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 529) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 530) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 531) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 532) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 533) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 534) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 535) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 536) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 537) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 538) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 539) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 540) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 541) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 542) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 543) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 544) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 545) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 546) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 547) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 548) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 549) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 550) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 551) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 552) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 553) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 554) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 555) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 556) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 557) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 558) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 559) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 560) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 561) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 562) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 563) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 564) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } - - if ($response->code == 565) { - 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); + } - if ($response->code == 566) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } + /** + * 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 + ) { - if ($response->code == 567) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } + //prepare query string for API call + $_queryBuilder = '/accounts/{accountId}/phoneNumberLookup/bulk/{requestId}'; - if ($response->code == 568) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } + //process optional query parameters + $_queryBuilder = APIHelper::appendUrlWithTemplateParameters($_queryBuilder, array ( + 'accountId' => $accountId, + 'requestId' => $requestId, + )); - if ($response->code == 569) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } + //validate and preprocess url + $_queryUrl = APIHelper::cleanUrl($this->config->getBaseUri(Servers::PHONENUMBERLOOKUPDEFAULT) . $_queryBuilder); - if ($response->code == 570) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } + //prepare headers + $_headers = array ( + 'user-agent' => BaseController::USER_AGENT, + 'Accept' => 'application/json' + ); - if ($response->code == 571) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } + //set HTTP basic auth parameters + Request::auth($this->config->getPhoneNumberLookupBasicAuthUserName(), $this->config->getPhoneNumberLookupBasicAuthPassword()); - if ($response->code == 572) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } + $_httpRequest = new HttpRequest(HttpMethod::GET, $_headers, $_queryUrl); - if ($response->code == 573) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); + //call on-before Http callback + if ($this->getHttpCallBack() != null) { + $this->getHttpCallBack()->callOnBeforeRequest($_httpRequest); } + // Set request timeout + Request::timeout($this->config->getTimeout()); - if ($response->code == 574) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } + // and invoke the API call request to fetch the response + $response = Request::get($_queryUrl, $_headers); - if ($response->code == 575) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } + $_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body); + $_httpContext = new HttpContext($_httpRequest, $_httpResponse); - if ($response->code == 576) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); + //call on-after Http callback + if ($this->getHttpCallBack() != null) { + $this->getHttpCallBack()->callOnAfterRequest($_httpContext); } - if ($response->code == 577) { + //Error handling using HTTP status codes + if ($response->code == 400) { throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', + 'Bad Request. Ensure that you have set the requestId as a URL path parameter.', $_httpContext ); } - if ($response->code == 578) { + if ($response->code == 401) { throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', + '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 == 579) { + if ($response->code == 404) { throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', + '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 == 580) { + if ($response->code == 429) { throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', + 'Too Many Requests. Reduce the amount of requests that you are sending in order to avoid receiving ' . + 'this status code.', $_httpContext ); } - if ($response->code == 581) { + 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.', @@ -1673,143 +233,107 @@ public function getLookupRequestStatus( ); } - if ($response->code == 582) { - 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); + } - if ($response->code == 583) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } + /** + * 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 + ) { - if ($response->code == 584) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } + //prepare query string for API call + $_queryBuilder = '/accounts/{accountId}/phoneNumberLookup'; - if ($response->code == 585) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } + //process optional query parameters + $_queryBuilder = APIHelper::appendUrlWithTemplateParameters($_queryBuilder, array ( + 'accountId' => $accountId, + )); - if ($response->code == 586) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } + //validate and preprocess url + $_queryUrl = APIHelper::cleanUrl($this->config->getBaseUri(Servers::PHONENUMBERLOOKUPDEFAULT) . $_queryBuilder); - if ($response->code == 587) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } + //prepare headers + $_headers = array ( + 'user-agent' => BaseController::USER_AGENT, + 'Accept' => 'application/json', + 'content-type' => 'application/json; charset=utf-8' + ); - if ($response->code == 588) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } + //json encode body + $_bodyJson = Request\Body::Json($body); - if ($response->code == 589) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } + //set HTTP basic auth parameters + Request::auth($this->config->getPhoneNumberLookupBasicAuthUserName(), $this->config->getPhoneNumberLookupBasicAuthPassword()); - if ($response->code == 590) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } + $_httpRequest = new HttpRequest(HttpMethod::POST, $_headers, $_queryUrl); - if ($response->code == 591) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); + //call on-before Http callback + if ($this->getHttpCallBack() != null) { + $this->getHttpCallBack()->callOnBeforeRequest($_httpRequest); } + // Set request timeout + Request::timeout($this->config->getTimeout()); - if ($response->code == 592) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } + // and invoke the API call request to fetch the response + $response = Request::post($_queryUrl, $_headers, $_bodyJson); - if ($response->code == 593) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); - } + $_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body); + $_httpContext = new HttpContext($_httpRequest, $_httpResponse); - if ($response->code == 594) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', - $_httpContext - ); + //call on-after Http callback + if ($this->getHttpCallBack() != null) { + $this->getHttpCallBack()->callOnAfterRequest($_httpContext); } - if ($response->code == 595) { - throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', + //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 == 596) { + if ($response->code == 401) { throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', + '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 == 597) { + if ($response->code == 415) { throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', + 'Invalid content-type. Ensure that your content-type header is set to application/json.', $_httpContext ); } - if ($response->code == 598) { + if ($response->code == 429) { throw new APIException( - 'Unexpected error. Please contact Bandwidth Support if your requests are receiving this status code ' . - 'for an extended period of time.', + 'Too Many Requests. Reduce the amount of requests that you are sending in order to avoid receiving ' . + 'this status code.', $_httpContext ); } - if ($response->code == 599) { + 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.', @@ -1822,7 +346,7 @@ public function getLookupRequestStatus( $mapper = $this->getJsonMapper(); $deserializedResponse = $mapper->mapClass( $response->body, - 'BandwidthLib\\PhoneNumberLookup\\Models\\OrderStatus' + 'BandwidthLib\\PhoneNumberLookup\\Models\\LookupResponse' ); return new ApiResponse($response->code, $response->headers, $deserializedResponse); } diff --git a/src/PhoneNumberLookup/Models/CreateAsyncBulkResponse.php b/src/PhoneNumberLookup/Models/CreateAsyncBulkResponse.php new file mode 100644 index 0000000..e333180 --- /dev/null +++ b/src/PhoneNumberLookup/Models/CreateAsyncBulkResponse.php @@ -0,0 +1,52 @@ +links = func_get_arg(0); + $this->data = func_get_arg(1); + $this->errors = func_get_arg(2); + } + } + + /** + * Encode this object to JSON + */ + public function jsonSerialize(): array + { + $json = array(); + $json['links'] = isset($this->links) ? array_values($this->links) : null; + $json['data'] = $this->data; + $json['errors'] = isset($this->errors) ? array_values($this->errors) : null; + + return array_filter($json); + } +} diff --git a/src/PhoneNumberLookup/Models/CreateAsyncBulkResponseData.php b/src/PhoneNumberLookup/Models/CreateAsyncBulkResponseData.php new file mode 100644 index 0000000..1a6ae17 --- /dev/null +++ b/src/PhoneNumberLookup/Models/CreateAsyncBulkResponseData.php @@ -0,0 +1,44 @@ +requestId = func_get_arg(0); + $this->status = func_get_arg(1); + } + } + + /** + * Encode this object to JSON + */ + public function jsonSerialize(): array + { + $json = array(); + $json['requestId'] = $this->requestId; + $json['status'] = $this->status; + + return array_filter($json); + } +} diff --git a/src/PhoneNumberLookup/Models/CreateLookupRequest.php b/src/PhoneNumberLookup/Models/CreateLookupRequest.php new file mode 100644 index 0000000..8c9ec72 --- /dev/null +++ b/src/PhoneNumberLookup/Models/CreateLookupRequest.php @@ -0,0 +1,38 @@ +phoneNumbers = func_get_arg(0); + } + } + + /** + * Encode this object to JSON + */ + public function jsonSerialize(): array + { + $json = array(); + $json['phoneNumbers'] = isset($this->phoneNumbers) ? array_values($this->phoneNumbers) : null; + + return array_filter($json); + } +} diff --git a/src/PhoneNumberLookup/Models/Error.php b/src/PhoneNumberLookup/Models/Error.php new file mode 100644 index 0000000..ae3d76f --- /dev/null +++ b/src/PhoneNumberLookup/Models/Error.php @@ -0,0 +1,60 @@ +code = func_get_arg(0); + $this->description = func_get_arg(1); + $this->type = func_get_arg(2); + $this->meta = func_get_arg(3); + } + } + + /** + * Encode this object to JSON + */ + public function jsonSerialize(): array + { + $json = array(); + $json['code'] = $this->code; + $json['description'] = $this->description; + $json['type'] = $this->type; + $json['meta'] = $this->meta; + + return array_filter($json); + } +} diff --git a/src/PhoneNumberLookup/Models/ErrorMeta.php b/src/PhoneNumberLookup/Models/ErrorMeta.php new file mode 100644 index 0000000..e0c6e33 --- /dev/null +++ b/src/PhoneNumberLookup/Models/ErrorMeta.php @@ -0,0 +1,52 @@ +phoneNumbers = func_get_arg(0); + $this->message = func_get_arg(1); + $this->code = func_get_arg(2); + } + } + + /** + * Encode this object to JSON + */ + public function jsonSerialize(): array + { + $json = array(); + $json['phoneNumbers'] = isset($this->phoneNumbers) ? array_values($this->phoneNumbers) : null; + $json['message'] = $this->message; + $json['code'] = $this->code; + + return array_filter($json); + } +} diff --git a/src/PhoneNumberLookup/Models/Link.php b/src/PhoneNumberLookup/Models/Link.php new file mode 100644 index 0000000..8172c00 --- /dev/null +++ b/src/PhoneNumberLookup/Models/Link.php @@ -0,0 +1,52 @@ +href = func_get_arg(0); + $this->rel = func_get_arg(1); + $this->method = func_get_arg(2); + } + } + + /** + * Encode this object to JSON + */ + public function jsonSerialize(): array + { + $json = array(); + $json['href'] = $this->href; + $json['rel'] = $this->rel; + $json['method'] = $this->method; + + return array_filter($json); + } +} diff --git a/src/PhoneNumberLookup/Models/LookupResponse.php b/src/PhoneNumberLookup/Models/LookupResponse.php new file mode 100644 index 0000000..e91d445 --- /dev/null +++ b/src/PhoneNumberLookup/Models/LookupResponse.php @@ -0,0 +1,52 @@ +links = func_get_arg(0); + $this->data = func_get_arg(1); + $this->errors = func_get_arg(2); + } + } + + /** + * Encode this object to JSON + */ + public function jsonSerialize(): array + { + $json = array(); + $json['links'] = isset($this->links) ? array_values($this->links) : null; + $json['data'] = $this->data; + $json['errors'] = isset($this->errors) ? array_values($this->errors) : null; + + return array_filter($json); + } +} diff --git a/src/PhoneNumberLookup/Models/LookupResponseData.php b/src/PhoneNumberLookup/Models/LookupResponseData.php new file mode 100644 index 0000000..54e945b --- /dev/null +++ b/src/PhoneNumberLookup/Models/LookupResponseData.php @@ -0,0 +1,53 @@ +requestId = func_get_arg(0); + $this->status = func_get_arg(1); + $this->results = func_get_arg(2); + } + } + + /** + * Encode this object to JSON + */ + public function jsonSerialize(): array + { + $json = array(); + $json['requestId'] = $this->requestId; + $json['status'] = $this->status; + $json['results'] = isset($this->results) ? array_values($this->results) : null; + + return array_filter($json); + } +} diff --git a/src/PhoneNumberLookup/Models/LookupResult.php b/src/PhoneNumberLookup/Models/LookupResult.php new file mode 100644 index 0000000..8a86ec3 --- /dev/null +++ b/src/PhoneNumberLookup/Models/LookupResult.php @@ -0,0 +1,119 @@ +phoneNumber = func_get_arg(0); + $this->lineType = func_get_arg(1); + $this->messagingProvider = func_get_arg(2); + $this->voiceProvider = func_get_arg(3); + $this->countryCodeA3 = func_get_arg(4); + $this->deactivationReporter = func_get_arg(5); + $this->deactivationDate = func_get_arg(6); + $this->deactivationEvent = func_get_arg(7); + $this->latestMessageDeliveryStatus = func_get_arg(8); + $this->initialMessageDeliveryStatusDate = func_get_arg(9); + $this->latestMessageDeliveryStatusDate = func_get_arg(10); + } + } + + /** + * Encode this object to JSON + */ + public function jsonSerialize(): array + { + $json = array(); + $json['phoneNumber'] = $this->phoneNumber; + $json['lineType'] = $this->lineType; + $json['messagingProvider'] = $this->messagingProvider; + $json['voiceProvider'] = $this->voiceProvider; + $json['countryCodeA3'] = $this->countryCodeA3; + $json['deactivationReporter'] = $this->deactivationReporter; + $json['deactivationDate'] = $this->deactivationDate; + $json['deactivationEvent'] = $this->deactivationEvent; + $json['latestMessageDeliveryStatus'] = $this->latestMessageDeliveryStatus; + $json['initialMessageDeliveryStatusDate'] = isset($this->initialMessageDeliveryStatusDate) ? + DateTimeHelper::toRfc3339DateTime($this->initialMessageDeliveryStatusDate) : null; + $json['latestMessageDeliveryStatusDate'] = isset($this->latestMessageDeliveryStatusDate) ? + DateTimeHelper::toRfc3339DateTime($this->latestMessageDeliveryStatusDate) : null; + + return array_filter($json); + } +} diff --git a/src/PhoneNumberLookup/Models/OrderRequest.php b/src/PhoneNumberLookup/Models/OrderRequest.php deleted file mode 100644 index e8e077f..0000000 --- a/src/PhoneNumberLookup/Models/OrderRequest.php +++ /dev/null @@ -1,42 +0,0 @@ -tns = func_get_arg(0); - } - } - - /** - * Encode this object to JSON - */ - public function jsonSerialize(): array - { - $json = array(); - $json['tns'] = isset($this->tns) ? - array_values($this->tns) : null; - - return array_filter($json); - } -} diff --git a/src/PhoneNumberLookup/Models/OrderResponse.php b/src/PhoneNumberLookup/Models/OrderResponse.php deleted file mode 100644 index 1129b98..0000000 --- a/src/PhoneNumberLookup/Models/OrderResponse.php +++ /dev/null @@ -1,50 +0,0 @@ -requestId = func_get_arg(0); - $this->status = func_get_arg(1); - } - } - - /** - * Encode this object to JSON - */ - public function jsonSerialize(): array - { - $json = array(); - $json['requestId'] = $this->requestId; - $json['status'] = $this->status; - - return array_filter($json); - } -} diff --git a/src/PhoneNumberLookup/Models/OrderStatus.php b/src/PhoneNumberLookup/Models/OrderStatus.php deleted file mode 100644 index a3d3d99..0000000 --- a/src/PhoneNumberLookup/Models/OrderStatus.php +++ /dev/null @@ -1,71 +0,0 @@ -requestId = func_get_arg(0); - $this->status = func_get_arg(1); - $this->failedTelephoneNumbers = func_get_arg(2); - $this->result = func_get_arg(3); - } - } - - /** - * Encode this object to JSON - */ - public function jsonSerialize(): array - { - $json = array(); - $json['requestId'] = $this->requestId; - $json['status'] = $this->status; - $json['failedTelephoneNumbers'] = - isset($this->failedTelephoneNumbers) ? - array_values($this->failedTelephoneNumbers) : null; - $json['result'] = isset($this->result) ? - array_values($this->result) : null; - - return array_filter($json); - } -} diff --git a/src/PhoneNumberLookup/Models/Result.php b/src/PhoneNumberLookup/Models/Result.php deleted file mode 100644 index 2547eff..0000000 --- a/src/PhoneNumberLookup/Models/Result.php +++ /dev/null @@ -1,114 +0,0 @@ -responseCode = func_get_arg(0); - $this->message = func_get_arg(1); - $this->e164Format = func_get_arg(2); - $this->formatted = func_get_arg(3); - $this->country = func_get_arg(4); - $this->lineType = func_get_arg(5); - $this->lineProvider = func_get_arg(6); - $this->mobileCountryCode = func_get_arg(7); - $this->mobileNetworkCode = func_get_arg(8); - } - } - - /** - * Encode this object to JSON - */ - public function jsonSerialize(): array - { - $json = array(); - $json['Response Code'] = $this->responseCode; - $json['Message'] = $this->message; - $json['E.164 Format'] = $this->e164Format; - $json['Formatted'] = $this->formatted; - $json['Country'] = $this->country; - $json['Line Type'] = $this->lineType; - $json['Line Provider'] = $this->lineProvider; - $json['Mobile Country Code'] = $this->mobileCountryCode; - $json['Mobile Network Code'] = $this->mobileNetworkCode; - - return array_filter($json); - } -} diff --git a/tests/ApiTest.php b/tests/ApiTest.php index d88760c..9d54d9a 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -193,14 +193,53 @@ public function testMfaVerify() { $this->assertTrue(is_bool($response->getResult()->valid)); } - public function testTnLookup() { - $body = new BandwidthLib\PhoneNumberLookup\Models\OrderRequest(); - $body->tns = [getenv("USER_NUMBER")]; - $createResponse = $this->bandwidthClient->getPhoneNumberLookup()->getClient()->createLookupRequest(getenv("BW_ACCOUNT_ID"), $body); - $this->assertTrue(strlen($createResponse->getResult()->requestId) > 0); - - $requestId = $createResponse->getResult()->requestId; - $getResponse = $this->bandwidthClient->getPhoneNumberLookup()->getClient()->getLookupRequestStatus(getenv("BW_ACCOUNT_ID"), $requestId); - $this->assertTrue(strlen($getResponse->getResult()->status) > 0); + public function testAsyncTnLookup() { + $body = new BandwidthLib\PhoneNumberLookup\Models\CreateLookupRequest(); + $body->phoneNumbers = [getenv("USER_NUMBER")]; + $createResponse = $this->bandwidthClient->getPhoneNumberLookup()->getClient()->createAsyncBulkLookupRequest(getenv("BW_ACCOUNT_ID"), $body); + $this->assertInstanceOf(BandwidthLib\PhoneNumberLookup\Models\CreateAsyncBulkResponse::class, $createResponse->getResult()); + $this->assertIsArray($createResponse->getResult()->links); + $this->assertInstanceOf(BandwidthLib\PhoneNumberLookup\Models\CreateAsyncBulkResponseData::class, $createResponse->getResult()->data); + $this->assertIsString($createResponse->getResult()->data->requestId); + $this->assertIsString($createResponse->getResult()->data->status); + $this->assertIsArray($createResponse->getResult()->errors); + + sleep(30); + + $statusResponse = $this->bandwidthClient->getPhoneNumberLookup()->getClient()->getAsyncLookupRequestStatus(getenv("BW_ACCOUNT_ID"), $createResponse->getResult()->data->requestId); + $this->assertInstanceOf(BandwidthLib\PhoneNumberLookup\Models\LookupResponse::class, $statusResponse->getResult()); + $this->assertIsArray($statusResponse->getResult()->links); + $this->assertInstanceOf(BandwidthLib\PhoneNumberLookup\Models\LookupResponseData::class, $statusResponse->getResult()->data); + $this->assertIsString($statusResponse->getResult()->data->requestId); + $this->assertIsString($statusResponse->getResult()->data->status); + $this->assertIsArray($statusResponse->getResult()->data->results); + $this->assertInstanceOf(BandwidthLib\PhoneNumberLookup\Models\LookupResult::class, $statusResponse->getResult()->data->results[0]); + $this->assertIsString($statusResponse->getResult()->data->results[0]->phoneNumber); + $this->assertIsString($statusResponse->getResult()->data->results[0]->lineType); + $this->assertIsString($statusResponse->getResult()->data->results[0]->messagingProvider); + $this->assertIsString($statusResponse->getResult()->data->results[0]->voiceProvider); + $this->assertIsString($statusResponse->getResult()->data->results[0]->countryCodeA3); + $this->assertIsArray($statusResponse->getResult()->errors); + } + + public function testSyncTnLookup() { + $body = new BandwidthLib\PhoneNumberLookup\Models\CreateLookupRequest(); + $body->phoneNumbers = [getenv("USER_NUMBER")]; + $response = $this->bandwidthClient->getPhoneNumberLookup()->getClient()->createSyncLookupRequest(getenv("BW_ACCOUNT_ID"), $body); + $this->assertInstanceOf(BandwidthLib\PhoneNumberLookup\Models\LookupResponse::class, $response->getResult()); + $this->assertIsArray($response->getResult()->links); + $this->assertInstanceOf(BandwidthLib\PhoneNumberLookup\Models\Link::class, $response->getResult()->links[0]); + $this->assertInstanceOf(BandwidthLib\PhoneNumberLookup\Models\LookupResponseData::class, $response->getResult()->data); + $this->assertIsString($response->getResult()->data->requestId); + $this->assertIsString($response->getResult()->data->status); + $this->assertEquals("COMPLETE", $response->getResult()->data->status); + $this->assertIsArray($response->getResult()->data->results); + $this->assertInstanceOf(BandwidthLib\PhoneNumberLookup\Models\LookupResult::class, $response->getResult()->data->results[0]); + $this->assertIsString($response->getResult()->data->results[0]->phoneNumber); + $this->assertIsString($response->getResult()->data->results[0]->lineType); + $this->assertIsString($response->getResult()->data->results[0]->messagingProvider); + $this->assertIsString($response->getResult()->data->results[0]->voiceProvider); + $this->assertIsString($response->getResult()->data->results[0]->countryCodeA3); + $this->assertIsArray($response->getResult()->errors); } }