From 4329b75161df6973ae1f3fb63fc268ae13c0383d Mon Sep 17 00:00:00 2001 From: Jacob Mulford <39915377+jmulford-bw@users.noreply.github.com> Date: Mon, 1 Feb 2021 13:52:38 -0500 Subject: [PATCH] New deploy (#18) * New deploy * Update APIController.php Co-authored-by: jmulford-bw --- composer.json | 3 +- phpunit.xml | 20 +++ src/Messaging/Controllers/APIController.php | 158 ++++++++++++++++-- src/Messaging/Models/BandwidthMessage.php | 23 +-- src/Messaging/Models/BandwidthMessageItem.php | 121 ++++++++++++++ .../Models/BandwidthMessagesList.php | 58 +++++++ src/Messaging/Models/Media.php | 2 +- src/Messaging/Models/MessageRequest.php | 25 +-- src/Messaging/Models/PageInfo.php | 65 +++++++ .../CallEngineModifyConferenceRequest.php | 106 ------------ 10 files changed, 435 insertions(+), 146 deletions(-) create mode 100644 phpunit.xml create mode 100644 src/Messaging/Models/BandwidthMessageItem.php create mode 100644 src/Messaging/Models/BandwidthMessagesList.php create mode 100644 src/Messaging/Models/PageInfo.php delete mode 100644 src/Voice/Models/CallEngineModifyConferenceRequest.php diff --git a/composer.json b/composer.json index 71e337c..46a90bc 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,8 @@ }, "require-dev": { "squizlabs/php_codesniffer": "^2.7", - "phan/phan": "^1.2" + "phan/phan": "^1.2", + "phpunit/phpunit": "4.8.*" }, "autoload": { "psr-4": { diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..5844324 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,20 @@ + + + + + ./tests + + + diff --git a/src/Messaging/Controllers/APIController.php b/src/Messaging/Controllers/APIController.php index 636ffd9..c6f378d 100644 --- a/src/Messaging/Controllers/APIController.php +++ b/src/Messaging/Controllers/APIController.php @@ -33,8 +33,8 @@ public function __construct($config, $httpCallBack = null) /** * listMedia * - * @param string $userId TODO: type description here - * @param string $continuationToken (optional) TODO: type description here + * @param string $userId User's account ID + * @param string $continuationToken (optional) Continuation token used to retrieve subsequent media. * @return ApiResponse response from the API call * @throws APIException Thrown if API call fails */ @@ -122,8 +122,8 @@ public function listMedia( /** * getMedia * - * @param string $userId TODO: type description here - * @param string $mediaId TODO: type description here + * @param string $userId 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 */ @@ -210,12 +210,13 @@ public function getMedia( /** * uploadMedia * - * @param string $userId TODO: type description here - * @param string $mediaId TODO: type description here - * @param integer $contentLength TODO: type description here + * @param string $userId User's account ID + * @param string $mediaId The user supplied custom media ID + * @param integer $contentLength The size of the entity-body * @param string $body TODO: type description here - * @param string $contentType (optional) Example: application/octet-stream - * @param string $cacheControl (optional) TODO: type description here + * @param string $contentType (optional) The media type of the entity-body + * @param string $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 */ @@ -233,8 +234,8 @@ public function uploadMedia( //process optional query parameters $_queryBuilder = APIHelper::appendUrlWithTemplateParameters($_queryBuilder, array ( - 'userId' => $userId, - 'mediaId' => $mediaId, + 'userId' => $userId, + 'mediaId' => $mediaId, ), false ); @@ -311,8 +312,8 @@ public function uploadMedia( /** * deleteMedia * - * @param string $userId TODO: type description here - * @param string $mediaId TODO: type description here + * @param string $userId 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 */ @@ -395,17 +396,142 @@ public function deleteMedia( return new ApiResponse($response->code, $response->headers, null); } + /** + * getMessages + * + * @param string $userId User's account ID + * @param string $messageId (optional) The ID of the message to search for. Special characters need to be + * encoded using URL encoding + * @param string $sourceTn (optional) The phone number that sent the message + * @param string $destinationTn (optional) The phone number that received the message + * @param string $messageStatus (optional) The status of the message. One of RECEIVED, QUEUED, SENDING, SENT, + * FAILED, DELIVERED, DLR_EXPIRED + * @param integer $errorCode (optional) The error code of the message + * @param string $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 $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 $pageToken (optional) A base64 encoded value used for pagination of results + * @param integer $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( + $userId, + $messageId = null, + $sourceTn = null, + $destinationTn = null, + $messageStatus = null, + $errorCode = null, + $fromDateTime = null, + $toDateTime = null, + $pageToken = null, + $limit = null + ) { + + //prepare query string for API call + $_queryBuilder = '/users/{userId}/messages'; + + //process optional query parameters + $_queryBuilder = APIHelper::appendUrlWithTemplateParameters($_queryBuilder, array ( + 'userId' => $userId, + )); + + //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); + } + /** * createMessage * - * @param string $userId TODO: type description here - * @param Models\MessageRequest $body (optional) TODO: type description here + * @param string $userId 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( $userId, - $body = null + $body ) { //prepare query string for API call diff --git a/src/Messaging/Models/BandwidthMessage.php b/src/Messaging/Models/BandwidthMessage.php index dcd6ccb..6b74e45 100644 --- a/src/Messaging/Models/BandwidthMessage.php +++ b/src/Messaging/Models/BandwidthMessage.php @@ -13,67 +13,68 @@ class BandwidthMessage implements \JsonSerializable { /** - * @todo Write general description for this property + * The id of the message * @var string|null $id public property */ public $id; /** - * @todo Write general description for this property + * The Bandwidth phone number associated with the message * @var string|null $owner public property */ public $owner; /** - * @todo Write general description for this property + * The application ID associated with the message * @var string|null $applicationId public property */ public $applicationId; /** - * @todo Write general description for this property + * The datetime stamp of the message in ISO 8601 * @var string|null $time public property */ public $time; /** - * @todo Write general description for this property + * The number of segments the original message from the user is broken into before sending over to + * carrier networks * @var integer|null $segmentCount public property */ public $segmentCount; /** - * @todo Write general description for this property + * The direction of the message relative to Bandwidth. Can be in or out * @var string|null $direction public property */ public $direction; /** - * @todo Write general description for this property + * The phone number recipients of the message * @var array|null $to public property */ public $to; /** - * @todo Write general description for this property + * The phone number the message was sent from * @var string|null $from public property */ public $from; /** - * @todo Write general description for this property + * The list of media URLs sent in the message * @var array|null $media public property */ public $media; /** - * @todo Write general description for this property + * The contents of the message * @var string|null $text public property */ public $text; /** - * @todo Write general description for this property + * The custom string set by the user * @var string|null $tag public property */ public $tag; diff --git a/src/Messaging/Models/BandwidthMessageItem.php b/src/Messaging/Models/BandwidthMessageItem.php new file mode 100644 index 0000000..4624d4b --- /dev/null +++ b/src/Messaging/Models/BandwidthMessageItem.php @@ -0,0 +1,121 @@ +messageId = func_get_arg(0); + $this->accountId = func_get_arg(1); + $this->sourceTn = func_get_arg(2); + $this->destinationTn = func_get_arg(3); + $this->messageStatus = func_get_arg(4); + $this->messageDirection = func_get_arg(5); + $this->messageType = func_get_arg(6); + $this->segmentCount = func_get_arg(7); + $this->errorCode = func_get_arg(8); + $this->receiveTime = func_get_arg(9); + $this->carrierName = func_get_arg(10); + } + } + + /** + * Encode this object to JSON + */ + public function jsonSerialize() + { + $json = array(); + $json['messageId'] = $this->messageId; + $json['accountId'] = $this->accountId; + $json['sourceTn'] = $this->sourceTn; + $json['destinationTn'] = $this->destinationTn; + $json['messageStatus'] = $this->messageStatus; + $json['messageDirection'] = $this->messageDirection; + $json['messageType'] = $this->messageType; + $json['segmentCount'] = $this->segmentCount; + $json['errorCode'] = $this->errorCode; + $json['receiveTime'] = $this->receiveTime; + $json['carrierName'] = $this->carrierName; + + return array_filter($json); + } +} diff --git a/src/Messaging/Models/BandwidthMessagesList.php b/src/Messaging/Models/BandwidthMessagesList.php new file mode 100644 index 0000000..32dadd5 --- /dev/null +++ b/src/Messaging/Models/BandwidthMessagesList.php @@ -0,0 +1,58 @@ +totalCount = func_get_arg(0); + $this->pageInfo = func_get_arg(1); + $this->messages = func_get_arg(2); + } + } + + /** + * Encode this object to JSON + */ + public function jsonSerialize() + { + $json = array(); + $json['totalCount'] = $this->totalCount; + $json['pageInfo'] = $this->pageInfo; + $json['messages'] = isset($this->messages) ? + array_values($this->messages) : null; + + return array_filter($json); + } +} diff --git a/src/Messaging/Models/Media.php b/src/Messaging/Models/Media.php index 2d50a5c..c5eeaaa 100644 --- a/src/Messaging/Models/Media.php +++ b/src/Messaging/Models/Media.php @@ -49,7 +49,7 @@ class Media implements \JsonSerializable public $tags; /** - * @todo Write general description for this property + * User's account ID * @var string|null $userId public property */ public $userId; diff --git a/src/Messaging/Models/MessageRequest.php b/src/Messaging/Models/MessageRequest.php index 5ac5d36..b75c646 100644 --- a/src/Messaging/Models/MessageRequest.php +++ b/src/Messaging/Models/MessageRequest.php @@ -13,37 +13,41 @@ class MessageRequest implements \JsonSerializable { /** - * @todo Write general description for this property - * @var string|null $applicationId public property + * The ID of the Application your from number is associated with in the Bandwidth Phone Number + * Dashboard. + * @required + * @var string $applicationId public property */ public $applicationId; /** - * @todo Write general description for this property - * @var array|null $to public property + * The phone number(s) the message should be sent to in E164 format + * @required + * @var array $to public property */ public $to; /** - * @todo Write general description for this property - * @var string|null $from public property + * One of your telephone numbers the message should come from in E164 format + * @required + * @var string $from public property */ public $from; /** - * @todo Write general description for this property + * The contents of the text message. Must be 2048 characters or less. * @var string|null $text public property */ public $text; /** - * @todo Write general description for this property + * A list of URLs to include as media attachments as part of the message. * @var array|null $media public property */ public $media; /** - * @todo Write general description for this property + * A custom string that will be included in callback events of the message. Max 1024 characters * @var string|null $tag public property */ public $tag; @@ -70,8 +74,7 @@ public function jsonSerialize() { $json = array(); $json['applicationId'] = $this->applicationId; - $json['to'] = isset($this->to) ? - array_values($this->to) : null; + $json['to'] = array_values($this->to); $json['from'] = $this->from; $json['text'] = $this->text; $json['media'] = isset($this->media) ? diff --git a/src/Messaging/Models/PageInfo.php b/src/Messaging/Models/PageInfo.php new file mode 100644 index 0000000..7adb04e --- /dev/null +++ b/src/Messaging/Models/PageInfo.php @@ -0,0 +1,65 @@ +prevPage = func_get_arg(0); + $this->nextPage = func_get_arg(1); + $this->prevPageToken = func_get_arg(2); + $this->nextPageToken = func_get_arg(3); + } + } + + /** + * Encode this object to JSON + */ + public function jsonSerialize() + { + $json = array(); + $json['prevPage'] = $this->prevPage; + $json['nextPage'] = $this->nextPage; + $json['prevPageToken'] = $this->prevPageToken; + $json['nextPageToken'] = $this->nextPageToken; + + return array_filter($json); + } +} diff --git a/src/Voice/Models/CallEngineModifyConferenceRequest.php b/src/Voice/Models/CallEngineModifyConferenceRequest.php deleted file mode 100644 index d871451..0000000 --- a/src/Voice/Models/CallEngineModifyConferenceRequest.php +++ /dev/null @@ -1,106 +0,0 @@ -status = func_get_arg(0); - $this->redirectUrl = func_get_arg(1); - $this->redirectFallbackUrl = func_get_arg(2); - $this->redirectMethod = func_get_arg(3); - $this->redirectFallbackMethod = func_get_arg(4); - $this->username = func_get_arg(5); - $this->password = func_get_arg(6); - $this->fallbackUsername = func_get_arg(7); - $this->fallbackPassword = func_get_arg(8); - } - } - - /** - * Encode this object to JSON - */ - public function jsonSerialize() - { - $json = array(); - $json['status'] = $this->status; - $json['redirectUrl'] = $this->redirectUrl; - $json['redirectFallbackUrl'] = $this->redirectFallbackUrl; - $json['redirectMethod'] = $this->redirectMethod; - $json['redirectFallbackMethod'] = $this->redirectFallbackMethod; - $json['username'] = $this->username; - $json['password'] = $this->password; - $json['fallbackUsername'] = $this->fallbackUsername; - $json['fallbackPassword'] = $this->fallbackPassword; - - return array_filter($json); - } -}