2022-01-06 16:09:55 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
from xml.dom.minidom import getDOMImplementation, parseString
|
|
|
|
|
2022-01-06 17:28:19 +01:00
|
|
|
from ..exceptions import exception_classes, OMResponseException
|
|
|
|
|
2022-01-06 16:09:55 +01:00
|
|
|
|
|
|
|
class Request:
|
|
|
|
"""
|
|
|
|
Request message class
|
|
|
|
|
|
|
|
:param name: Name of the message
|
|
|
|
:param seq: Unique sequence number to associate responses
|
|
|
|
|
|
|
|
Usage::
|
|
|
|
>>> req = Request("Ping")
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, name, seq=None):
|
|
|
|
self.name = name
|
|
|
|
self.attrs = {}
|
|
|
|
self.childs = {}
|
|
|
|
|
|
|
|
if seq is not None:
|
|
|
|
self.attrs["seq"] = seq
|
|
|
|
|
|
|
|
@property
|
|
|
|
def seq(self):
|
|
|
|
return self.attrs.get("seq")
|
|
|
|
|
|
|
|
|
|
|
|
class DictRequest(Request):
|
|
|
|
"""
|
|
|
|
Create a message by dict attributes
|
|
|
|
|
|
|
|
:param name: Name of the message
|
|
|
|
:param attrs: Message attributes
|
|
|
|
:param childs: Message children
|
|
|
|
|
|
|
|
Usage::
|
|
|
|
>>> req = DictRequest("Ping", {"timeStamp": 2342})
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, name, attrs={}, childs={}):
|
|
|
|
self.name = name
|
|
|
|
self.attrs = attrs
|
2022-01-16 13:47:06 +01:00
|
|
|
self.childs = childs
|
2022-01-06 16:09:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Response:
|
|
|
|
"""
|
|
|
|
Response message class
|
|
|
|
|
|
|
|
:param name: Name of the message
|
|
|
|
:param attrs: Message attributes
|
|
|
|
:param childs: Message children
|
|
|
|
"""
|
|
|
|
def __init__(self, name, attrs={}, childs={}):
|
|
|
|
self.name = name
|
|
|
|
self.attrs = attrs
|
|
|
|
self.childs = childs
|
|
|
|
|
2022-01-06 17:28:19 +01:00
|
|
|
def raise_on_error(self):
|
|
|
|
"""
|
|
|
|
Raises an exception if the response contains an error.
|
|
|
|
|
|
|
|
Usage::
|
|
|
|
|
|
|
|
>>> try:
|
|
|
|
>>> r.raise_on_error()
|
|
|
|
>>> except mitel_ommclient2.exceptions.EAuth as e:
|
|
|
|
>>> print("We don't care about authentication!")
|
|
|
|
|
|
|
|
See children of :class:`mitel_ommclient2.exceptions.OMResponseException` for all possible exceptions.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self.errCode is not None:
|
2022-01-06 23:56:35 +01:00
|
|
|
raise exception_classes.get(self.errCode, OMResponseException)(response=self)
|
2022-01-06 17:28:19 +01:00
|
|
|
|
2022-01-06 16:09:55 +01:00
|
|
|
@property
|
|
|
|
def seq(self):
|
|
|
|
return self.attrs.get("seq")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def errCode(self):
|
|
|
|
return self.attrs.get("errCode")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def info(self):
|
|
|
|
return self.attrs.get("info")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def bad(self):
|
|
|
|
return self.attrs.get("bad")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def maxLen(self):
|
|
|
|
return self.attrs.get("maxLen")
|
|
|
|
|
2022-01-06 18:13:48 +01:00
|
|
|
from .getaccount import GetAccount, GetAccountResp
|
2022-01-16 13:52:08 +01:00
|
|
|
from .getppdev import GetPPDev, GetPPDevResp
|
2022-01-06 23:53:52 +01:00
|
|
|
from .open import Open, OpenResp
|
2022-01-06 16:09:55 +01:00
|
|
|
from .ping import Ping, PingResp
|
|
|
|
|
|
|
|
def construct(request):
|
|
|
|
"""
|
|
|
|
Builds the XML message DOM and returns as string
|
|
|
|
"""
|
|
|
|
impl = getDOMImplementation()
|
|
|
|
message = impl.createDocument(None, request.name, None)
|
|
|
|
root = message.documentElement
|
|
|
|
|
|
|
|
for k, v in request.attrs.items():
|
|
|
|
root.setAttribute(str(k), str(v))
|
|
|
|
|
|
|
|
|
|
|
|
for k, v in request.childs.items():
|
|
|
|
child = message.createElement(k)
|
|
|
|
if v is not None:
|
|
|
|
for c_k, c_v in v.items():
|
|
|
|
child.setAttribute(str(c_k), str(c_v))
|
|
|
|
root.appendChild(child)
|
|
|
|
return root.toxml()
|
|
|
|
|
|
|
|
def _response_type_by_name(name):
|
|
|
|
response_types = [
|
2022-01-06 18:13:48 +01:00
|
|
|
GetAccountResp,
|
2022-01-16 13:52:08 +01:00
|
|
|
GetPPDevResp,
|
2022-01-06 16:09:55 +01:00
|
|
|
PingResp,
|
|
|
|
]
|
|
|
|
|
|
|
|
response_types_dict = {r.__name__: r for r in response_types}
|
|
|
|
|
|
|
|
return response_types_dict.get(name, Response)
|
|
|
|
|
|
|
|
def parse(message):
|
|
|
|
message = parseString(message)
|
|
|
|
root = message.documentElement
|
|
|
|
|
|
|
|
name = root.tagName
|
|
|
|
attrs = {}
|
|
|
|
childs = {}
|
|
|
|
|
|
|
|
for i in range(0, root.attributes.length):
|
|
|
|
item = root.attributes.item(i)
|
|
|
|
attrs[item.name] = item.value
|
|
|
|
|
|
|
|
child = root.firstChild
|
|
|
|
while child is not None:
|
|
|
|
new_child = {}
|
|
|
|
for i in range(0, child.attributes.length):
|
|
|
|
item = child.attributes.item(i)
|
|
|
|
new_child[item.name] = item.value
|
|
|
|
|
|
|
|
childname = child.tagName
|
|
|
|
if childname in childs:
|
|
|
|
childs[childname].append(new_child)
|
|
|
|
else:
|
|
|
|
childs[childname] = [new_child]
|
|
|
|
|
|
|
|
child = child.nextSibling
|
|
|
|
|
|
|
|
|
|
|
|
return _response_type_by_name(name)(name, attrs, childs)
|