Integrating Django with exchange rate web services in 10 lines of code

Posted: 2013-06-13 18:05 [Source]
Tags:  django esb soa web-services integrations how-to planet-python yql

Summary: The post introduces Zato, an open-source integration platform in Python, and shows you how to integrate Django, or indeed any piece of Python software, with Zato and external web services using nothing but plain Python objects.

Applications in any programming language can be integrated using Zato but being written in Python itself, Zato offer a convenience client for software in Python and that will be used throughout the text.

Zato is a lightweight, yet complete, ESB (Enterprise Service Bus). And the project's goal is to become a powerful, yet lightweight, one.

Start here for a gentle introduction to what ESB and SOA (Service-Oriented Architecture) are about, but in short, they let you integrate multiple applications each potentially using different formats, protocols and programming languages with the aim of supporting interesting processes you need to automate. And with Zato this is all in pure Python with as little headaches as possible.

How things should stand

As a Python programmer, about the only thing I feel I should need in order to invoke web services exposed by any sort of systems is a simple API based on dicts or other dict-like objects, like Bunch.

It should be always possible to write code like what is below and expect it will just work regardless of the complexity of underlying protocols and data transports.

Given that it's a blog of the Zato project it won't come as a surprise that I am about to tell you that Zato allows you to achieve just that, to think in terms of services and dictionaries without having to worry about how everything is actually implemented underneath.

You delegate the job of an actual integration to Zato which becomes the component responsible for dealing with protocols and data formats, fetching information, straightening it and returning to you a unified view. This lets you focus on your job only and nicely follows the UNIX philosophy of separating software into clearly defined blocks interoperating in order to achieve an interesting result. Not to mention that this what the integrations industry has been using to tackle such scenarios for decades now.

This way you can focus on your own app, not on data integration. Someone else takes care of it.

The overall scheme

The diagram depicts what we will achieve:

../images/exchange-rates/process.png
  • Users enters a currency code to find EUR exchange rates to in an HTML form

  • A Django application invokes a Zato client providing a Python dictionary with currencies selected on input.

    Behind the scenes, the dictionary is converted into an HTTP JSON call but this is completely transparent to you as a Django programmer.

  • Zato receives the call already converted to a Bunch instance and invokes 3 web services provided by:

  • Output from 3 different sources is converted to a clean Pythonic response sent back to Django

  • Django app receives a list of dictionaries on output ready to use in a template which is shown to the user

Implementation

Django side

First, clone this repository (we'll call the directory you'll clone it to DJANGO_APP_DIR) and run DJANGO_APP_DIR/install.sh - this will use install or upgrade distribute and virtualenv and use pip/buildout to download a couple of dependencies and install everything under virtualenv.

DJANGO_APP_DIR$ git clone git://github.com/zatosource/zato-django-integration.git .
DJANGO_APP_DIR$ ./install.sh
[snip]
DJANGO_APP_DIR$ ./bin/py sampleapp/src/run.py

You can now go to http://127.0.0.1:8188 and witness an '[Errno 111] Connection refused' error. This is OK. Zato is not running yet.

What you can already have a look though is the Django code. Basically, a middleware class is used to inject a Zato client and the client is used to invoke a service which will be defined in the next steps.

Let's see, this is how the middleware looks like..

And here's the view..

If it were a project where you'd be doing Django programming only then you could congratulate yourself. The code shown above is everything you need to write to invoke a Zato service and fetch the exchange rates.

This is 10 lines of Python code, counting imports or class definitions in. Without the boilerplate, it will be 2 or 3 lines of code needed to invoke web services.

OK, there's also a trivial piece of HTML, the gist of which is here ..

but that's it. There is nothing else on Django side, job well done!

Zato side

First thing is, read at least the first part of the tutorial. This will install Zato and create a quickstart cluster.

Done? OK, let's continue.

Save the code below as exchangerates.py..