diff --git a/composer.json b/composer.json index 8cb6822..425d8cd 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ }, "autoload": { "psr-4": { - "FlowrouteNumbersLib\\": "src/" + "FlowrouteNumbersLib\\": "src/FlowrouteNumbers/" } } -} \ No newline at end of file +} diff --git a/demo.php b/demo.php index 7c3d55e..55fc305 100644 --- a/demo.php +++ b/demo.php @@ -4,9 +4,6 @@ error_reporting(E_ALL); require_once('vendor/autoload.php'); -foreach (glob('src/*.php') as $filename){require_once $filename;} -foreach (glob('src/Controllers/*.php') as $filename){require_once $filename;} -foreach (glob('src/Models/*.php') as $filename){require_once $filename;} use FlowrouteNumbersLib\Controllers\InboundRoutesController; use FlowrouteNumbersLib\Controllers\PurchasablePhoneNumbersController; @@ -16,9 +13,15 @@ print "Number Control Demo." . PHP_EOL; -//--- Purchasable Phone Numbers -// Create our controller -$pnc = new PurchasablePhoneNumbersController(); +// Flowroute API Access Key and Secret Key +$access_key = '78675316'; +$secret_key = 'i0WRu1eHXolvfTGv1RIIsrkRfVGJLn0Q'; + +// Create our controllers +$pnc = new PurchasablePhoneNumbersController($access_key, $secret_key); +$tnc = new TelephoneNumbersController($access_key, $secret_key); +$irc = new InboundRoutesController($access_key, $secret_key); + // Retrieve Available NPAs print("--Retrieve Available NPAs\n"); @@ -27,87 +30,37 @@ // Retrieve Available NPA NXXs print("--Retrieve Available NPA NXXs\n"); -$response = $pnc->listAreaAndExchange(); -print_r($response); +try { + $response = $pnc->listAreaAndExchange(); + print_r($response); +} catch(APIException $e) { + print("Error - " . strval($e->getResponseCode()) . ' ' . $e->getMessage() . PHP_EOL); +} // Search for purchasable Numbers print("--Search for numbers in Seattle Washington\n"); -$response = $pnc->search(10,206,641,null,'seattle','wa',null); -print_r($response); - - -//--- Telephone Numbers -// Create our controller -$tnc = new TelephoneNumbersController(); - -// Purchase a Phone Number -print("--Purchase a Phone Number\n"); -$billing = new BillingMethod('METERED'); - -$number = '12066417661'; - try { - $response = $tnc->purchase($billing, $number); - print_r($response); + $response = $pnc->search(10,206,641,null,'seattle','wa',null); + print_r($response); } catch(APIException $e) { - print("Error - " . strval($e->getResponseCode()) . ' ' . $e->getMessage() . PHP_EOL); + print("Error - " . strval($e->getResponseCode()) . ' ' . $e->getMessage() . PHP_EOL); } // List Account Phone Numbers print("--List Account Phone Numbers\n"); try { - $response = $tnc->listAccountTelephoneNumbers(); - print_r($response); + $response = $tnc->listAccountTelephoneNumbers(); + print_r($response); } catch(APIException $e) { - print("Error - " . strval($e->getResponseCode()) . ' ' . $e->getMessage() . PHP_EOL); + print("Error - " . strval($e->getResponseCode()) . ' ' . $e->getMessage() . PHP_EOL); } -// Retrieve Phone Number Details -print("--Retrieve Number Details\n"); -try { - $response = $tnc->telephoneNumberDetails($number); - print_r($response); -} catch(APIException $e) { - print("Error - " . strval($e->getResponseCode()) . ' ' . $e->getMessage() . PHP_EOL); -} - - -//--- Inbound Routes -// Create our controller -$inbound = new InboundRoutesController(); - // Retrieve Routes print("--Retrieve Inbound Routes\n"); try { - $response = $inbound->mlist(); - print_r($response); -} catch(APIException $e) { - print("Error - " . strval($e->getResponseCode()) . ' ' . $e->getMessage() . PHP_EOL); -} - -// Create Route -print("--Create New Inbound Route\n"); -try { - $routename = 'PSTNroute1'; - $routetype = 'PSTN'; - $routevalue = '12065551212'; - $response = $inbound->createNewRoute($routename, $routetype, $routevalue); - print_r("New Inbound Route Successfully Created\n"); + $response = $irc->mlist(); + print_r($response); } catch(APIException $e) { - print("Error - " . strval($e->getResponseCode()) . ' ' . $e->getMessage() . PHP_EOL); + print("Error - " . strval($e->getResponseCode()) . ' ' . $e->getMessage() . PHP_EOL); } -//update routes -print("--Update TN's Primary and Failover Route\n"); -try { - $rtes = '{"routes": [{"name": "PSTNroute1"}, {"name": "PSTNroute1"}]}'; - $tn = "19515551212"; - $response = $tnc->update($tn, $rtes); - print_r($response); -} catch(APIException $e) { - print("Error - " . strval($e->getResponseCode()) . ' ' . $e->getMessage() . PHP_EOL); -} - - - - diff --git a/src/FlowrouteNumbers.php b/src/FlowrouteNumbers.php new file mode 100644 index 0000000..8c0ec70 --- /dev/null +++ b/src/FlowrouteNumbers.php @@ -0,0 +1,11 @@ + diff --git a/src/APIException.php b/src/FlowrouteNumbers/APIException.php similarity index 100% rename from src/APIException.php rename to src/FlowrouteNumbers/APIException.php diff --git a/src/APIHelper.php b/src/FlowrouteNumbers/APIHelper.php similarity index 100% rename from src/APIHelper.php rename to src/FlowrouteNumbers/APIHelper.php diff --git a/src/Configuration.php b/src/FlowrouteNumbers/Configuration.php similarity index 100% rename from src/Configuration.php rename to src/FlowrouteNumbers/Configuration.php diff --git a/src/Controllers/InboundRoutesController.php b/src/FlowrouteNumbers/Controllers/InboundRoutesController.php similarity index 91% rename from src/Controllers/InboundRoutesController.php rename to src/FlowrouteNumbers/Controllers/InboundRoutesController.php index ab46b38..b391ec3 100644 --- a/src/Controllers/InboundRoutesController.php +++ b/src/FlowrouteNumbers/Controllers/InboundRoutesController.php @@ -11,7 +11,6 @@ use FlowrouteNumbersLib\APIHelper; use FlowrouteNumbersLib\Configuration; use FlowrouteNumbersLib\CustomAuthUtility; -use Unirest\Unirest; class InboundRoutesController { @@ -34,8 +33,8 @@ class InboundRoutesController { */ function __construct($username=null, $password=null) { - $this->username = $username ? $username : Configuration::$username; - $this->password = $password ? $password : Configuration::$password; + $this->username = $username; + $this->password = $password; } /** @@ -72,7 +71,7 @@ public function mlist ( //append custom auth authorization headers $response = CustomAuthUtility::appendCustomAuthParams('GET', - $queryUrl, $headers); + $queryUrl, $headers, '', $this->username, $this->password); //Error handling using HTTP status codes if ($response->code == 401) { @@ -126,7 +125,7 @@ public function createNewRoute ( //and invoke the API call request to fetch the response $response = CustomAuthUtility::appendCustomAuthParams('PUT', - $queryUrl, $headers, $body); + $queryUrl, $headers, $body, $this->username, $this->password); //Error handling using HTTP status codes if ($response->code == 400) { @@ -144,4 +143,4 @@ public function createNewRoute ( return $response->body; } -} \ No newline at end of file +} diff --git a/src/Controllers/PurchasablePhoneNumbersController.php b/src/FlowrouteNumbers/Controllers/PurchasablePhoneNumbersController.php similarity index 93% rename from src/Controllers/PurchasablePhoneNumbersController.php rename to src/FlowrouteNumbers/Controllers/PurchasablePhoneNumbersController.php index 1e817a1..4bb3133 100644 --- a/src/Controllers/PurchasablePhoneNumbersController.php +++ b/src/FlowrouteNumbers/Controllers/PurchasablePhoneNumbersController.php @@ -11,7 +11,6 @@ use FlowrouteNumbersLib\APIHelper; use FlowrouteNumbersLib\Configuration; use FlowrouteNumbersLib\CustomAuthUtility; -use Unirest\Unirest; class PurchasablePhoneNumbersController { @@ -34,8 +33,8 @@ class PurchasablePhoneNumbersController { */ function __construct($username=null, $password=null) { - $this->username = $username ? $username : Configuration::$username; - $this->password = $password ? $password : Configuration::$password; + $this->username = $username; + $this->password = $password; } /** @@ -74,7 +73,7 @@ public function listAreaAndExchange ( ); $response = CustomAuthUtility::appendCustomAuthParams('GET', - $queryUrl, $headers); + $queryUrl, $headers, '', $this->username, $this->password); //Error handling using HTTP status codes if ($response->code == 400) { @@ -120,9 +119,8 @@ public function listAvailableNPAs ( 'user-agent' => 'Flowroute SDK 1.0', 'Accept' => 'application/json' ); - $response = CustomAuthUtility::appendCustomAuthParams('GET', - $queryUrl, $headers); + $queryUrl, $headers, '', $this->username, $this->password); //Error handling using HTTP status codes if ($response->code == 400) { @@ -188,7 +186,7 @@ public function search ( ); $response = CustomAuthUtility::appendCustomAuthParams('GET', - $queryUrl, $headers); + $queryUrl, $headers, '', $this->username, $this->password); //Error handling using HTTP status codes if ($response->code == 400) { @@ -206,4 +204,4 @@ public function search ( return $response->body; } -} \ No newline at end of file +} diff --git a/src/Controllers/TelephoneNumbersController.php b/src/FlowrouteNumbers/Controllers/TelephoneNumbersController.php similarity index 87% rename from src/Controllers/TelephoneNumbersController.php rename to src/FlowrouteNumbers/Controllers/TelephoneNumbersController.php index 706512c..9c1aaa2 100644 --- a/src/Controllers/TelephoneNumbersController.php +++ b/src/FlowrouteNumbers/Controllers/TelephoneNumbersController.php @@ -12,7 +12,6 @@ use FlowrouteNumbersLib\Configuration; use FlowrouteNumbersLib\CustomAuthUtility; use FlowrouteNumbersLib\Models\BillingMethod; -use Unirest\Unirest; class TelephoneNumbersController { @@ -35,8 +34,8 @@ class TelephoneNumbersController { */ function __construct($username=null, $password=null) { - $this->username = $username ? $username : Configuration::$username; - $this->password = $password ? $password : Configuration::$password; + $this->username = $username; + $this->password = $password; } /** @@ -68,7 +67,7 @@ public function telephoneNumberDetails ( ); $response = CustomAuthUtility::appendCustomAuthParams('GET', - $queryUrl, $headers); + $queryUrl, $headers, '', $this->username, $this->password); //Error handling using HTTP status codes if ($response->code == 400) { @@ -118,7 +117,7 @@ public function purchase ( ); $response = CustomAuthUtility::appendCustomAuthParams('PUT', - $queryUrl, $headers, $billing->jsonSerialize()); + $queryUrl, $headers, $billing->jsonSerialize(), $this->username, $this->password); print_r($response); @@ -174,7 +173,7 @@ public function listAccountTelephoneNumbers ( ); $response = CustomAuthUtility::appendCustomAuthParams('GET', - $queryUrl, $headers); + $queryUrl, $headers, '', $this->username, $this->password); //Error handling using HTTP status codes if ($response->code == 400) { @@ -194,14 +193,16 @@ public function listAccountTelephoneNumbers ( /** * Updates the routing information for a telephone number on your account, as indicated by the specified URI. The body of the request requires two routes listed in order of preference (primary first and fail over second). - * @param string $number Required parameter: The telephone number who's routing you wish to update - * @param array $routes Required parameter: JSON string containing the The routes obtained from the routes resource that you would like to point your telephone number to. + * @param string $number Required parameter: The telephone number who's routing you wish to update + * @param string $primary_route Required parameter: Name of the preffered primary route + * @param string $secondary_route Required parameter: Name of the preffered failover route * @return string response from the API call * @throws APIException **/ public function update ( $number, - $routes) + $primary_route, + $secondary_route) { //the base uri for api requests $queryBuilder = Configuration::$BASEURI; @@ -222,9 +223,11 @@ public function update ( 'user-agent' => 'Flowroute SDK 1.0', 'content-type' => 'application/json; charset=utf-8' ); - + + $body = '{"routes": [{"name": "' . $primary_route . '"}, {"name": "' . $secondary_route . '"}]}'; + $response = CustomAuthUtility::appendCustomAuthParams('PATCH', - $queryUrl, $headers, $routes); + $queryUrl, $headers, $body, $this->username, $this->password); //Error handling using HTTP status codes if ($response->code == 400) { @@ -241,5 +244,4 @@ public function update ( return $response->body; } - -} \ No newline at end of file +} diff --git a/src/CustomAuthUtility.php b/src/FlowrouteNumbers/CustomAuthUtility.php similarity index 79% rename from src/CustomAuthUtility.php rename to src/FlowrouteNumbers/CustomAuthUtility.php index 124f61b..8fb75a8 100644 --- a/src/CustomAuthUtility.php +++ b/src/FlowrouteNumbers/CustomAuthUtility.php @@ -15,16 +15,22 @@ class CustomAuthUtility { * @param string $query_url The url to send the query to * @param array $headers Header array to send in the request * @param string $body Message body to send + * @param string $username API access key + * @param string $password API secret key * @return response from HTTP request */ public static function appendCustomAuthParams( $method = 'GET', $query_url=NULL, $headers=array(), - $body='') + $body='', + $username=NULL, + $password=NULL) { - if (!isset($query_url)) { $query_url = Configuration::$BASEURI; } + if (!isset($username)) { $username = Configuration::$username; } + if (!isset($password)) { $password = Configuration::$password; } + $timestamp = gmdate('Y-m-d\TH:i:s'); $headers['X-Timestamp'] = $timestamp; $parsedurl = parse_url($query_url); @@ -45,23 +51,23 @@ public static function appendCustomAuthParams( $message_string .= $method . PHP_EOL; $message_string .= $body_md5 . PHP_EOL . $canonicalUri; $message_string = utf8_encode($message_string); - $signature = hash_hmac('sha1', $message_string, Configuration::$password); + $signature = hash_hmac('sha1', $message_string, $password); switch (strtoupper($method)) { case 'GET': - $request = Unirest::get($query_url, $headers, NULL, Configuration::$username, $signature); + $request = Unirest::get($query_url, $headers, NULL, $username, $signature); break; case 'POST': - $request = Unirest::post($query_url, $headers, $body, Configuration::$username, $signature); + $request = Unirest::post($query_url, $headers, $body, $username, $signature); break; case 'PUT': - $request = Unirest::put($query_url, $headers, $body, Configuration::$username, $signature); + $request = Unirest::put($query_url, $headers, $body, $username, $signature); break; case 'PATCH': - $request = Unirest::patch($query_url, $headers, $body, Configuration::$username, $signature); + $request = Unirest::patch($query_url, $headers, $body, $username, $signature); break; case 'DELETE': - $request = Unirest::delete($query_url, $headers, $body, Configuration::$username, $signature); + $request = Unirest::delete($query_url, $headers, $body, $username, $signature); break; default: trigger_error("Invalid method supplied.", E_USER_ERROR); @@ -72,4 +78,4 @@ public static function appendCustomAuthParams( $response = Unirest::getResponse($request); return $response; } -} \ No newline at end of file +} diff --git a/src/Models/BillingMethod.php b/src/FlowrouteNumbers/Models/BillingMethod.php similarity index 100% rename from src/Models/BillingMethod.php rename to src/FlowrouteNumbers/Models/BillingMethod.php diff --git a/src/Models/Route.php b/src/FlowrouteNumbers/Models/Route.php similarity index 100% rename from src/Models/Route.php rename to src/FlowrouteNumbers/Models/Route.php