11import sys
22import requests
33from base64 import b64encode
4+ from typing import List , Dict
45from collections import namedtuple
56
67
78class NextSms (object ):
89
9- BASE_URL = 'https://messaging-service.co.tz'
10+ BASE_URL = ' https://messaging-service.co.tz/api/sms/v1/test/text/single '
1011
11- User = namedtuple ('User' , 'username password secret_kery ' )
12+ User = namedtuple ('User' , 'username password secret_key ' )
1213
13- def __init__ (self , username : str , password : str ) -> None :
14+ def __init__ (self ) -> None :
15+ self ._user = None
16+
17+ def initialize (self , username : str , password : str ) -> None :
1418 self ._user = self .create_user (username , password )
1519
16- def create_user (self , username , password ):
20+ def create_user (self , username , password ) -> User :
1721 if not isinstance (username , str ):
1822 raise TypeError (
1923 f"username should be of type <class 'str'> not { type (username )} " )
@@ -27,13 +31,41 @@ def create_user(self, username, password):
2731 )
2832
2933 def create_header (self ):
34+ if not self ._user :
35+ raise Exception (
36+ '''
37+ Please Make sure You initialize before calling any other method
38+
39+ >> import nextsms
40+ >> nextsms.initialize(username, password)'''
41+ )
42+
3043 return {
31- 'Content-Type' : 'json' ,
44+ 'Accept' : 'application/json' ,
45+ 'Content-Type' : 'application/json' ,
3246 'Authorization' : f'Basic { self ._user .secret_key } '
3347 }
3448
35- def send_sms (self ):
36- pass
49+ def send_sms (self , message : str , recipient : str , sender_id : str = "NEXTSMS" ):
50+ if not isinstance (sender_id , str ):
51+ raise TypeError (
52+ f"sender_id should of type <class 'str'> not { type (sender_id )} " )
53+ if not isinstance (recipient , str ):
54+ raise TypeError (
55+ f"recipient should be of type <class 'str'> not { type (recipient )} " )
56+ if not isinstance (message , str ):
57+ raise TypeError (
58+ f"message should be of type <class 'str'> not { type (message )} " )
59+
60+ return requests .post (
61+ self .BASE_URL ,
62+ headers = self .create_header (),
63+ json = {
64+ 'from' : sender_id ,
65+ 'to' : recipient ,
66+ 'text' : message
67+ }
68+ ).json ()
3769
3870
39- sys .modules [__name__ ] = NextSms
71+ sys .modules [__name__ ] = NextSms ()
0 commit comments