There is such a term as Remote Procedure Call (RPC). In other words, by using this technology, programs can call functions on remote computers. There are many ways to implement RPC. Here are some of them related to web technologies and this is what we will focus on:
Each of these technologies can be used in Django. In this article we will look at the libraries which add RPC support for Django.
Let's start with the most common protocol - REST. REST stands for Representational State Transfer - transfer of the state representation. In fact, REST is an architectural style, not the protocol. For example, SOAP is a protocol. Due to its popularity and prevalence there is a large selection of libraries for Django which allow you to turn your views into REST views by making simple actions.
In the world of Django REST API there are a few of the most popular libraries, and we will compare them. In comparison table I have identified the most important factors, in my opinion, that are relevant when choosing the library.
|Django REST framework||Tastypie||Piston||Django XML-RPC||Django REST Pandas|
|Version||3.5.3 (Production/Stable)||0.13.3 (Beta)||0.2.3 (Alpha)||0.1.7 (Production/Stable)||0.5.0 (Production/Stable)|
|Python 3 support||Yes||Yes||No||Yes0||Yes|
|API key authentication||Yes||Yes||No||-||-|
|Accept Headers||Yes||Yes||No||-||via DRF|
|Serializations||JSON, JSONp, XML, YAML, HTML, MessagePack, CSV||JSON, JSONp, XML, YAML, HTML, plist||XML, JSON, YAML, Pickle, Django||-||CSV, TXT, XLS, XLSX, JSON, PNG, SVG|
So, we can summarize, if your API architecture is complex (mobile devices support, versioning, Mongo models serialization), it is better to choose Django REST framework. Tastypie and Piston can be used for small, simple projects.
Nowadays, REST is the most popular. But there may be situations when you need to have an interface for legacy products. The library, which adds SOAP support in django, is called spyne. Let's see how this works in django. First, install the
pip install spyne package and add
'rpctest.core' into INSTALLED_APPS.
Let's make a curl request to check our test application:
As a result, the output we get:
As you can see, there is nothing complicated, spyne library does all the work. Spyne has a lot of settings, for example, if it is necessary to get JSON instead of XML as a response from the API, it is done as follows:
Now the response from the API will be rendered in JSON
Apart from Django, spyne.io supports Twisted and Pyramid.
Protocol Buffers is a new serialization protocol, which was proposed by Google as XML replacement. As Google claims, buffers protocol is easier, faster and smaller than XML. Officially, there is support for C ++, C #, Go, Java and Python. Other languages are supported by third-party developers.
The first thing you need is to create .proto file, where the structure will be described.
In our example, the
Person object is described: it has two mandatory fields -
id/name, one optional field -
PhoneNumber field, which can contain user’s multiple phone numbers.
When .proto is created, it should be compiled. This is done with help of protobuf library.
person_pb2.py file will be created.
Now imagine that we have a view, which shows information about the user. As an example, I will use the simplest way to show how Protocol Buffer can work in Django.
In 2015, Facebook introduced a new standard in the public declaration of data structures and methods of obtaining data - GraphQL. Among the main features of GraphQL we can point out the next ones:
As you can see from the article, among all the technologies for API implementation in Django, REST leads by the number of libraries. The popularity of REST is characterized by its simplicity and functionality. It is also important to note that the data is transmitted without the use of additional layers, so REST is considered to be less resource-intensive, unlike SOAP or XML-RPC. But it also has disadvantages, like any other technology. When you select a backend for the API, be guided by your needs rather than popularity.
In one of our recent projects, we had an interesting case: the whole application was built around an interactive map for a fairly large shopping mall, and the main goal of the system was to plot the closest route to a user-selected destination.
SQL is a fairly complicated language with a steep learning curve. For a large number of people who make use of SQL, learning to apply it efficiently takes lots of trials and errors. Here are some tips on how you can make your SELECT queries better. The majority of tips should be applicable to any relational database management system, but the terminology and exact namings will be taken from PostgreSQL.