Make exceptions have a base class; add type hints to functions
This commit is contained in:
parent
0bb7fb94cd
commit
0ef8f7182d
|
@ -1,11 +1,14 @@
|
||||||
|
import typing
|
||||||
# Environment var definitions
|
# Environment var definitions
|
||||||
ENV_URI='TCX_SITE_URI'
|
ENV_URI='TCX_SITE_URI'
|
||||||
ENV_AUTH_USER='TCX_API_AUTH_USERNAME'
|
ENV_AUTH_USER='TCX_API_AUTH_USERNAME'
|
||||||
ENV_AUTH_PASS='TCX_API_AUTH_PASSWORD'
|
ENV_AUTH_PASS='TCX_API_AUTH_PASSWORD'
|
||||||
|
|
||||||
class APIError(Exception):
|
class Py3CXException(Exception):
|
||||||
pass
|
pass
|
||||||
class ValidationError(Exception):
|
class APIError(Py3CXException):
|
||||||
|
pass
|
||||||
|
class ValidationError(Py3CXException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class Request:
|
class Request:
|
||||||
|
@ -74,7 +77,7 @@ class TransactionalObject(APIObject):
|
||||||
'Id':self._session})
|
'Id':self._session})
|
||||||
|
|
||||||
class Py3CX:
|
class Py3CX:
|
||||||
class __Call(ReadOnlyObject):
|
class _Call(ReadOnlyObject):
|
||||||
def __init__(self, tcx, callid):
|
def __init__(self, tcx, callid):
|
||||||
super().__init__(tcx, 'activeCalls')
|
super().__init__(tcx, 'activeCalls')
|
||||||
self.params=callid
|
self.params=callid
|
||||||
|
@ -95,7 +98,7 @@ class Py3CX:
|
||||||
self.tcx.rq.post('activeCalls/drop', params={
|
self.tcx.rq.post('activeCalls/drop', params={
|
||||||
'Id': self.params})
|
'Id': self.params})
|
||||||
self.params=None
|
self.params=None
|
||||||
class __User(TransactionalObject):
|
class _User(TransactionalObject):
|
||||||
def __init__(self, parent, params):
|
def __init__(self, parent, params):
|
||||||
super().__init__(parent, 'ExtensionList/set')
|
super().__init__(parent, 'ExtensionList/set')
|
||||||
self.params=params
|
self.params=params
|
||||||
|
@ -115,9 +118,9 @@ class Py3CX:
|
||||||
self.sip_authpw=parms['AuthPassword']['_value']
|
self.sip_authpw=parms['AuthPassword']['_value']
|
||||||
self.provision_uri=parms['MyPhoneProvLink']['_value']
|
self.provision_uri=parms['MyPhoneProvLink']['_value']
|
||||||
self.webpw=parms['AccessPassword']['_value']
|
self.webpw=parms['AccessPassword']['_value']
|
||||||
self.extension=Py3CX.__Extension(self.tcx, self.params)
|
self.extension=Py3CX._Extension(self.tcx, self.params)
|
||||||
self.cancel()
|
self.cancel()
|
||||||
class __Extension(ReadOnlyObject):
|
class _Extension(ReadOnlyObject):
|
||||||
def __init__(self, parent, params, populate=True):
|
def __init__(self, parent, params, populate=True):
|
||||||
super().__init__(parent, api='ExtensionList')
|
super().__init__(parent, api='ExtensionList')
|
||||||
self.params=params
|
self.params=params
|
||||||
|
@ -141,7 +144,7 @@ class Py3CX:
|
||||||
self.cli=res['OutboundCallerId']
|
self.cli=res['OutboundCallerId']
|
||||||
self.mobile=res['MobileNumber']
|
self.mobile=res['MobileNumber']
|
||||||
self.online=res['IsRegistered']
|
self.online=res['IsRegistered']
|
||||||
class __PhoneSystem:
|
class _PhoneSystem:
|
||||||
class System(object):
|
class System(object):
|
||||||
class License(object):
|
class License(object):
|
||||||
pass
|
pass
|
||||||
|
@ -205,7 +208,7 @@ class Py3CX:
|
||||||
self.refresh_inroute()
|
self.refresh_inroute()
|
||||||
self.refresh_sysstat()
|
self.refresh_sysstat()
|
||||||
@property
|
@property
|
||||||
def extensionlist(self):
|
def list_extensions(self) -> typing.List[Py3CX._Extension]:
|
||||||
if self.extlist is None:
|
if self.extlist is None:
|
||||||
self.extlist=ReadOnlyObject(self.tcx, api='ExtensionList')
|
self.extlist=ReadOnlyObject(self.tcx, api='ExtensionList')
|
||||||
self.extlist.refresh()
|
self.extlist.refresh()
|
||||||
|
@ -213,7 +216,7 @@ class Py3CX:
|
||||||
assert (len(res)>0), "No extensions found"
|
assert (len(res)>0), "No extensions found"
|
||||||
ret=[]
|
ret=[]
|
||||||
for extension in res:
|
for extension in res:
|
||||||
this=Py3CX.__Extension(self.tcx, params=extension['Number'], populate=False)
|
this=Py3CX._Extension(self.tcx, params=extension['Number'], populate=False)
|
||||||
this.firstname=extension['FirstName']
|
this.firstname=extension['FirstName']
|
||||||
this.surname=extension['LastName']
|
this.surname=extension['LastName']
|
||||||
this.name="%s %s" % (this.firstname, this.surname)
|
this.name="%s %s" % (this.firstname, this.surname)
|
||||||
|
@ -226,7 +229,7 @@ class Py3CX:
|
||||||
ret.append(this)
|
ret.append(this)
|
||||||
return ret
|
return ret
|
||||||
@property
|
@property
|
||||||
def callslist(self):
|
def list_calls(self) -> typing.List[Py3CX._Call]:
|
||||||
if self.calllist is None:
|
if self.calllist is None:
|
||||||
self.calllist=ReadOnlyObject(self.tcx, api='activeCalls')
|
self.calllist=ReadOnlyObject(self.tcx, api='activeCalls')
|
||||||
self.calllist.refresh()
|
self.calllist.refresh()
|
||||||
|
@ -234,7 +237,7 @@ class Py3CX:
|
||||||
if len(res)==0: return []
|
if len(res)==0: return []
|
||||||
ret=[]
|
ret=[]
|
||||||
for call in res:
|
for call in res:
|
||||||
this=Py3CX.__Call(self.tcx, call['Id'])
|
this=Py3CX._Call(self.tcx, call['Id'])
|
||||||
this.caller=call['Caller']
|
this.caller=call['Caller']
|
||||||
this.callee=call['Callee']
|
this.callee=call['Callee']
|
||||||
this.state=call['Status']
|
this.state=call['Status']
|
||||||
|
@ -260,16 +263,16 @@ class Py3CX:
|
||||||
'Password': self.passw})
|
'Password': self.passw})
|
||||||
self.rq.sess.headers.update({'x-xsrf-token':rs.cookies['XSRF-TOKEN']})
|
self.rq.sess.headers.update({'x-xsrf-token':rs.cookies['XSRF-TOKEN']})
|
||||||
@property
|
@property
|
||||||
def authenticated(self):
|
def authenticated(self) -> bool:
|
||||||
try:
|
try:
|
||||||
self.rq.get('CurrentUser')
|
self.rq.get('CurrentUser')
|
||||||
except APIError:
|
except APIError:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
@property
|
@property
|
||||||
def System(self):
|
def system(self) -> Py3CX._PhoneSystem:
|
||||||
assert self.authenticated, "Py3CX not authenticated yet!"
|
assert self.authenticated, "Py3CX not authenticated yet!"
|
||||||
if self.__tcxsystem is None:
|
if self.__tcxsystem is None:
|
||||||
self.__tcxsystem=__PhoneSystem(self)
|
self.__tcxsystem=Py3CX._PhoneSystem(self)
|
||||||
return self.__tcxsystem
|
return self.__tcxsystem
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue