Les connexions sortantes REST permettent d'invoquer des endpoints externes basés sur REST.
Par exemple, supposons qu'il existe un endpoint POST REST à l'adresse https://example.com qui attend :
{"facturation" : "395.7", "monnaie" : "EUR"}
X-App-Name : Zato " et " X-Environment : Production
Une connexion sortante et un service l'utilisant se lisent comme suit :
# -*- coding: utf-8 -*-
# Zato
from zato.server.service import Service
class SetBillingInfo(Service):
""" Updates a customer's billing information.
"""
def handle(self):
# Python dict representing the payload we want to send across
payload = {'billing':'395.7', 'currency':'EUR'}
# Python dict with all the query parameters, including path and query string
params = {'cust_id':'39175', 'phone_no':'271637517', 'priority':'normal'}
# Headers the endpoint expects
headers = {'X-App-Name': 'Zato', 'X-Environment':'Production'}
# Obtains a connection object
conn = self.outgoing.plain_http['SetBillingInfo'].conn
# Invoke the resource providing all the information on input
response = conn.post(self.cid, payload, params, headers=headers)
Notes :
En tant que programmeur, vous n'avez jamais besoin de quitter Python Land, la (dé)sérialisation est automatique et vous pouvez travailler uniquement avec des classes de données ou des dictionnaires Python.
L'objet réponse offre un accès à l'ensemble du corps, des en-têtes et des autres métadonnées que l'endpoint renvoie.
L'accès aux endpoints JSON peut être encore simplifié grâce à l'adaptateur JSON dédié.