Skip to content

Commit d35eb81

Browse files
committed
Updated Clients and added endpoints
Updated Rest\Client to support all APIs. Removed Rest\AsynchClient. Added endpoints for all APIs Added functionality to select and specify endpoint.
1 parent 284c013 commit d35eb81

15 files changed

+102
-107
lines changed

README.md

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,12 @@ To achieve better performance, messages you send With MobiWeb Asynchronous SMS A
464464
$username = "";
465465
$password = "";
466466

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

469474
//Submit message
470475
$message = $client->broadcast(
@@ -491,7 +496,12 @@ print_r($message);
491496
$username = "";
492497
$password = "";
493498

494-
$client = new MobiWeb\Rest\AsynchClient($username, $password);
499+
//Endpoint Options:
500+
// Client::SMPP_API_OTP_ENDPOINT - Asynchronous SMS API (SMS) - For (one-time pins) OTP & Notification messages
501+
// Client::SMPP_API_MRK_ENDPOINT - Asynchronous SMS API (SMS) - For marketing messages
502+
$endpoint = MobiWeb\Rest\Client::SMPP_API_OTP_ENDPOINT;
503+
504+
$client = new MobiWeb\Rest\Client($username, $password, $endpoint);
495505

496506
$message = $client->broadcast(
497507
[[
@@ -523,7 +533,12 @@ print_r($message);
523533
$username = "";
524534
$password = "";
525535

526-
$client = new MobiWeb\Rest\AsynchClient($username, $password);
536+
//Endpoint Options:
537+
// Client::SMPP_API_OTP_ENDPOINT - Asynchronous SMS API (SMS) - For (one-time pins) OTP & Notification messages
538+
// Client::SMPP_API_MRK_ENDPOINT - Asynchronous SMS API (SMS) - For marketing messages
539+
$endpoint = MobiWeb\Rest\Client::SMPP_API_OTP_ENDPOINT;
540+
541+
$client = new MobiWeb\Rest\Client($username, $password, $endpoint);
527542

528543
$message = $client->broadcast(
529544
[[
@@ -557,7 +572,12 @@ print_r($message);
557572
$username = "";
558573
$password = "";
559574

560-
$client = new MobiWeb\Rest\AsynchClient($username, $password);
575+
//Endpoint Options:
576+
// Client::SMPP_API_OTP_ENDPOINT - Asynchronous SMS API (SMS) - For (one-time pins) OTP & Notification messages
577+
// Client::SMPP_API_MRK_ENDPOINT - Asynchronous SMS API (SMS) - For marketing messages
578+
$endpoint = MobiWeb\Rest\Client::SMPP_API_OTP_ENDPOINT;
579+
580+
$client = new MobiWeb\Rest\Client($username, $password, $endpoint);
561581

562582
//Get account balance and print it
563583
echo $client->getBalance();
@@ -574,10 +594,15 @@ echo $client->getBalance();
574594
$username = "";
575595
$password = "";
576596

577-
$client = new MobiWeb\Rest\AsynchClient($username, $password);
597+
//Endpoint Options:
598+
// Client::SMPP_API_OTP_ENDPOINT - Asynchronous SMS API (SMS) - For (one-time pins) OTP & Notification messages
599+
// Client::SMPP_API_MRK_ENDPOINT - Asynchronous SMS API (SMS) - For marketing messages
600+
$endpoint = MobiWeb\Rest\Client::SMPP_API_OTP_ENDPOINT;
601+
602+
$client = new MobiWeb\Rest\Client($username, $password, $endpoint);
578603

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

582607
?>
583608
```

composer.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "mobiweb/sdk",
3-
"description": "4.0.0 PHP library for communicating with the MobiWeb REST APIs.",
3+
"description": "4.0.2 PHP library for communicating with the MobiWeb REST APIs.",
44
"minimum-stability": "stable",
55
"keywords": [
6-
"sms", "api", "mobiweb", "php", "messaging", "hlr", "hlr lookup", "mobile lookup", "mobile number lookup", "OTP", "one time password", "2FA", "two factor authentication", "smpp", "asynch"
6+
"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"
77
],
88
"type": "library",
99
"license": "MIT",
@@ -14,7 +14,9 @@
1414
"homepage": "https://www.solutions4mobiles.com"
1515
}],
1616
"require": {
17-
"php": ">=7.0.0"
17+
"php": ">=7.0.0",
18+
"ext-curl": "*",
19+
"ext-json": "*"
1820
},
1921
"support": {
2022
"issues": "https://github.com/mobiweb/mobiweb-php/issues",

src/MobiWeb/Http/Client.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace MobiWeb\Http;
44

5+
use \stdClass;
6+
57
class Client {
68

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

2931
}
3032

31-
public function request(string $url = null, string $method = null, array $headers = array(), $data = null): object{
33+
public function request(string $url = null, string $method = null, array $headers = array(), $data = null): stdClass{
3234

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

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

137138
}
138139

139-
}
140-
141-
?>
140+
}

src/MobiWeb/Rest/AsynchClient.php

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/MobiWeb/Rest/Authentication.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public function authenticate() :bool{
4848
if($executedRequest->response->body->status_code != HttpClient::HTTP_OK){
4949
$apiError = new APIError($executedRequest->response->body->status_code, $executedRequest->response->body->status_message, $executedRequest->response->body->errors);
5050
throw new \Exception($apiError->print());
51-
return false;
5251
}
5352
$this->timestamp = new \DateTime();
5453
$this->access_token = $executedRequest->response->body->payload->access_token;
@@ -62,7 +61,6 @@ public function refresh() :bool{
6261

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

6866
$http = new HttpClient();
@@ -75,7 +73,6 @@ public function refresh() :bool{
7573
if($executedRequest->response->body->status_code != HttpClient::HTTP_OK){
7674
$apiError = new APIError($executedRequest->response->body->status_code, $executedRequest->response->body->status_message, $executedRequest->response->body->errors);
7775
throw new \Exception($apiError->print());
78-
return false;
7976
}
8077
$this->timestamp = new \DateTime();
8178
$this->access_token = $executedRequest->response->body->payload->access_token;
@@ -110,7 +107,6 @@ public function isAuthenticated() :bool{
110107
if(($interval->s < Authentication::VALIDITY_PERIOD) && ($interval->s >= (Authentication::VALIDITY_PERIOD - Authentication::VALIDITY_THRESHOLD))){
111108
if(!$this->refresh()){
112109
throw new \Exception("Refresh connection failed");
113-
return false;
114110
}
115111
}
116112

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

120116
}
121117

122-
}
123-
124-
?>
118+
}

src/MobiWeb/Rest/Client.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,28 @@
1111
class Client {
1212

1313
protected $auth;
14-
const API_ENDPOINT = "https://sms.solutions4mobiles.com/apis";
14+
protected $endpoint;
15+
const API_ENDPOINT = "https://sms.solutions4mobiles.com/apis";
16+
const SMPP_API_OTP_ENDPOINT = "https://apix.solutions4mobiles.com/apis";
17+
const SMPP_API_MRK_ENDPOINT = "https://apix2.solutions4mobiles.com/apis";
1518
const HLR = "hlr";
1619
const SMS = "sms";
1720
const OTP = "otp";
1821

1922

20-
public function __construct(string $username = null, string $password = null){
23+
public function __construct(string $username = null, string $password = null, string $endpoint = Client::API_ENDPOINT){
2124

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

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

34+
$this->endpoint = $endpoint;
35+
3136
}
3237

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

54+
if($this->endpoint != Client::API_ENDPOINT) {
55+
throw new \Exception("Unsupported service for selected endpoint");
56+
}
57+
4958
return OTP::generate($this->auth, $mobile, $sender, $message, $validity);
5059

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

68+
if($this->endpoint != Client::API_ENDPOINT) {
69+
throw new \Exception("Unsupported service for selected endpoint");
70+
}
71+
5972
return OTP::validate($this->auth, $id, $mobile, $pin);
6073

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

82+
if($this->endpoint != Client::API_ENDPOINT) {
83+
throw new \Exception("Unsupported service for selected endpoint");
84+
}
85+
6986
return HLR::lookup($this->auth, $mobile);
7087

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

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

98+
if($this->endpoint != Client::API_ENDPOINT && $service != Client::SMS) {
99+
throw new \Exception("Unsupported service for selected endpoint");
100+
}
101+
81102
return Util::getPricing($this->auth,$service);
82103

83104
}
84105

85-
}
86-
87-
?>
106+
}

src/MobiWeb/Rest/Error.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,4 @@ public function print(){
2727
return true;
2828
}
2929

30-
}
31-
32-
?>
30+
}

src/MobiWeb/Rest/HLR.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public static function lookup(Auth $auth = null, string $mobile): array{
2222
$access_token = $auth->getAccessToken();
2323
if(!$access_token){
2424
throw new \Exception("Cannot retrieve Access Token");
25-
return false;
2625
}
2726

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

4139
return array($executedRequest->response->body->payload);

src/MobiWeb/Rest/Message.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public static function broadcast(Auth $auth = null, array $args): array{
2222
$access_token = $auth->getAccessToken();
2323
if(!$access_token){
2424
throw new \Exception("Cannot retrieve Access Token");
25-
return false;
2625
}
2726

2827
$endpoint = $auth->getEndPoint();

src/MobiWeb/Rest/OTP.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class OTP {
1313
const VALIDATE_ENDPOINT = "/otp/v3/validate/";
1414
const OTP_METHOD = "POST";
1515

16-
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){
16+
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{
1717

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

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

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

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

7571
return true;

0 commit comments

Comments
 (0)