emhub.client#
This module define the DataClient
class to communicate with an existing
EMHub server via its REST API.
By default, the DataClient
class will use the configuration read from
os.environ in the config
class.
A helper function open_client
is provided for creating a context
where a DataClient
instance is created, logged in and out.
- class emhub.client.config#
Store configuration variables read from os.environ.
- EMHUB_SERVER_URL#
EMhub server URL (e.g. https://emhub.org or http://127.0.0.1:5000).
- EMHUB_USER#
Username for login into the remote EMhub server.
- EMHUB_PASSWORD#
Password for login into the server.
- emhub.client.open_client()#
Context creation for login/logout with a
DataClient
using the configuration inconfig
.
- class emhub.client.DataClient(server_url=None)#
Client class to communicate with a remote EMhub server via the REST API.
- create_session(attrs)#
Request to create a new
Session
in the server.- Parameters:
attrs (dict) – Dict with the attributes of the session.
attrs
should of the form:{ 'name': 'SessionName', 'start': '2023-07-27 09:00' 'end': '2023-07-27 23:59' }
- Returns:
The JSON result from the request.
- delete_session(attrs)#
Request the server to delete a session.
- Parameters:
attrs (dict) – Attributes to be deleted.
id
must be inattrs
.- Returns:
The JSON result from the request with deleted session.
- get_active_sessions()#
Return all sessions that are active.
- get_session(sessionId, attrs=None)#
Retrieve data from the
Session
with thissessionId
.- Parameters:
sessionId – Id of the session to retrieve.
attrs – What attributes to retrieve, if None all attributes will be returned.
Examples
with open_client() as dc: # Retrieving all attributes from Session 100 sid = 100 s1 = dc.get_session(sid) # Just fetching session name and start date s2 = dc.get_session(sid, ['name', 'start'])
- json()#
Retrieve the result of the last Request as JSON.
- login(username=None, password=None)#
Login into the EMhub server with the given credentials.
It is required to login before most of the operations that requires an authenticated user.
- Parameters:
username (str) – The username to login
password (str) – password to login.
- Returns:
The request result of the login operation.
Examples
Login to a remote server providing all credentials:
dc = DataClient('https://emhub.org') dc.login('admin', 'admin')
Login using default variables in
config
:dc = DataClient() dc.login()
- logout()#
Logout the user from the EMhub server.
- request(method, jsonData=None, bp='api')#
Make a request to the server sending this
jsonData
.- Parameters:
method (str) – Method (or endpoint) to send the request in the server.
jsonData (dict) – Data to be sent to the remote endpoint.
bp (str) – Blueprint in the server where to send the request. By default it will use the ‘api’ blueprint.
- Returns:
The request object result from the request.
- class emhub.client.TaskHandler(worker, task)#
- error(msg)#
Log some error using the internal logger.
- getLogPrefix()#
Internal function to have a unique Log prefix.
- info(msg)#
Log some info using the internal logger.
- process()#
Process the given task. This method should be implemented in subclasses. This function will be called many times while processing the task. The stop method will signal to stop the main loop.
- request_config(config)#
Shortcut function to
request_data
that retrieve a config form.
- request_data(endpoint, jsonData=None)#
Make a request to one of the server’s endpoints.
- Parameters:
jsonData – arguments that will send as JSON to the request.
- request_dict(endpoint, jsonData=None)#
Shortcut function to
request_data
that return the result as a dict.
- run()#
Implement thread’s activity, running an infite loop calling
process
until thestop
method is called.
- stop()#
Stop the current thread.
- update_task(event, tries=-1, wait=10)#
Update task info.
- Parameters:
event – info that will be sent as part of the update
tries – try this many times to update the task, if less than zero, try forever
wait – seconds to wait between tries