@ -1,4 +1,4 @@
#!/usr/bin/env python3
0 #!/usr/bin/env python3
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
# sipcentric/__init__.py
# sipcentric/__init__.py
@ -20,9 +20,13 @@ logger = logging.getLogger(__name__)
class Sipcentric ( object ) :
class Sipcentric ( object ) :
def __init__ (
def __init__ ( self , username , password , base = ' https://pbx.sipcentric.com/api/v1 ' , customer = ' me ' ) :
self ,
username ,
password ,
base = " https://pbx.sipcentric.com/api/v1 " ,
customer = " me " ,
) :
self . username = username # Account username
self . username = username # Account username
self . password = password # Account password
self . password = password # Account password
self . base = base # Base API URL (default: https://pbx.sipcentric.com/api/v1)
self . base = base # Base API URL (default: https://pbx.sipcentric.com/api/v1)
@ -42,220 +46,182 @@ class Sipcentric(object):
self . sounds = Sounds ( self )
self . sounds = Sounds ( self )
self . outgoingCallerIds = OutgoingCallerIds ( self )
self . outgoingCallerIds = OutgoingCallerIds ( self )
self . Stream = Stream ( self )
self . stream = Stream ( self )
def _request ( self , uri , method = ' GET ' , data = None , params = None ) :
url = self . base + ' /customers/ ' + self . customer + ' / ' + uri
def _request ( self , uri , method = " GET " , data = None , params = None ) :
url = self . base + " /customers/ " + self . customer + " / " + uri
auth = requests . auth . HTTPBasicAuth ( self . username , self . password ) # Basic auth
auth = requests . auth . HTTPBasicAuth ( self . username , self . password ) # Basic auth
if method == ' GET ' :
if method == " GET " :
if params :
if params :
r = requests . get ( url , auth = auth , params = params , verify = True , timeout = 3.000 )
r = requests . get (
url , auth = auth , params = params , verify = True , timeout = 3.000
)
else :
else :
r = requests . get ( url , auth = auth , verify = True , timeout = 3.000 )
r = requests . get ( url , auth = auth , verify = True , timeout = 3.000 )
elif method == ' POST ' :
elif method == " POST " :
headers = { " content-type " : " application/json " }
headers = { ' content-type ' : ' application/json ' }
if params :
if params :
r = requests . post ( url , auth = auth , headers = headers , data = json . dumps ( data ) , params = params , verify = True , timeout = 3.000 )
r = requests . post (
url ,
auth = auth ,
headers = headers ,
data = json . dumps ( data ) ,
params = params ,
verify = True ,
timeout = 3.000 ,
)
else :
else :
r = requests . post ( url , auth = auth , headers = headers , data = json . dumps ( data ) , verify = True , timeout = 3.000 )
r = requests . post (
url ,
auth = auth ,
headers = headers ,
data = json . dumps ( data ) ,
verify = True ,
timeout = 3.000 ,
)
if ( r . status_code == 200 ) or ( r . status_code == 201 ) :
if ( r . status_code == 200 ) or ( r . status_code == 201 ) :
try :
try :
response = r . json ( )
response = r . json ( )
return response
return response
except :
except :
return True
return True
elif r . status_code == 401 :
elif r . status_code == 401 :
raise AuthenticationException (
raise AuthenticationException ( ' We couldn \' t authenticate you with the API. Make sure you are using the correct credentials from the \' Web Users \' section of the control panel. If you dont have an account, sign up for one at https://my.nimvelo.com/signup' )
" We couldn ' t authenticate you with the API. Make sure you are using the correct credentials from the ' Web Users ' section of the control panel. If you dont have an account, sign up for one at https://my.nimvelo.com/signup"
)
return False
return False
else :
else :
if r . json ( ) :
if r . json ( ) :
raise Exception ( " HTTP Error " + str ( r . status_code ) , r . json ( ) )
raise Exception ( ' HTTP Error ' + str ( r . status_code ) , r . json ( ) )
else :
else :
raise Exception (
raise Exception ( ' HTTP Error ' + str ( r . status_code ) , ' Something went wrong with the request ' )
" HTTP Error " + str ( r . status_code ) ,
" Something went wrong with the request " ,
)
return False
return False
class Account ( object ) :
class Account ( object ) :
def __init__ ( self , parent ) :
def __init__ ( self , parent ) :
self . parent = parent
self . parent = parent
self . uri = ' ' # Not needed for the base of the customer
self . uri = " " # Not needed for the base of the customer
def get ( self ) :
def get ( self ) :
return self . parent . _request ( self . uri )
return self . parent . _request ( self . uri )
class CallBundles ( object ) :
class CallBundles ( object ) :
def __init__ ( self , parent ) :
def __init__ ( self , parent ) :
self . parent = parent
self . parent = parent
self . uri = ' callbundles '
self . uri = " callbundles "
def get ( self ) :
def get ( self ) :
return self . parent . _request ( self . uri )
return self . parent . _request ( self . uri )
class Recordings ( object ) :
class Recordings ( object ) :
def __init__ ( self , parent ) :
def __init__ ( self , parent ) :
self . parent = parent
self . parent = parent
self . uri = ' recordings '
self . uri = " recordings "
def get ( self ) :
def get ( self ) :
return self . parent . _request ( self . uri )
return self . parent . _request ( self . uri )
class PhoneBook ( object ) :
class PhoneBook ( object ) :
def __init__ ( self , parent ) :
def __init__ ( self , parent ) :
self . parent = parent
self . parent = parent
self . uri = ' phonebook '
self . uri = " phonebook "
def get ( self ) :
def get ( self ) :
return self . parent . _request ( self . uri )
return self . parent . _request ( self . uri )
class TimeIntervals ( object ) :
class TimeIntervals ( object ) :
def __init__ ( self , parent ) :
def __init__ ( self , parent ) :
self . parent = parent
self . parent = parent
self . uri = ' timeintervals '
self . uri = " timeintervals "
def get ( self ) :
def get ( self ) :
return self . parent . _request ( self . uri )
return self . parent . _request ( self . uri )
class Endpoints ( object ) :
class Endpoints ( object ) :
def __init__ ( self , parent ) :
def __init__ ( self , parent ) :
self . parent = parent
self . parent = parent
self . uri = ' endpoints '
self . uri = " endpoints "
def get ( self ) :
def get ( self ) :
return self . parent . _request ( self . uri )
return self . parent . _request ( self . uri )
class PhoneNumbers ( object ) :
class PhoneNumbers ( object ) :
def __init__ ( self , parent ) :
def __init__ ( self , parent ) :
self . parent = parent
self . parent = parent
self . uri = ' phonenumbers '
self . uri = " phonenumbers "
def get ( self ) :
def get ( self ) :
return self . parent . _request ( self . uri )
return self . parent . _request ( self . uri )
class Sms ( object ) :
class Sms ( object ) :
def __init__ ( self , parent ) :
def __init__ ( self , parent ) :
self . parent = parent
self . parent = parent
self . uri = ' sms '
self . uri = " sms "
def get ( self ) :
def get ( self ) :
return self . parent . _request ( self . uri )
return self . parent . _request ( self . uri )
def post ( self , to = None , _from = None , body = None ) :
def post ( self , to = None , _from = None , body = None ) :
data = { " type " : " smsmessage " , " to " : to , " from " : _from , " body " : body }
data = {
return self . parent . _request ( self . uri , method = " POST " , data = data )
' type ' : ' smsmessage ' ,
' to ' : to ,
' from ' : _from ,
' body ' : body
}
return self . parent . _request ( self . uri , method = ' POST ' , data = data )
class CreditStatus ( object ) :
class CreditStatus ( object ) :
def __init__ ( self , parent ) :
def __init__ ( self , parent ) :
self . parent = parent
self . parent = parent
self . uri = ' creditstatus '
self . uri = " creditstatus "
def get ( self ) :
def get ( self ) :
return self . parent . _request ( self . uri )
return self . parent . _request ( self . uri )
class Calls ( object ) :
class Calls ( object ) :
def __init__ ( self , parent ) :
def __init__ ( self , parent ) :
self . parent = parent
self . parent = parent
self . uri = ' calls '
self . uri = " calls "
def get ( self ) :
def get ( self ) :
return self . parent . _request ( self . uri )
return self . parent . _request ( self . uri )
class Sounds ( object ) :
class Sounds ( object ) :
def __init__ ( self , parent ) :
def __init__ ( self , parent ) :
self . parent = parent
self . parent = parent
self . uri = ' sounds '
self . uri = " sounds "
def get ( self ) :
def get ( self ) :
return self . parent . _request ( self . uri )
return self . parent . _request ( self . uri )
class OutgoingCallerIds ( object ) :
class OutgoingCallerIds ( object ) :
def __init__ ( self , parent ) :
def __init__ ( self , parent ) :
self . parent = parent
self . parent = parent
self . uri = ' outgoingcallerids '
self . uri = " outgoingcallerids "
def get ( self ) :
def get ( self ) :
return self . parent . _request ( self . uri )
return self . parent . _request ( self . uri )
class AuthenticationException ( Exception ) :
class AuthenticationException ( Exception ) :
pass
pass
if __name__ == ' __main__ ' :
if __name__ == " __main__ " :
logging . error ( ' Do not run directly, import module first! ' )
logging . error ( " Do not run directly, import module first! " )
sys . exit ( )
sys . exit ( )