Using Flask to build a RESTful web service

Since the REST concepts are built on the HTTP protocol, a RESTful API is an extension to an HTTP service. For robust, high-performance, secure operations, a common practice is to build on a server such as Apache HTTPD or NGINX. These servers don't support Python directly; they require an extension module to interface with a Python application. 

Any interface between externally-facing web servers and Python will adhere to the Web Services Gateway Interface (WSGI). For more information, see http://www.wsgi.org. The Python standard library includes a WSGI reference implementation. See PEP 3333, http://www.python.org/dev/peps/pep-3333/, for details on this standard. See https://wiki.python.org/moin/WebServers for a number of web servers that support WSGI. When working with NGINX, for example, the uWSGI plugin provides the necessary bridge to Python.

The WSGI standard defines a minimal set of features shared by all Python web frameworks. It's challenging to work with, however, because many features of web services aren't part of this minimal interface. For example, authentication and session management can be implemented in a way that adheres to the WSGI standard, but it can be rather complex.

There are several high-level application frameworks that adhere to the WSGI standard. When we build a RESTful web service, we'll often use a framework that makes it very easy to create our application. For the examples in this chapter, we'll use the Flask framework. For more information, see http://flask.pocoo.org/docs/1.0/

A Flask application is an instance of the Flask class. An essential feature of Flask is the routing table to map URI paths to specific functions. We'll start with a simple application with a few routes to show how this works. First, however, we need some objects to transfer via RESTful services.

Let's take a look at the problems faced while transferring domain objects. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset