Services Module

services.services

TcEx Framework Service module

class tcex.services.services.Services(tcex)[source]

Bases: object

Service methods for customer Service (e.g., Triggers).

Parameters:tcex (object) – Instance of TcEx.
add_metric(label, value)[source]

Add a metric.

Metrics are reported in heartbeat message.

Parameters:
  • label (str) – The metric label (e.g., hits) to add.
  • value (int|str) – The value for the metric.
create_config(trigger_id, config, message, status)[source]

Add config item to service config object.

Parameters:
  • trigger_id (int) – The trigger ID for the current config.
  • config (dict) – The config for the current trigger.
  • message (str) – A simple message for the action.
  • status (str) – The passed/fail status for the App handling of config.
delete_config(trigger_id, message, status)[source]

Delete config item from config object.

Parameters:
  • trigger_id (int) – The trigger ID for the current config.
  • message (str) – A simple message for the action.
  • status (str) – The passed/fail status for the App handling of config.
fire_event(callback, **kwargs)[source]

Trigger a FireEvent command.

Parameters:callback (callable) – The trigger method in the App to call.
fire_event_publish(trigger_id, session_id, request_key=None)[source]

Send FireEvent command.

Parameters:
  • trigger_id (int) – The ID of the trigger.
  • session_id (str) – The generated session for this fired event.
  • request_key (str) – The request key for this response.
fire_event_trigger(callback, playbook, trigger_id, config, **kwargs)[source]

Fire event for trigger.

Parameters:
  • callback (callable) – The App callback method for firing an event.
  • playbook (tcex.Playbook) – A configure playbook instance for using to interact with KvStore.
  • trigger_id (int) – The current trigger Id.
  • config (dict) – A dict containing the configuration information.
format_query_string(params)[source]

Convert name/value array to a query string.

Parameters:params (dict) – The query params for the request.
Returns:The query params reformatted as a string.
Return type:str
format_request_headers(headers)[source]

Convert name/value array to a headers dict.

Parameters:headers (dict) – The dict of key/value header data.
Returns:The restructured header data.
Return type:dict
format_response_headers(headers)[source]

Convert name/value array to a query string.

Parameters:headers (dict) – The dict header data to be converted to key/value pairs.
Returns:The restructured header data.
Return type:dict
heartbeat()[source]

Start heartbeat process.

heartbeat_monitor()[source]

Publish heartbeat on timer.

increment_metric(label, value=1)[source]

Increment a metric if already exists.

Parameters:
  • label (str) – The metric label (e.g., hits) to increment.
  • value (int) – The increment value. Defaults to 1.
listen()[source]

List for message coming from broker.

listen_mqtt()[source]

Listen for message coming from broker.

listen_redis()[source]

Listen for message coming from broker.

loop_forever()[source]

Block and wait for shutdown.

message_thread(name, target, args, kwargs=None)[source]

Start a message thread.

Parameters:
  • name (str) – The name of the thread.
  • target (callable) – The method to call for the thread.
  • args (tuple) – The args to pass to the target method.
metrics

Return current metrics.

mqtt_client

Return the correct KV store for this execution.

on_connect(client, userdata, flags, rc)[source]

On connect method for mqtt broker.

on_log(client, userdata, level, buf)[source]

Handle MQTT on_log events.

on_message_mqtt(client, userdata, message)[source]

On message for mqtt.

on_message_redis(message)[source]

Subscribe and listen to “message” on Redis topic.

on_publish(client, userdata, result)[source]

Handle MQTT on_log events.

on_subscribe(client, userdata, mid, granted_qos)[source]

Handle MQTT on_log events.

playbook(session_id, variables)[source]

Return a configure playbook instance.

Parameters:
  • session_id (str) – The current session Id.
  • variables (list) – The requested output variables.
Returns:

An instance of Playbooks.

Return type:

tcex.Playbook

process_config(message)[source]

Process config message.

Parameters:message (dict) – The broker message.
process_run_service(message)[source]

Process Webhook event messages.

{
“command”: “RunService”, “apiToken”: “abc123”, “bodyVariable”: “request.body”, “headers”: [ { key/value pairs } ], “method”: “GET”, “queryParams”: [ { key/value pairs } ], “requestKey”: “123abc”

}

Parameters:message (dict) – The broker message.
process_run_service_response(*args, **kwargs)[source]

Handle service event responses.

(‘200 OK’, [(‘content-type’, ‘application/json’), (‘content-length’, ‘103’)])

process_shutdown(reason)[source]

Handle a shutdown message.

Parameters:reason (str) – The reason for the shutdown.
process_webhook(message)[source]

Process Webhook event messages.

Parameters:message (dict) – The message data from the broker.
publish(message, topic=None)[source]

Publish a message on client topic.

Parameters:
  • message (str) – The message to be sent on client topic.
  • topic (str) – The broker topic. Default to None.
ready

Return ready boolean.

redis_client

Return the correct KV store for this execution.

server_topic(message)[source]

Handle any event coming in on server_topic.

Parameters:message (dict) – The broker message.
static session_id(trigger_id=None)[source]

Return a uuid4 session id.

Parameters:trigger_id (str) – Optional trigger_id value used in testing framework.
session_logfile

Return a uuid4 session id.

thread_name

Return a uuid4 session id.

update_metric(label, value)[source]

Update a metric if already exists.

Parameters:
  • label (str) – The metric label (e.g., hits) to update.
  • value (int|str) – The updated value for the metric.