Skip to content

Commit ada40bb

Browse files
committed
added docstring
1 parent 422d16b commit ada40bb

File tree

1 file changed

+81
-9
lines changed

1 file changed

+81
-9
lines changed

nextsms/__init__.py

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,40 @@ class NextSms(object):
1212

1313
User = namedtuple('User', 'username password secret_key')
1414

15-
def __init__(self) -> None:
16-
self._user = None
15+
def __init__(self, username: str = '', password: str = '') -> None:
16+
"""Initialize nextsms access credentials
17+
18+
Args:
19+
username (str): your username for nextsms
20+
password (str): your login password for nextsms
21+
"""
22+
self._user = (None
23+
if not all(username and password)
24+
else self.create_user(username, password))
1725

1826
def initialize(self, username: str, password: str) -> None:
27+
"""Initialize nextsms access credentials
28+
29+
Args:
30+
username (str): your username for nextsms
31+
password (str): your login password for nextsms
32+
"""
1933
self._user = self.create_user(username, password)
2034

21-
def create_user(self, username, password) -> User:
35+
def create_user(self, username: str, password: str) -> User:
36+
"""Create a namedtuple of user credentials
37+
38+
Args:
39+
username ([type]): username for nextms
40+
password ([type]): password for nextsms
41+
42+
Raises:
43+
TypeError: if username in not of type <class 'str'>
44+
TypeError: if password in not of type <class 'str'>
45+
46+
Returns:
47+
User: namedtuple datascture to hold user details (username, password, secret_key)
48+
"""
2249
if not isinstance(username, str):
2350
raise TypeError(
2451
f"username should be of type <class 'str'> not {type(username)}")
@@ -32,6 +59,14 @@ def create_user(self, username, password) -> User:
3259
)
3360

3461
def create_header(self) -> Dict:
62+
"""Auto generate json headers to be used in request
63+
64+
Raises:
65+
Exception: If user credentials are not specified
66+
67+
Returns:
68+
Dict: headers to be used on post requests
69+
"""
3570
if not self._user:
3671
raise Exception(
3772
'''
@@ -47,7 +82,28 @@ def create_header(self) -> Dict:
4782
'Authorization': f'Basic {self._user.secret_key}'
4883
}
4984

50-
def send_sms(self, message: str, recipients: Union[str, List], sender_id: str = "NEXTSMS") -> Dict:
85+
def sendsms(self, message: str, recipients: Union[str, List[str]], sender_id: str = "NEXTSMS") -> Dict:
86+
"""Method to send sms using nextsms gateway
87+
88+
Args:
89+
message (str): message to be sent
90+
recipients (Union[str, List[str]]): A string of a single number or List of multiple recipients
91+
sender_id (str, optional): your Sender ID Defaults to "NEXTSMS".
92+
93+
Raises:
94+
TypeError: If message is not type of <class 'str'>
95+
TypeError: If recipients is not type of <class 'str'> or <class 'list'>
96+
TypeError: If message is not type of <class 'str'>
97+
98+
Returns:
99+
Dict: Response from the nextsms gateway
100+
101+
Example:
102+
103+
>> import nextsms
104+
>> sender = nextsms('KalebuJordan', 'kalebu@opensource')
105+
>> sender.sendsms('hello', '255757294146', 'Neurotech')
106+
"""
51107
if not isinstance(sender_id, str):
52108
raise TypeError(
53109
f"sender_id should of type <class 'str'> not {type(sender_id)}")
@@ -67,12 +123,28 @@ def send_sms(self, message: str, recipients: Union[str, List], sender_id: str =
67123
'text': message
68124
}).json()
69125

70-
def send_bulk(self, messages: List, sender_id: str = "NEXTSMS"):
126+
def send_bulk(self, messages: List[Dict]) -> Dict:
127+
"""[summary]
71128
72-
if not isinstance(sender_id, str):
73-
raise TypeError(
74-
f"sender_id should be of type <class 'str'> not {type(sender_id)}")
129+
Args:
130+
messages (List[Dict]): List of message objects to be sent
131+
132+
Raises:
133+
TypeError: if messages is not of type <class 'list'>
134+
135+
Returns:
136+
Dict: NextSMS Response
137+
138+
139+
Example:
75140
141+
>> import nextsms
142+
>> sender = nextsms('KalebuJordan', 'kalebu@opensource')
143+
>> messages = [
144+
{'from':'NEXTSMS', 'to':'255757294146', 'text':'hello'},
145+
{'from':'NEXTSMS', 'to':'255754205561', 'text':'hello'}]
146+
>> sender.send_bulk(messages)
147+
"""
76148
if not isinstance(messages, list):
77149
raise TypeError(
78150
f"messages should be of type <class 'list'> not type {type(messages)}")
@@ -86,4 +158,4 @@ def send_bulk(self, messages: List, sender_id: str = "NEXTSMS"):
86158
).json()
87159

88160

89-
sys.modules[__name__] = NextSms()
161+
sys.modules[__name__] = NextSms

0 commit comments

Comments
 (0)