Ejemplo n.º 1
0
def json_call(service, data, config):
    """Make a call to a Zato service from a set list with JSON POST data.

    The service URLs are configured in the SERVICE_URL module global
    dictionary with the service name as written in the header of the Zato
    service documentation page as the key.

    @params service: service name
    @param data: dictionary of data to send a JSON post data
    @param config: configuration dictionary as read from 'deploy.conf'

    """
    address = 'http://%s:%s' % (config.lb_host, config.lb_port)
    try:
        path = SERVICE_URLS[service]
    except KeyError:
        log.error("Unknown zato JSON service name: 5s", service)
        raise

    auth = (config.http_user, config.http_password)
    client = JSONClient(address, path, auth)
    log.debug("Invoking service at '%s' with data: %s", path, data)
    res = client.invoke(data)

    if not res.ok:
        raise JSONCallResponseError(
            "Zato non-successful result code: {}".format(res))

    return bunchify(res.data)
# -*- coding: utf-8 -*-

# Zato
from zato.client import AnyServiceInvoker, JSONClient

# Our app
from settings import CLIENT_ADDRESS, CLIENT_CREDENTIALS, CLIENT_PATH_ANY, CLIENT_PATH_JSON

client_any = AnyServiceInvoker(CLIENT_ADDRESS, CLIENT_PATH_ANY, CLIENT_CREDENTIALS)
client_json = JSONClient(CLIENT_ADDRESS, CLIENT_PATH_JSON, CLIENT_CREDENTIALS)

class ZatoMiddleware(object):
    def process_request(self, req):
        req.client_any = client_any
        req.client_json = client_json