cleanups and improvements to api

This commit is contained in:
Matthew Connelly 2019-05-21 11:18:33 +01:00
parent 90fe289a1d
commit 87a1fdce4f
1 changed files with 272 additions and 199 deletions

View File

@ -24,9 +24,9 @@ class Request:
assert (resp.status_code==expect)
except AssertionError:
raise APIError("Assertion error when handling API response; response code was %s, but expected response was %s" % (resp.status_code, expect))
except exceptions.SSLError as e:
except self.exceptions.SSLError as e:
raise APIError("TLS error returned when communicating; use tls_verify=False or check leaf certs: %s" % str(e))
except exceptions.BaseHTTPError as e:
except self.exceptions.BaseHTTPError as e:
raise APIError("HTTP error raised when communicating: %s" % str(e))
except ConnectionError as e:
raise APIError("ConnectionError raised when communicating: %s" % str(e))
@ -38,9 +38,9 @@ class Request:
assert (resp.status_code==expect)
except AssertionError:
raise APIError("Assertion error when handling API response; response code was %s, but expected response was %s" % (resp.status_code, expect))
except exceptions.SSLError as e:
except self.exceptions.SSLError as e:
raise APIError("TLS error returned when communicating; use tls_verify=False or check leaf certs: %s" % str(e))
except exceptions.BaseHTTPError as e:
except self.exceptions.BaseHTTPError as e:
raise APIError("HTTP error raised when communicating: %s" % str(e))
except ConnectionError as e:
raise APIError("ConnectionError raised when communicating: %s" % str(e))
@ -48,6 +48,8 @@ class Request:
raise APIError("Other exception raised during API call: %s" % str(e))
return resp
class BasicObject(object):
pass
class ReadOnlyObject(object):
def __init__(self, parent, api):
self.tcx=parent
@ -56,10 +58,10 @@ class ReadOnlyObject(object):
self._result=self.tcx.rq.get(self.api, params=params)
self.active=self._result.json()
class TransactionalObject(object):
def __init__(self, parent, api, submit='edit/save', discard='edit/cancel'):
def __init__(self, parent, api, save='edit/save', discard='edit/cancel'):
self.tcx=parent
self.api=api
self.submit=submit
self.save=save
self.discard=discard
def create(self, params):
self._result=self.tcx.rq.post(self.api, params=params)
@ -71,6 +73,28 @@ class TransactionalObject(object):
self.tcx.rq.post(self.discard, params={
'Id':self._session})
class Call(ReadOnlyObject):
def __init__(self, tcx, callid):
super().__init__(tcx, 'activeCalls')
self.params=callid
def refresh(self):
self.refresh()
self.timestamp=self._result.headers.get('date')
res=list(filter(lambda cid: cid['Id'] == self.params, self._result.json()['list']))
assert (len(res)>0), "No call found in currently-active call list for ID %s" % self.params
assert(len(res)==1), "More than one active call found for ID %s" % self.params
res=res[0]
self.id=res['Id']
self.caller=res['Caller']
self.callee=res['Callee']
self.state=res['Status']
self.duration=res['Duration']
self.since=res['LastChangeStatus']
def hangup(self):
self.tcx.rq.post('activeCalls/drop', params={
'Id': self.params})
self.params=None
class User(TransactionalObject):
def __init__(self, parent, params):
super().__init__(parent, 'ExtensionList/set')
@ -94,10 +118,11 @@ class User(TransactionalObject):
self.extension=Extension(self.tcx, self.params)
self.cancel()
class Extension(ReadOnlyObject):
def __init__(self, parent, params):
def __init__(self, parent, params, populate=True):
super().__init__(parent, api='ExtensionList')
self.params=params
self.load()
if populate:
self.load()
def load(self):
self.refresh(params=self.params)
res=self._result
@ -124,16 +149,12 @@ class PhoneSystem:
pass
def __init__(self, tcx):
self.tcx=tcx
self.extlist=None
def refresh_sysstat(self):
sysstat=self.tcx.rq.get('SystemStatus').json()
addl=self.tcx.rq.get('SystemStatus/AdditionalStatus').json()
inroute=self.tcx.rq.get('InboundRulesList').json()['list']
hists=self.tcx.rq.post('SystemStatus/getDbInformation').json()
certstat=self.tcx.rq.get('SystemStatus/GetSingleStatus').json()
self.Status.exts_online=sysstat['ExtensionsRegistered']
self.Status.trunks_online=sysstat['TrunksRegistered']
self.Status.calls_active=sysstat['CallsActive']
self.Status.callrecs_bytes=addl['RecordingUsedSpace']
self.Status.diskbytes_free=sysstat['FreeDiskSpace']
self.Status.membytes_free=sysstat['FreePhysicalMemory']
self.Status.banned_ips=sysstat['BlacklistedIpCount']
@ -141,23 +162,15 @@ class PhoneSystem:
self.Status.sysexts_online=not sysstat['HasUnregisteredSystemExtensions']
self.Status.services_online=not sysstat['HasNotRunningServices']
self.System.routes_out=sysstat['OutboundRules']
self.System.routes_in=len(inroute)
self.System.fqdn=sysstat['FQDN']
self.System.webconf=sysstat['WebMeetingFQDN']
self.System.version=sysstat['Version']
self.System.diskbytes_total=sysstat['TotalDiskSpace']
self.System.membytes_total=sysstat['TotalPhysicalMemory']
self.System.calls_total=hists['CallHistoryCount']
self.System.chats_total=hists['ChatMessagesCount']
self.System.callrecs_enabled=not addl['RecordingStopped']
self.System.callrecs_quota=addl['RecordingQuota']
self.System.exts_total=sysstat['ExtensionsTotal']
self.System.trunks_total=sysstat['TrunksTotal']
self.System.firebase_confed=sysstat['OwnPush']
self.System.backups_enabled=sysstat['BackupScheduled']
self.System.License.firewallsupported=certstat['Health']['Firewall']
self.System.License.trunkssupported=certstat['Health']['Trunks']
self.System.License.phonessupported=certstat['Health']['Phones']
self.System.License.activated=sysstat['Activated']
self.System.License.calls=sysstat['MaxSimCalls']
self.System.License.meetingcalls=sysstat['MaxSimMeetingParticipants']
@ -168,7 +181,67 @@ class PhoneSystem:
self.System.License.key=sysstat['LicenseKey']
self.System.License.sku=sysstat['ProductCode']
self.System.License.spla=sysstat['IsSpla']
def refresh_addl(self):
addl=self.tcx.rq.get('SystemStatus/AdditionalStatus').json()
self.Status.callrecs_bytes=addl['RecordingUsedSpace']
self.System.callrecs_enabled=not addl['RecordingStopped']
self.System.callrecs_quota=addl['RecordingQuota']
def refresh_inroute(self):
inroute=self.tcx.rq.get('InboundRulesList').json()['list']
self.System.routes_in=len(inroute)
def refresh_hists(self):
hists=self.tcx.rq.post('SystemStatus/getDbInformation', params={}).json()
self.System.calls_total=hists['CallHistoryCount']
self.System.chats_total=hists['ChatMessagesCount']
def refresh_certified(self):
certstat=self.tcx.rq.get('SystemStatus/GetSingleStatus').json()
self.System.License.firewallsupported=certstat['Health']['Firewall']
self.System.License.trunkssupported=certstat['Health']['Trunks']
self.System.License.phonessupported=certstat['Health']['Phones']
def refresh(self):
self.refresh_addl()
self.refresh_certified()
self.refresh_hists()
self.refresh_inroute()
self.refresh_sysstat()
@property
def extensionlist(self):
if self.extlist is None:
self.extlist=ReadOnlyObject(self.tcx, api='ExtensionList')
self.extlist.refresh()
res=self.extlist._result.json()['list']
assert (len(res)>0), "No extensions found"
ret=[]
for extension in res:
this=Extension(self.tcx, params=extension['Number'], populate=False)
this.firstname=extension['FirstName']
this.surname=extension['LastName']
this.name="%s %s" % (this.firstname, this.surname)
this.dnd=extension['DND']
this.status=extension['CurrentProfile']
this.mail=extension['Email']
this.cli=extension['OutboundCallerId']
this.mobile=extension['MobileNumber']
this.online=extension['IsRegistered']
ret.append(this)
return ret
@property
def callslist(self):
if self.calllist is None:
self.calllist=ReadOnlyObject(self.tcx, api='activeCalls')
self.calllist.refresh()
res=self.calllist._result.json()['list']
if len(res)==0: return []
ret=[]
for call in res:
this=Call(self.tcx, call['Id'])
this.caller=call['Caller']
this.callee=call['Callee']
this.state=call['Status']
this.duration=res['Duration']
this.since=res['LastChangeStatus']
ret.append(this)
return ret
class Py3CX:
def __init__(self, uri=None, tls_verify=True):