Skip to content

Commit

Permalink
Updated Clients and added endpoints
Browse files Browse the repository at this point in the history
Updated Rest\Client to support all APIs.
Removed Rest\AsynchClient.
Added endpoints for all APIs
Added functionality to select and specify endpoint.
  • Loading branch information
Beliam committed Jun 21, 2024
1 parent 284c013 commit d35eb81
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 107 deletions.
37 changes: 31 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,12 @@ To achieve better performance, messages you send With MobiWeb Asynchronous SMS A
$username = "";
$password = "";

$client = new MobiWeb\Rest\AsynchClient($username, $password);
//Endpoint Options:
// Client::SMPP_API_OTP_ENDPOINT - Asynchronous SMS API (SMS) - For (one-time pins) OTP & Notification messages
// Client::SMPP_API_MRK_ENDPOINT - Asynchronous SMS API (SMS) - For marketing messages
$endpoint = MobiWeb\Rest\Client::SMPP_API_OTP_ENDPOINT;

$client = new MobiWeb\Rest\Client($username, $password, $endpoint);

//Submit message
$message = $client->broadcast(
Expand All @@ -491,7 +496,12 @@ print_r($message);
$username = "";
$password = "";

$client = new MobiWeb\Rest\AsynchClient($username, $password);
//Endpoint Options:
// Client::SMPP_API_OTP_ENDPOINT - Asynchronous SMS API (SMS) - For (one-time pins) OTP & Notification messages
// Client::SMPP_API_MRK_ENDPOINT - Asynchronous SMS API (SMS) - For marketing messages
$endpoint = MobiWeb\Rest\Client::SMPP_API_OTP_ENDPOINT;

$client = new MobiWeb\Rest\Client($username, $password, $endpoint);

$message = $client->broadcast(
[[
Expand Down Expand Up @@ -523,7 +533,12 @@ print_r($message);
$username = "";
$password = "";

$client = new MobiWeb\Rest\AsynchClient($username, $password);
//Endpoint Options:
// Client::SMPP_API_OTP_ENDPOINT - Asynchronous SMS API (SMS) - For (one-time pins) OTP & Notification messages
// Client::SMPP_API_MRK_ENDPOINT - Asynchronous SMS API (SMS) - For marketing messages
$endpoint = MobiWeb\Rest\Client::SMPP_API_OTP_ENDPOINT;

$client = new MobiWeb\Rest\Client($username, $password, $endpoint);

$message = $client->broadcast(
[[
Expand Down Expand Up @@ -557,7 +572,12 @@ print_r($message);
$username = "";
$password = "";

$client = new MobiWeb\Rest\AsynchClient($username, $password);
//Endpoint Options:
// Client::SMPP_API_OTP_ENDPOINT - Asynchronous SMS API (SMS) - For (one-time pins) OTP & Notification messages
// Client::SMPP_API_MRK_ENDPOINT - Asynchronous SMS API (SMS) - For marketing messages
$endpoint = MobiWeb\Rest\Client::SMPP_API_OTP_ENDPOINT;

$client = new MobiWeb\Rest\Client($username, $password, $endpoint);

//Get account balance and print it
echo $client->getBalance();
Expand All @@ -574,10 +594,15 @@ echo $client->getBalance();
$username = "";
$password = "";

$client = new MobiWeb\Rest\AsynchClient($username, $password);
//Endpoint Options:
// Client::SMPP_API_OTP_ENDPOINT - Asynchronous SMS API (SMS) - For (one-time pins) OTP & Notification messages
// Client::SMPP_API_MRK_ENDPOINT - Asynchronous SMS API (SMS) - For marketing messages
$endpoint = MobiWeb\Rest\Client::SMPP_API_OTP_ENDPOINT;

$client = new MobiWeb\Rest\Client($username, $password, $endpoint);

//Get account pricing and print it
print_r($client->getPricing(MobiWeb\Rest\AsynchClient::SMS));
print_r($client->getPricing(MobiWeb\Rest\Client::SMS));

?>
```
Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "mobiweb/sdk",
"description": "4.0.0 PHP library for communicating with the MobiWeb REST APIs.",
"description": "4.0.2 PHP library for communicating with the MobiWeb REST APIs.",
"minimum-stability": "stable",
"keywords": [
"sms", "api", "mobiweb", "php", "messaging", "hlr", "hlr lookup", "mobile lookup", "mobile number lookup", "OTP", "one time password", "2FA", "two factor authentication", "smpp", "asynch"
"sms", "api", "mobiweb", "php", "messaging", "hlr", "hlr lookup", "mobile lookup", "mobile number lookup", "OTP", "one time password", "2FA", "two factor authentication", "smpp api", "smpp", "asynch"
],
"type": "library",
"license": "MIT",
Expand All @@ -14,7 +14,9 @@
"homepage": "https://www.solutions4mobiles.com"
}],
"require": {
"php": ">=7.0.0"
"php": ">=7.0.0",
"ext-curl": "*",
"ext-json": "*"
},
"support": {
"issues": "https://github.com/mobiweb/mobiweb-php/issues",
Expand Down
9 changes: 4 additions & 5 deletions src/MobiWeb/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace MobiWeb\Http;

use \stdClass;

class Client {

const GET = "GET";
Expand All @@ -28,7 +30,7 @@ public function __construct(){

}

public function request(string $url = null, string $method = null, array $headers = array(), $data = null): object{
public function request(string $url = null, string $method = null, array $headers = array(), $data = null): stdClass{

if(!$url){
throw new \Exception("Invalid URL: ". $url);
Expand All @@ -47,7 +49,6 @@ public function request(string $url = null, string $method = null, array $header
break;
default:
throw new \Exception("Unsupported HTTP Method: ". $method);
break;
}

$options[CURLOPT_URL] = $url;
Expand Down Expand Up @@ -136,6 +137,4 @@ protected static function getResponseBody(string $response = null, string $heade

}

}

?>
}
53 changes: 0 additions & 53 deletions src/MobiWeb/Rest/AsynchClient.php

This file was deleted.

8 changes: 1 addition & 7 deletions src/MobiWeb/Rest/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public function authenticate() :bool{
if($executedRequest->response->body->status_code != HttpClient::HTTP_OK){
$apiError = new APIError($executedRequest->response->body->status_code, $executedRequest->response->body->status_message, $executedRequest->response->body->errors);
throw new \Exception($apiError->print());
return false;
}
$this->timestamp = new \DateTime();
$this->access_token = $executedRequest->response->body->payload->access_token;
Expand All @@ -62,7 +61,6 @@ public function refresh() :bool{

if(!$this->refresh_token){
throw new \Exception("Refresh_token is required to refresh connection");
return false;
}

$http = new HttpClient();
Expand All @@ -75,7 +73,6 @@ public function refresh() :bool{
if($executedRequest->response->body->status_code != HttpClient::HTTP_OK){
$apiError = new APIError($executedRequest->response->body->status_code, $executedRequest->response->body->status_message, $executedRequest->response->body->errors);
throw new \Exception($apiError->print());
return false;
}
$this->timestamp = new \DateTime();
$this->access_token = $executedRequest->response->body->payload->access_token;
Expand Down Expand Up @@ -110,7 +107,6 @@ public function isAuthenticated() :bool{
if(($interval->s < Authentication::VALIDITY_PERIOD) && ($interval->s >= (Authentication::VALIDITY_PERIOD - Authentication::VALIDITY_THRESHOLD))){
if(!$this->refresh()){
throw new \Exception("Refresh connection failed");
return false;
}
}

Expand All @@ -119,6 +115,4 @@ public function isAuthenticated() :bool{

}

}

?>
}
31 changes: 25 additions & 6 deletions src/MobiWeb/Rest/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,28 @@
class Client {

protected $auth;
const API_ENDPOINT = "https://sms.solutions4mobiles.com/apis";
protected $endpoint;
const API_ENDPOINT = "https://sms.solutions4mobiles.com/apis";
const SMPP_API_OTP_ENDPOINT = "https://apix.solutions4mobiles.com/apis";
const SMPP_API_MRK_ENDPOINT = "https://apix2.solutions4mobiles.com/apis";
const HLR = "hlr";
const SMS = "sms";
const OTP = "otp";


public function __construct(string $username = null, string $password = null){
public function __construct(string $username = null, string $password = null, string $endpoint = Client::API_ENDPOINT){

if (!$username || !$password) {
throw new \Exception("Username and Password are required to create a Client");
}

$this->auth = new Auth($username,$password,Client::API_ENDPOINT);
$this->auth = new Auth($username,$password,$endpoint);
if(!$this->auth->authenticate()){
throw new \Exception("Authentication failed");
}

$this->endpoint = $endpoint;

}

public function broadcast(array $args): array{
Expand All @@ -46,6 +51,10 @@ public function generate(string $mobile, string $sender, string $message, int $v
throw new \Exception("Mobile number is required to generate an OTP");
}

if($this->endpoint != Client::API_ENDPOINT) {
throw new \Exception("Unsupported service for selected endpoint");
}

return OTP::generate($this->auth, $mobile, $sender, $message, $validity);

}
Expand All @@ -56,6 +65,10 @@ public function validate(string $id, string $mobile, string $pin): bool{
throw new \Exception("Mobile number, OTP pin and OTP ID is required to validate an OTP");
}

if($this->endpoint != Client::API_ENDPOINT) {
throw new \Exception("Unsupported service for selected endpoint");
}

return OTP::validate($this->auth, $id, $mobile, $pin);

}
Expand All @@ -66,6 +79,10 @@ public function lookup(string $mobile): array{
throw new \Exception("Mobile number is required to make a HLR Lookup");
}

if($this->endpoint != Client::API_ENDPOINT) {
throw new \Exception("Unsupported service for selected endpoint");
}

return HLR::lookup($this->auth, $mobile);

}
Expand All @@ -78,10 +95,12 @@ public function getBalance(): float{

public function getPricing(string $service=Client::SMS): array{

if($this->endpoint != Client::API_ENDPOINT && $service != Client::SMS) {
throw new \Exception("Unsupported service for selected endpoint");
}

return Util::getPricing($this->auth,$service);

}

}

?>
}
4 changes: 1 addition & 3 deletions src/MobiWeb/Rest/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,4 @@ public function print(){
return true;
}

}

?>
}
2 changes: 0 additions & 2 deletions src/MobiWeb/Rest/HLR.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public static function lookup(Auth $auth = null, string $mobile): array{
$access_token = $auth->getAccessToken();
if(!$access_token){
throw new \Exception("Cannot retrieve Access Token");
return false;
}

$http = new HttpClient();
Expand All @@ -35,7 +34,6 @@ public static function lookup(Auth $auth = null, string $mobile): array{
if($executedRequest->response->body->status_code != HttpClient::HTTP_OK){
$apiError = new APIError($executedRequest->response->body->status_code, $executedRequest->response->body->status_message, $executedRequest->response->body->payload->error);
throw new \Exception($apiError->print());
return false;
}

return array($executedRequest->response->body->payload);
Expand Down
1 change: 0 additions & 1 deletion src/MobiWeb/Rest/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public static function broadcast(Auth $auth = null, array $args): array{
$access_token = $auth->getAccessToken();
if(!$access_token){
throw new \Exception("Cannot retrieve Access Token");
return false;
}

$endpoint = $auth->getEndPoint();
Expand Down
6 changes: 1 addition & 5 deletions src/MobiWeb/Rest/OTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class OTP {
const VALIDATE_ENDPOINT = "/otp/v3/validate/";
const OTP_METHOD = "POST";

public static function generate(Auth $auth = null, string $mobile, string $sender = "SECUREPIN", string $message = "Please do not share your password pin. Your password pin is: [PIN]", int $validity = 600){
public static function generate(Auth $auth = null, string $mobile, string $sender = "SECUREPIN", string $message = "Please do not share your password pin. Your password pin is: [PIN]", int $validity = 600): array{

if (!$auth) {
throw new \Exception("Cannot generate OTP without authentication");
Expand All @@ -22,7 +22,6 @@ public static function generate(Auth $auth = null, string $mobile, string $sende
$access_token = $auth->getAccessToken();
if(!$access_token){
throw new \Exception("Cannot retrieve Access Token");
return false;
}

$http = new HttpClient();
Expand All @@ -39,7 +38,6 @@ public static function generate(Auth $auth = null, string $mobile, string $sende
if($executedRequest->response->body->status_code != HttpClient::HTTP_CREATED){
$apiError = new APIError($executedRequest->response->body->status_code, $executedRequest->response->body->status_message, $executedRequest->response->body->errors);
throw new \Exception($apiError->print());
return false;
}

return array($executedRequest->response->body->payload);
Expand All @@ -54,7 +52,6 @@ public static function validate(Auth $auth = null, string $id, string $mobile, s
$access_token = $auth->getAccessToken();
if(!$access_token){
throw new \Exception("Cannot retrieve Access Token");
return false;
}

$http = new HttpClient();
Expand All @@ -69,7 +66,6 @@ public static function validate(Auth $auth = null, string $id, string $mobile, s
if($executedRequest->response->body->status_code != HttpClient::HTTP_OK){
$apiError = new APIError($executedRequest->response->body->status_code, $executedRequest->response->body->status_message, $executedRequest->response->body->errors);
throw new \Exception($apiError->print());
return false;
}

return true;
Expand Down
Loading

0 comments on commit d35eb81

Please sign in to comment.