Compare commits

..

No commits in common. "e6f93150a016e48fd9e4fd96e87f986cb0be4500" and "80fb8cd7cc328767a45cf87da39d1954662b2800" have entirely different histories.

7 changed files with 89 additions and 63 deletions

View File

@ -1,6 +1,6 @@
# Mitel OMMClient2 # Mitel OMMClient2
Another attempt for a modern client library to the Mitel OM Application XML Interface. Another attempt for a client library to the Mitel OM Application XML Interface.
## Quicksart ## Quicksart
@ -16,10 +16,10 @@ c = mitel_ommclient2.OMMClient2("omm.local", "admin", "admin")
c.ping() c.ping()
# Create custom messages # Create custom messages
r = c.request(mitel_ommclient2.messages.Ping(timeStamp=2342)) r = c.session.request(mitel_ommclient2.messages.Ping(timeStamp=2342))
# Craft your own request, if it is not implemented yet # Craft your own request, if it is not implemented yet
r = c.request(mitel_ommclient2.messages.DictRequest("Ping", {"timeStamp": 2342})) r = c.session.request(mitel_ommclient2.messages.DictRequest("Ping", {"timeStamp": 2342}))
``` ```
Consult class documentation for more in depth examples and options. Consult class documentation for more in depth examples and options.

View File

@ -4,22 +4,6 @@ mitel\_ommclient2.messages package
Submodules Submodules
---------- ----------
mitel\_ommclient2.messages.getaccount module
--------------------------------------------
.. automodule:: mitel_ommclient2.messages.getaccount
:members:
:undoc-members:
:show-inheritance:
mitel\_ommclient2.messages.getppdev module
------------------------------------------
.. automodule:: mitel_ommclient2.messages.getppdev
:members:
:undoc-members:
:show-inheritance:
mitel\_ommclient2.messages.open module mitel\_ommclient2.messages.open module
-------------------------------------- --------------------------------------

View File

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

View File

@ -1,7 +1,7 @@
.. _api: .. _api:
API Documentation mitel_ommclient2
================= ================
.. toctree:: .. toctree::
:maxdepth: 4 :maxdepth: 4

View File

@ -3,5 +3,6 @@
from . import client from . import client
from . import connection from . import connection
from . import messages from . import messages
from . import session
from .client import OMMClient2 from .client import OMMClient2

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from .connection import Connection from .session import Session
from . import messages from . import messages
class OMMClient2: class OMMClient2:
@ -13,53 +13,29 @@ class OMMClient2:
:param host: Hostname or IP address of the OMM :param host: Hostname or IP address of the OMM
:param username: Username :param username: Username
:param password: Password :param password: Password
:param port: Port where to access the API, if None, use default value :param session: A :class:`mitel_ommclient2.session.Session` object
:param ommsync: If True login as OMM-Sync client. Some operations in OMM-Sync mode might lead to destroy DECT paring.
Usage:: Usage::
>>> c = OMMClient2("omm.local", "admin", "admin") >>> c = OMMClient2("omm.local", "admin", "admin")
>>> c.ping() >>> c.ping()
Use request to send custom messages:: Use session for not implemented features::
>>> r = s.request(mitel_ommclient2.messages.Ping()) >>> r = s.session.request(mitel_ommclient2.messages.Ping())
To get more contol over the connection handling, initialize
:class:`mitel_ommclient2.session.Session` manually::
>>> s = mitel_ommclient2.session.Session("omm.local", "admin", "admin", port=12345)
>>> c = OMMClient2(session=s)
""" """
def __init__(self, host, username, password, port=None, ommsync=False): def __init__(self, host=None, username=None, password=None, session=None):
self._host = host if session is None:
self._username = username self.session = Session(host, username, password)
self._password = password else:
self._port = port self.session = session
self._ommsync = ommsync
# prepare connect arguments
kwargs = {}
if self._port is not None:
kwargs["port"] = self._port
# Connect
self._connection = Connection(self._host, **kwargs)
self._connection.connect()
# Login
r = self.request(messages.Open(self._username, self._password, UserDeviceSyncClient=self._ommsync))
r.raise_on_error()
def request(self, request):
"""
Sends a request, waits for response and returns response
:param request: Request object
Usage::
>>> r = c.request(mitel_ommclient2.messages.Ping())
>>> r.name
'PingResp'
"""
return self._connection.request(request)
def get_account(self, id): def get_account(self, id):
""" """
@ -68,7 +44,7 @@ class OMMClient2:
:param id: User id :param id: User id
""" """
r = self.request(messages.GetAccount(id)) r = self.session.request(messages.GetAccount(id))
r.raise_on_error() r.raise_on_error()
if r.account is None: if r.account is None:
return None return None
@ -80,7 +56,7 @@ class OMMClient2:
:param id: Device id :param id: Device id
""" """
r = self.request(messages.GetPPDev(ppn)) r = self.session.request(messages.GetPPDev(ppn))
r.raise_on_error() r.raise_on_error()
if r.pp is None: if r.pp is None:
return None return None
@ -93,7 +69,7 @@ class OMMClient2:
Returns `True` when response is received. Returns `True` when response is received.
""" """
r = self.request(messages.Ping()) r = self.session.request(messages.Ping())
if r.errCode is None: if r.errCode is None:
return True return True
return False return False

View File

@ -0,0 +1,57 @@
#!/usr/bin/env python3
from time import sleep
from . import connection
from . import messages
class Session:
"""
Synchronous API session handler
:param host: Hostname or IP address of OMM
:param username: Username
:param password: Password
:param port: Port
:param ommsync: If True login as OMM-Sync client. Some operations in OMM-Sync mode might lead to destroy DECT paring.
:param connection_class: One of :class:`mitel_ommclient2.connection.Connection` or :class:`mitel_ommclient2.connection.SSLConnection`
Usage::
>>> s = Session("omm.local", "admin", "admin")
>>> s.request(mitel_ommclient2.messages.Ping())
"""
def __init__(self, host, username, password, port=None, ommsync=False, connection_class=None):
self.host = host
self.username = username
self.password = password
self.port = port
self.ommsync = ommsync
self.connection_class = connection_class
if self.connection_class is None:
self.connection_class = connection.Connection
self._connection = None
self._ensure_connection()
def _ensure_connection(self):
"""
Make sure we are connected and logged in
"""
if self._connection is None:
kwargs = {}
if self.port is not None:
kwargs["port"] = self.port
self._connection = self.connection_class(self.host, **kwargs)
self._connection.connect()
# Login
r = self.request(messages.Open(self.username, self.password, UserDeviceSyncClient=self.ommsync))
r.raise_on_error()
def request(self, request):
return self._connection.request(request)