Skip to content

Instantly share code, notes, and snippets.

@dsuch
Last active August 29, 2015 14:15
Show Gist options
  • Save dsuch/129d4d217010d9bb7b7d to your computer and use it in GitHub Desktop.
Save dsuch/129d4d217010d9bb7b7d to your computer and use it in GitHub Desktop.
How to use async callbacks
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
# Zato
from zato.server.service import Service
ORDER_CACHE_PREFIX = 'order.{}'
class AcceptOrder(Service):
name = 'esb.order.accept'
def handle(self):
# Store context in Redis
ctx = {'my':'important data'}
self.kvdb.conn.hmset(ORDER_CACHE_PREFIX.format(self.cid), ctx)
# Easier to work with Bunch objects
async_request = self.request.bunchified()
async_request.cid = self.cid
# Invoke the adapter in async
self.invoke_async('esb.feasibility-study', async_request, callback='esb.order.confirm')
class FeasibilityStudy(Service):
name = 'esb.feasibility-study'
class SimpleIO:
input_required = ('cid', 'address', 'product')
output_required = ('result', 'cid')
def handle(self):
# Connect to inventory backend and check if we can offer a line on that address
conn = self.outgoing.plain_http['My Inventory'].conn
# In practice, interesting data would be extracted from response here
response = conn.post(self.cid, {'address':'abc', 'product':'My 4G'})
self.response.payload = {'result':'Ok, can be done', 'cid':self.request.input.cid}
class ConfirmOrder(Service):
name = 'esb.order.confirm'
def handle(self):
# Look up context in Redis
ctx = self.kvdb.conn.hgetall(ORDER_CACHE_PREFIX.format(self.request.cid))
# Do something useful based on context here
pass
# Notify the initial caller of the result
conn = self.outgoing.plain_http['My Client'].conn
# Here we just pass the response in as-is
conn.post(self.cid, self.request.payload)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment