Compare commits

..

3 Commits

Author SHA1 Message Date
f62579ce90 Add GetAccount 2022-01-06 18:13:48 +01:00
496b915c9d Stop on failed login 2022-01-06 17:59:12 +01:00
9f9df79ab8 Introduce exception handling 2022-01-06 17:28:19 +01:00
6 changed files with 227 additions and 2 deletions

View File

@ -28,6 +28,14 @@ mitel\_ommclient2.connection module
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
mitel\_ommclient2.exceptions module
-----------------------------------
.. automodule:: mitel_ommclient2.exceptions
:members:
:undoc-members:
:show-inheritance:
mitel\_ommclient2.session module mitel\_ommclient2.session module
-------------------------------- --------------------------------

View File

@ -37,6 +37,18 @@ class OMMClient2:
else: else:
self.session = session self.session = session
def get_account(self, id):
"""
Get account
:param id: User id
"""
r = self.session.request(message.GetAccount(id))
r.raise_on_error()
return r.account[0]
def ping(self): def ping(self):
""" """
Is OMM still there? Is OMM still there?

View File

@ -0,0 +1,156 @@
#!/usr/bin/env python3
exception_classes = {}
def _collect_exception_class(c):
"""
Decorator that collects exception classes for parsing error codes.
"""
exception_classes[c.__name__] = c
return c
class OMResponseException(Exception):
def __init__(self, response, msg=None):
self.response = response
if msg is None:
msg = self.response.info
super().__init__(msg)
@_collect_exception_class
class EAreaFull(OMResponseException):
pass
@_collect_exception_class
class EAuth(OMResponseException):
pass
@_collect_exception_class
class EDectRegDomainInvalid(OMResponseException):
pass
@_collect_exception_class
class EEncryptNotAllowed(OMResponseException):
pass
@_collect_exception_class
class EExist(OMResponseException):
pass
@_collect_exception_class
class EFailed(OMResponseException):
pass
@_collect_exception_class
class EForbidden(OMResponseException):
pass
@_collect_exception_class
class EInProgress(OMResponseException):
pass
@_collect_exception_class
class EInval(OMResponseException):
def __init__(self, response):
super().__init__(response, response.bad)
@_collect_exception_class
class EInvalidChars(OMResponseException):
pass
@_collect_exception_class
class ELicense(OMResponseException):
pass
@_collect_exception_class
class ELicenseFile(OMResponseException):
pass
@_collect_exception_class
class ELicenseWrongInstallId(OMResponseException):
pass
@_collect_exception_class
class EMissing(OMResponseException):
def __init__(self, response):
super().__init__(response, response.bad)
@_collect_exception_class
class ENoEnt(OMResponseException):
pass
@_collect_exception_class
class ENoMem(OMResponseException):
pass
@_collect_exception_class
class EPerm(OMResponseException):
pass
@_collect_exception_class
class EPwEmpty(OMResponseException):
pass
@_collect_exception_class
class EPwSimilarToHost(OMResponseException):
pass
@_collect_exception_class
class EPwSimilarToName(OMResponseException):
pass
@_collect_exception_class
class EPwTooManySimilarChars(OMResponseException):
pass
@_collect_exception_class
class EPwTooShort(OMResponseException):
pass
@_collect_exception_class
class EPwTooSimilar(OMResponseException):
pass
@_collect_exception_class
class EPwTooWeak(OMResponseException):
pass
@_collect_exception_class
class EPwUnchanged(OMResponseException):
pass
@_collect_exception_class
class ETooLong(OMResponseException):
def __init__(self, response):
super().__init__(response, response.bad + ", maximum of " + str(response.maxLen))
@_collect_exception_class
class EWlanRegDomainInvalid(OMResponseException):
pass

View File

@ -2,6 +2,8 @@
from xml.dom.minidom import getDOMImplementation, parseString from xml.dom.minidom import getDOMImplementation, parseString
from ..exceptions import exception_classes, OMResponseException
class Request: class Request:
""" """
@ -58,6 +60,23 @@ class Response:
self.attrs = attrs self.attrs = attrs
self.childs = childs self.childs = childs
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:
raise exception_classes.get(self.errCode, OMResponseException)()
@property @property
def seq(self): def seq(self):
return self.attrs.get("seq") return self.attrs.get("seq")
@ -78,10 +97,9 @@ class Response:
def maxLen(self): def maxLen(self):
return self.attrs.get("maxLen") return self.attrs.get("maxLen")
from .getaccount import GetAccount, GetAccountResp
from .ping import Ping, PingResp from .ping import Ping, PingResp
def construct(request): def construct(request):
""" """
Builds the XML message DOM and returns as string Builds the XML message DOM and returns as string
@ -104,6 +122,7 @@ def construct(request):
def _response_type_by_name(name): def _response_type_by_name(name):
response_types = [ response_types = [
GetAccountResp,
PingResp, PingResp,
] ]

View File

@ -0,0 +1,27 @@
#!/usr/bin/env python3
from . import Request, Response
class GetAccount(Request):
def __init__(self, id, maxRecords=None, **kwargs):
super().__init__("GetAccount", **kwargs)
self.attrs["id"] = id
if maxRecords is not None:
self.attrs["maxRecords"] = maxRecords
@property
def id(self):
return self.attrs.get("id")
@property
def maxRecords(self):
return self.attrs.get("maxRecords")
class GetAccountResp(Response):
@property
def account(self):
return self.attrs.get("account")

View File

@ -57,10 +57,13 @@ class Session:
self._connection.connect() self._connection.connect()
# Login
self._connection.send(messages.Open(self.username, self.password)) self._connection.send(messages.Open(self.username, self.password))
res = self._wait_for_respose() res = self._wait_for_respose()
res.raise_on_error()
def request(self, request): def request(self, request):
""" """
Sends a request and waits for response Sends a request and waits for response