Book Image

Building RESTful Python Web Services

By : Gaston C. Hillar
Book Image

Building RESTful Python Web Services

By: Gaston C. Hillar

Overview of this book

Python is the language of choice for millions of developers worldwide, due to its gentle learning curve as well as its vast applications in day-to-day programming. It serves the purpose of building great web services in the RESTful architecture. This book will show you the best tools you can use to build your own web services. Learn how to develop RESTful APIs using the popular Python frameworks and all the necessary stacks with Python, Django, Flask, and Tornado, combined with related libraries and tools. We will dive deep into each of these frameworks to build various web services, and will provide use cases and best practices on when to use a particular framework to get the best results. We will show you everything required to successfully develop RESTful APIs with the four frameworks such as request handling, URL mapping, serialization, validation, authentication, authorization, versioning, ORMs, databases, custom code for models and views, and asynchronous callbacks. At the end of each framework, we will add authentication and security to the RESTful APIs and prepare tests for it. By the end of the book, you will have a deep understanding of the stacks needed to build RESTful web services.
Table of Contents (18 chapters)
Building RESTful Python Web Services
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Preface

Understanding the tasks performed by each HTTP method


In the preceding table, the GET HTTP verb appears twice but with two different scopes. The first row shows a GET HTTP verb applied to a collection of games (collection of resources) and the second row shows a GET HTTP verb applied to a game (a single resource).

Let's consider that http://localhost:8000/games/ is the URL for the collection of games. If we add a number and a slash (/) to the preceding URL, we identify a specific game whose id or primary key is equal to the specified numeric value. For example, http://localhost:8000/games/12/ identifies the game whose id or primary key is equal to 12.

We have to compose and send an HTTP request with the following HTTP verb (POST) and request URL (http://localhost:8000/games/) to create a new game. In addition, we have to provide the JSON (JavaScript Object Notation) key-value pairs with the field names and the values to create the new game. As a result of the request, the server will validate the provided values for the fields, make sure that it is a valid game and persist it in the database.

The server will insert a new row with the new game in the appropriate table and it will return a 201 Created status code and a JSON body with the recently added game serialized to JSON, including the assigned id or primary key that was automatically generated by the database and assigned to the game object.

POST http://localhost:8000/games/ 

We have to compose and send an HTTP request with the following HTTP verb (GET) and request URL (http://localhost:8000/games/{id}/) to retrieve the game whose id or primary key matches the specified numeric value in the place where {id} is written.

For example, if we use the request URL http://localhost:8000/games/50/, the server will retrieve the game whose id or primary key matches 50.

As a result of the request, the server will retrieve a game with the specified id or primary key from the database and create the appropriate game object in Python. If a game is found, the server will serialize the game object into JSON and return a 200 OK status code and a JSON body with the serialized game object. If no game matches the specified id or primary key, the server will return just a 404 Not Found status:

GET http://localhost:8000/games/{id}/ 

We have to compose and send an HTTP request with the following HTTP verb (PUT) and request URL (http://localhost:8000/games/{id}/) to retrieve the game whose id or primary key matches the specified numeric value in the place where {id} is written and replace it with a game created with the provided data. In addition, we have to provide the JSON key-value pairs with the field names and the values to create the new game that will replace the existing one. As a result of the request, the server will validate the provided values for the fields, make sure that it is a valid game and replace the one that matches the specified id or primary key with the new one in the database. The id or primary key for the game will be the same after the update operation. The server will update the existing row in the appropriate table and it will return a 200 OK status code and a JSON body with the recently updated game serialized to JSON. If we don't provide all the necessary data for the new game, the server will return a 400 Bad Request status code. If the server doesn't find a game with the specified id, the server will return just a 404 Not Found status.

PUT http://localhost:8000/games/{id}/ 

We have to compose and send an HTTP request with the following HTTP verb (DELETE) and request URL (http://localhost:8000/games/{id}/) to remove the game whose id or primary key matches the specified numeric value in the place where {id} is written. For example, if we use the request URL http://localhost:8000/games/20/, the server will delete the game whose id or primary key matches 20. As a result of the request, the server will retrieve a game with the specified id or primary key from the database and create the appropriate game object in Python. If a game is found, the server will request the ORM to delete the game row associated with this game object and the server will return a 204 No Content status code. If no game matches the specified id or primary key, the server will return just a 404 Not Found status.

DELETE http://localhost:8000/games/{id}/