Python client

Zato provides a convenience client, called AnyServiceInvoker, for use by other Python applications to invoke services in a given cluster. Because Zato services can be exposed through many methods, there is no hard requirement that Python applications use the client, instead it’s rather meant to be an easy to use API client that a Zato user would like to have in any case.

CLI’s zato service invoke and the web admin both use AnyServiceInvoker.

AnyServiceInvoker

__init__

__init__(address, path, auth=None, session=None, to_bunch=False, max_response_repr=DEFAULT_MAX_RESPONSE_REPR, max_cid_repr=DEFAULT_MAX_CID_REPR, logger=None)
Parameters:
  • address (string) – Address using the format of http://host:port of a Zato server or load-balancer a service to be invoked is defined on
  • path (string) – URL path of a channel the service to be invoked is accessed through
  • auth (tuple) – Optional tuple of (username, password), in plain text, corresponding to an HTTP Basic Auth definition the channel is secured with
  • to_bunch (bool) – Whether the response should be converted to a Bunch instance before it’s returned
  • max_response_repr (int) – If using a response’s __repr__ method, when it’s being printed out on screen for instance, how many characters of the response actually to output. Defaults to zato.client.DEFAULT_MAX_RESPONSE_REPR which is 2500 characters.
  • max_cid_repr (int) – Used in the same circumstances as max_response_repr. A CID has always 40 characters, however, when logging responses on screen it’s sometimes desirable to limit the numbers of characters printed on either end of a CID. For instance, if CID is 4d38731837f90a3b5906fbc0 and max_cid_repr is 5, __repr__ will use ‘4d387..6fbc0’ with any characters in between suppressed. max_cid_repr defaults to 5 and you can use the zato.client.CID_NO_CLIP constant to turn the clipping feature off.

invoke

invoke(name=None, payload='', headers=None, channel='invoke', data_format='json', transport=None, id=None, to_json=True, output_repeated=ZATO_NOT_GIVEN)

Invokes a service synchronously, the client will block waiting for the response.

Parameters:
  • name (string) – Name of the service to invoke. Either name or id must be provided.
  • payload (any) – Payload to invoke the service with. May be a Python dictionary if it’s a SIO service and to_json is True.
  • headers (dict) – HTTP headers to pass in to the service invoker
  • channel (zato.common.CHANNEL) – Channel the service will be invoked with
  • data_format (zato.common.DATA_FORMAT) – Data format the service will be invoked with
  • transport (zato.common.TRANSPORT) – Transport the service will be invoked over
  • id (int) – ID of the service to invoke. Either name or id must be provided.
  • to_json (bool) – Whether payload should be converted to JSON
  • output_repeated (bool) – Whether the expected output is repeated. If left at zato.common.ZATO_NON_GIVEN, it will be assumed to be true if the service name ends in ‘list’, so that ‘my.application.customer.get-list’ will by default be assumed to return a SIO list of objects
Return type:

A Response object

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from zato.client import AnyServiceInvoker

address = 'http://localhost:11223'
path = '/zato/admin/invoke'
auth = ('admin.invoke', 'ccd77ca1ff414c3e8308e3f35d8292df')

client = AnyServiceInvoker(address, path, auth)
response = client.invoke('zato.ping')

if response.ok:
    print(response.data)
else:
    print(response.details)
$ py myclient.py
{u'pong': u'zato'}
$

invoke_async

Invokes a service asynchronously. Works exactly like invoke except that the client isn't blocked. Instead a message to invoke the service is published in Redis and client receives the CID a service has been invoked with so that its actual response can be correlated with the client request at a later time.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from zato.client import AnyServiceInvoker

address = 'http://localhost:11223'
path = '/zato/admin/invoke'
auth = ('admin.invoke', 'ccd77ca1ff414c3e8308e3f35d8292df')

client = AnyServiceInvoker(address, path, auth)
response = client.invoke_async('zato.ping')

if response.ok:
    print(response.data)
else:
    print(response.details)
$ py myclient.py
f53b59e2215cc7cb608d0382
$

Response object

The result of invoking a service, no matter if in a blocking way or asynchronously, is always a Response object which provides access to the underlying data a service produced along with a couple of useful attributes.

Depending on the client class used, the response attributes may hold data of different types.

Name Datatype Description
inner requests.Response The inner HTTP response as returned by the requests library
ok boolean Whether the invocation succeeded, True if zato_env.result is equal to ZATO_OK
has_data boolean

If data a service produced is available. Will be set to True only if ok is True. Otherwise, if ok is not True, details will contain error information.

Note however that both ok and data can be True if a service was invoked with no problems yet it didn't return any output.

data (depends) Service response in format depending on the client class used. Will be available only if ok and has_data are True. If a service returns JSON document, the document converted to a Python object. Otherwise, service response as a string.
details (depends) Taken from zato_env.details