100 lines
3.5 KiB
Python
100 lines
3.5 KiB
Python
# Environment var definitions
|
|
ENV_URI='TCX_SITE_URI'
|
|
ENV_AUTH_USER='TCX_API_AUTH_USERNAME'
|
|
ENV_AUTH_PASS='TCX_API_AUTH_PASSWORD'
|
|
|
|
class APIError(Exception):
|
|
pass
|
|
class ValidationError(Exception):
|
|
pass
|
|
|
|
class Request:
|
|
from requests import ConnectionError, exceptions
|
|
def __init__(self, uri, verify):
|
|
from requests import Session
|
|
self.base="%s/api/{}" % uri
|
|
self.sess=Session()
|
|
self.sess.verify=verify
|
|
|
|
def get(self, api, params=None, expect=200):
|
|
try:
|
|
resp=self.sess.get(url=self.base.format(api), params=params)
|
|
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:
|
|
raise APIError("TLS error returned when communicating; use tls_verify=False or check leaf certs: %s" % str(e))
|
|
except 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))
|
|
return resp
|
|
|
|
def post(self, api, params, expect=200):
|
|
try:
|
|
resp=self.sess.post(url=self.base.format(api), json=params)
|
|
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:
|
|
raise APIError("TLS error returned when communicating; use tls_verify=False or check leaf certs: %s" % str(e))
|
|
except 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))
|
|
except Exception as e:
|
|
raise APIError("Other exception raised during API call: %s" % str(e))
|
|
return resp
|
|
|
|
class Extension(object):
|
|
def __init__(self, parent, params):
|
|
self.tcx=parent
|
|
self.params=params
|
|
self.load()
|
|
|
|
def load(self):
|
|
res=self.tcx.rq.get('ExtensionList')
|
|
self.timestamp=res.headers.get('date')
|
|
res=list(filter(lambda ext: ext['Number'] == self.params, res.json()['list']))
|
|
assert (len(res)>0), "No extension found for: %s" % self.params
|
|
assert (len(res)==1), "More than one extension found for %s" % self.params
|
|
res=res[0]
|
|
self.number=res['Number']
|
|
self.firstname=res['FirstName']
|
|
self.surname=res['LastName']
|
|
self.name="%s %s" % (self.firstname, self.surname)
|
|
self.dnd=res['DND']
|
|
self.status=res['CurrentProfile']
|
|
self.mail=res['Email']
|
|
self.cli=res['OutboundCallerId']
|
|
self.mobile=res['MobileNumber']
|
|
self.online=res['IsRegistered']
|
|
|
|
|
|
class Py3CX:
|
|
def __init__(self, uri=None, tls_verify=True):
|
|
from os import getenv
|
|
self.uri=uri if uri is not None and uri.startswith('http') else getenv(ENV_URI, None)
|
|
assert (self.uri is not None and self.uri.startswith('http')), "Please provide URI"
|
|
self.uname=getenv(ENV_AUTH_USER, None)
|
|
self.passw=getenv(ENV_AUTH_PASS, None)
|
|
self.rq=Request(uri=self.uri, verify=tls_verify)
|
|
|
|
def authenticate(self, username=None, password=None):
|
|
if username is not None and password is not None:
|
|
self.uname=username
|
|
self.passw=password
|
|
assert (self.uname is not None and self.passw is not None), "Authentication information needed"
|
|
self.rq.post('login', params={
|
|
'Username': self.uname,
|
|
'Password': self.passw})
|
|
|
|
@property
|
|
def authenticated(self):
|
|
try:
|
|
self.rq.get('CurrentUser')
|
|
except APIError:
|
|
return False
|
|
return True
|
|
|