Creating responses
Assign a string representation of JSON to self.response.payload - it doesn’t matter
how it’s produced as long as it results in a string, hence all the examples below
achieve the same.
| from zato.server.service import Service
class MyService(Service):
def handle(self):
self.response.payload = """{"customer": {"products": [{"type": "AMZN",
"id": 1}, {"type": "KZUA", "id": 2}], "id": 123, "name": "John Brown"}}"""
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | # anyjson
from anyjson import dumps
# Zato
from zato.server.service import Service
class MyService(Service):
def handle(self):
response = {
'customer': {
'id':123,
'name':'John Brown',
'products': [
{'id':1, 'type':'AMZN'},
{'id':2, 'type':'KZUA'}
]}}
self.response.payload = dumps(response)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 | # anyjson
from anyjson import dumps
# bunch
from bunch import Bunch
# Zato
from zato.server.service import Service
class MyService(Service):
def handle(self):
response = Bunch()
response.customer = Bunch()
response.customer.id = 123
response.customer.name = 'John Brown'
response.customer.products = []
response.customer.products.append({'id':1, 'type':'AMZN'})
response.customer.products.append({'id':2, 'type':'KZUA'})
self.response.payload = dumps(response)
|
Output below reformatted for clarity.
| $ curl localhost:11223/example -d '{}'
{"customer":
{"products": [
{"type": "AMZN", "id": 1},
{"type": "KZUA", "id": 2}],
"id": 123,
"name": "John Brown"}}
$
|
Invoking a JSON service using GET
Create a
plain HTTP
outgoing connection to a remote JSON service,
such as this one provided by Yahoo!
and
invoke
it through self.outgoing.plain_http.
The response is a string that can be turned into a Python dictionary or a
Bunch
instance, like in the examples below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | # anyjson
from anyjson import loads
# Zato
from zato.server.service import Service
class MyService(Service):
def handle(self):
response = self.outgoing.plain_http.get('Yahoo! Finance').conn.get(self.cid)
data = loads(response.text)
for item in data['list']['resources']:
name = item['resource']['fields']['name']
price = item['resource']['fields']['price']
self.logger.info('{} {}'.format(name, price))
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | # anyjson
from anyjson import loads
# bunch
from bunch import bunchify
# Zato
from zato.server.service import Service
class MyService(Service):
def handle(self):
response = self.outgoing.plain_http.get('Yahoo! Finance').conn.get(self.cid)
data = bunchify(loads(response.text))
for item in data.list.resources:
name = item.resource.fields.name
price = item.resource.fields.price
self.logger.info('{} {}'.format(name, price))
|
Invoking a JSON service using POST
Create a
plain HTTP outgoing connection
to a remote JSON service and
invoke
it
through self.outgoing.plain_http passing the input JSON document as a string
to the .post method.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | # anyjson
from anyjson import dumps
# Zato
from zato.server.service import Service
class MyService(Service):
def handle(self):
cust_profile = self.outgoing.plain_http.get('CRM Customer Profile')
request = dumps({'cust_id':1, 'name':'Foo Bar'})
response = cust_profile.conn.post(self.cid, request)
self.logger.info(response.text)
|