1+ """
2+ A Nimba SMS Client API.
3+
4+ This module contains Class Client manager services available in API : developers.nimbasms.com
5+
6+ Dependencies
7+ -----------
8+ requests : A library for HTTP Request
9+
10+ class
11+ ---------
12+ HTTPClient : Abstract class representing HTTP Client
13+ Response: Response representing data output.
14+ Client : Manager all services APIs.
15+ """
16+
117import logging
218import platform
319import json
420
21+ from urllib .parse import urlencode
522from requests import Request , Session , hooks
623from requests .adapters import HTTPAdapter
7- from urllib .parse import urlencode
8- from nimbasms .execptions import NimbaException
24+ from nimbasms .execptions import NimbaSMSException
25+
26+ from nimbasms .rest import Accounts
27+ from nimbasms .rest import Contacts
28+ from nimbasms .rest import Groups
29+ from nimbasms .rest import Messages
30+ from nimbasms .rest import SenderNames
931
1032__version__ = '1.0.0'
1133
1234_logger = logging .getLogger ('nimbasms' )
1335
14- class HttpClient ( object ) :
36+ class HttpClient :
1537 """
1638 An Abstract class representing an HTTP client.
1739 """
@@ -23,7 +45,10 @@ def request(self, method, url, params=None, data=None, headers=None,
2345 raise NimbaSMSException ('HttpClient is a an abstract class' )
2446
2547
26- class Response (object ):
48+ class Response :
49+ """
50+ Representing data output API calls.
51+ """
2752 def __init__ (self , status_code , text , headers = None ):
2853 self .content = text
2954 self .headers = headers
@@ -33,14 +58,20 @@ def __init__(self, status_code, text, headers=None):
3358
3459 @property
3560 def text (self ):
61+ """
62+ Content output byte
63+ """
3664 return self .content
3765
3866 @property
3967 def data (self ):
68+ """
69+ Output data response APIs
70+ """
4071 return json .loads (self .content )
4172
4273 def __repr__ (self ):
43- return 'HTTP {} {}' . format ( self .status_code , self .content )
74+ return f 'HTTP { self .status_code } { self .content } '
4475
4576
4677class NimbaHttpClient (HttpClient ):
@@ -120,31 +151,33 @@ def request(self, method, url, params=None, data=None, headers=None,
120151 return self .last_response
121152
122153 def _log_request (self , kwargs ):
154+ """
155+ Logger request APIs
156+ """
123157 self .logger .info ('-- BEGIN Nimba SMS API Request --' )
124158
125159 if kwargs ['params' ]:
126- self .logger .info ('{} Request: {}?{}' . format ( kwargs [ 'method' ],
127- kwargs ['url' ], urlencode (kwargs ['params' ])) )
128- self .logger .info (' Query Params: {}' . format ( kwargs ['params' ]) )
160+ self .logger .info (
161+ f" { kwargs [ 'method' ] } Request: { kwargs ['url' ]} ? { urlencode (kwargs ['params' ])} " )
162+ self .logger .info (f" Query Params: { kwargs ['params' ]} " )
129163 else :
130- self .logger .info ('{} Request: {}' .format (kwargs ['method' ],
131- kwargs ['url' ]))
164+ self .logger .info (f"{ kwargs ['method' ]} Request: { kwargs ['url' ]} " )
132165
133166 if kwargs ['headers' ]:
134167 self .logger .info ('Headers:' )
135168 for key , value in kwargs ['headers' ].items ():
136169 #Do not log authorization headers
137170 if 'authorization' not in key .lower ():
138- self .logger .info ('{ } : {}' . format ( key , value ) )
171+ self .logger .info (f' { key } : { value } ' )
139172
140173 self .logger .info ('-- END Nimba SMS API Request --' )
141174
142175 def _log_response (self , response ):
143- self .logger .info ('Response Status Code: {}' . format ( response .status_code ) )
144- self .logger .info ('Response Headers: {}' . format ( response .headers ) )
176+ self .logger .info (f 'Response Status Code: { response .status_code } ' )
177+ self .logger .info (f 'Response Headers: { response .headers } ' )
145178
146179
147- class Client ( object ) :
180+ class Client :
148181 """A client for accessing the Nimba SMS API."""
149182
150183 def __init__ (self , account_sid = None , access_token = None ):
@@ -154,13 +187,10 @@ def __init__(self, account_sid=None, access_token=None):
154187 :param str account_sid: Account SID
155188 :param str access_token: Token authenticate
156189 """
157- self .account_sid = account_sid
158- self .access_token = access_token
159-
160- if not self .account_sid or not self .access_token :
161- raise NimbaException ("Credentials are required"
190+ if not account_sid or not access_token :
191+ raise NimbaSMSException ("Credentials are required"
162192 " to create a NimbaClient" )
163- self .auth = (self . account_sid , self . access_token )
193+ self .auth = (account_sid , access_token )
164194
165195 self .http_client = NimbaHttpClient ()
166196
@@ -193,12 +223,8 @@ def request(self, method, uri, params=None, data=None,
193223 os_name = platform .system ()
194224 os_arch = platform .machine ()
195225 python_version = platform .python_version ()
196- headers ['User-Agent' ] = 'nimba-python/{} ({} {}) Python/{}' .format (
197- pkg_version ,
198- os_name ,
199- os_arch ,
200- python_version
201- )
226+ headers ['User-Agent' ] = (
227+ f'nimba-python/{ pkg_version } ({ os_name } { os_arch } ) Python/{ python_version } ' )
202228 headers ['X-Nimba-Client' ] = 'utf-8'
203229
204230 if method == 'POST' and 'Content-Type' not in headers :
@@ -225,7 +251,6 @@ def accounts(self):
225251 :returns Accounts
226252 """
227253 if self ._accounts is None :
228- from nimbasms .rest import Accounts
229254 self ._accounts = Accounts (self )
230255 return self ._accounts
231256
@@ -237,7 +262,6 @@ def messages(self):
237262 :returns Messages NimbaAPI
238263 """
239264 if self ._messages is None :
240- from nimbasms .rest import Messages
241265 self ._messages = Messages (self )
242266 return self ._messages
243267
@@ -249,30 +273,27 @@ def contacts(self):
249273 :returns Contacts NimbaAPI
250274 """
251275 if self ._contacts is None :
252- from nimbasms .rest import Contacts
253276 self ._contacts = Contacts (self )
254277 return self ._contacts
255278
256279 @property
257280 def groups (self ):
258281 """
259- Group Accounts
260-
282+ Group Accounts.
283+
261284 :returns Groups NimbaAPI
262285 """
263286 if self ._groups is None :
264- from nimbasms .rest import Groups
265287 self ._groups = Groups (self )
266288 return self ._groups
267289
268290 @property
269291 def sendernames (self ):
270292 """
271- Sendername Accounts
272-
293+ Sendername Accounts.
294+
273295 :returns Sendername NimbaAPI
274296 """
275297 if self ._sendernames is None :
276- from nimbasms .rest import SenderNames
277298 self ._sendernames = SenderNames (self )
278299 return self ._sendernames
0 commit comments