Further rejiggering of classes

This commit is contained in:
Maff 2019-05-21 20:02:25 +01:00
parent 740bb07a64
commit 0bb7fb94cd
1 changed files with 275 additions and 276 deletions

View File

@ -50,17 +50,17 @@ class Request:
class BasicObject(object): class BasicObject(object):
pass pass
class ReadOnlyObject(object): class APIObject(object):
def __init__(self, parent, api): def __init__(self, parent, api):
self.tcx=parent self.tcx=parent
self.api=api self.api=api
class ReadOnlyObject(APIObject):
def refresh(self, params={}): def refresh(self, params={}):
self._result=self.tcx.rq.get(self.api, params=params) self._result=self.tcx.rq.get(self.api, params=params)
self.active=self._result.json() self.active=self._result.json()
class TransactionalObject(object): class TransactionalObject(APIObject):
def __init__(self, parent, api, save='edit/save', discard='edit/cancel'): def __init__(self, parent, api, save='edit/save', discard='edit/cancel'):
self.tcx=parent super().__init__(parent, api)
self.api=api
self.save=save self.save=save
self.discard=discard self.discard=discard
def create(self, params): def create(self, params):
@ -73,7 +73,8 @@ class TransactionalObject(object):
self.tcx.rq.post(self.discard, params={ self.tcx.rq.post(self.discard, params={
'Id':self._session}) 'Id':self._session})
class Call(ReadOnlyObject): class Py3CX:
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
@ -94,8 +95,7 @@ class Call(ReadOnlyObject):
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 +115,9 @@ class User(TransactionalObject):
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=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 +141,7 @@ class Extension(ReadOnlyObject):
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
@ -213,7 +213,7 @@ class PhoneSystem:
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=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)
@ -234,7 +234,7 @@ class PhoneSystem:
if len(res)==0: return [] if len(res)==0: return []
ret=[] ret=[]
for call in res: for call in res:
this=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']
@ -242,8 +242,6 @@ class PhoneSystem:
this.since=res['LastChangeStatus'] this.since=res['LastChangeStatus']
ret.append(this) ret.append(this)
return ret return ret
class Py3CX:
def __init__(self, uri=None, tls_verify=True): def __init__(self, uri=None, tls_verify=True):
from os import getenv from os import getenv
self.uri=uri if uri is not None and uri.startswith('http') else getenv(ENV_URI, None) self.uri=uri if uri is not None and uri.startswith('http') else getenv(ENV_URI, None)
@ -251,6 +249,7 @@ class Py3CX:
self.uname=getenv(ENV_AUTH_USER, None) self.uname=getenv(ENV_AUTH_USER, None)
self.passw=getenv(ENV_AUTH_PASS, None) self.passw=getenv(ENV_AUTH_PASS, None)
self.rq=Request(uri=self.uri, verify=tls_verify) self.rq=Request(uri=self.uri, verify=tls_verify)
self.__tcxsystem=None
def authenticate(self, username=None, password=None): def authenticate(self, username=None, password=None):
if username is not None and password is not None: if username is not None and password is not None:
self.uname=username self.uname=username
@ -270,7 +269,7 @@ class Py3CX:
@property @property
def System(self): def System(self):
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=__PhoneSystem(self)
return self.tcxsystem return self.__tcxsystem