Skip to content

Commit de50e50

Browse files
committed
added a way to change environment
1 parent ada40bb commit de50e50

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

nextsms/__init__.py

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,67 @@
11
import sys
22
import requests
33
from base64 import b64encode
4-
from typing import List, Dict, Union
54
from collections import namedtuple
5+
from typing import List, Dict, Union, Iterable
66

77

88
class NextSms(object):
99

10-
BASE_URL_SINGLE = 'https://messaging-service.co.tz/api/sms/v1/test/text/single'
11-
BASE_URL_MULTIPLE = 'https://messaging-service.co.tz/api/sms/v1/test/text/multi'
10+
SANDBOX_URL_SINGLE = 'https://messaging-service.co.tz/api/sms/v1/test/text/single'
11+
SANDBOX_URL_MULTIPLE = 'https://messaging-service.co.tz/api/sms/v1/test/text/multi'
12+
PRODUCTION_URL_SINGLE = 'https://messaging-service.co.tz/api/sms/v1/text/single'
13+
PRODUCTION_URL_MULTIPLE = 'https://messaging-service.co.tz/api/sms/v1/text/multi'
1214

15+
_sandbox = False
1316
User = namedtuple('User', 'username password secret_key')
1417

15-
def __init__(self, username: str = '', password: str = '') -> None:
18+
def __init__(self, username: str = '', password: str = '', sandbox=False) -> None:
1619
"""Initialize nextsms access credentials
1720
1821
Args:
1922
username (str): your username for nextsms
2023
password (str): your login password for nextsms
2124
"""
25+
self.sandbox = sandbox
2226
self._user = (None
2327
if not all(username and password)
2428
else self.create_user(username, password))
2529

30+
@property
31+
def sandbox(self) -> bool:
32+
"""Return the state of the sandbox environment whether its active or inactive
33+
34+
Returns:
35+
bool: True if sandbox environment is active, False if not
36+
"""
37+
return self._sandbox
38+
39+
@sandbox.setter
40+
def sandbox(self, is_active: bool) -> None:
41+
"""Set sandbox environment to active or inactive
42+
43+
Args:
44+
is_active (bool): State of the sandbox environment (True|False)
45+
46+
Raises:
47+
TypeError: If is_active is not of <class 'bool'>
48+
49+
Example:
50+
>> import nextsms
51+
>> sender = nextsms('KalebuJordan', 'kalebu@opensource')
52+
>> sender.sandbox = True
53+
"""
54+
if not isinstance(is_active, bool):
55+
raise TypeError(
56+
f"sandbox should of of type <class 'bool'> not {type(is_active)}")
57+
58+
if is_active:
59+
self.base_url_single = self.SANDBOX_URL_SINGLE
60+
self.base_url_multiple = self.SANDBOX_URL_MULTIPLE
61+
else:
62+
self.base_url_single = self.PRODUCTION_URL_SINGLE
63+
self.base_url_multiple = self.PRODUCTION_URL_MULTIPLE
64+
2665
def initialize(self, username: str, password: str) -> None:
2766
"""Initialize nextsms access credentials
2867
@@ -32,7 +71,7 @@ def initialize(self, username: str, password: str) -> None:
3271
"""
3372
self._user = self.create_user(username, password)
3473

35-
def create_user(self, username: str, password: str) -> User:
74+
def create_user(self, username: str, password: str) -> Iterable:
3675
"""Create a namedtuple of user credentials
3776
3877
Args:

0 commit comments

Comments
 (0)