In this chapter, we will start our journey towards RESTful Web APIs with Python and four different Web frameworks. Python is one of the most popular and versatile programming languages. There are thousands of Python packages, which allow you to extend Python capabilities to any kind of domain you can imagine. We can work with many different Web frameworks and packages to easily build simple and complex RESTful Web APIs with Python, and we can also combine these frameworks with other Python packages.
We can leverage our existing knowledge of Python and its packages to code the different pieces of our RESTful Web APIs and their ecosystem. We can use the object-oriented features to create code that is easier to maintain, understand, and reuse. We can use all the packages that we already know to interact with databases, Web services, and different APIs. Python makes it easy for us to create RESTful Web APIs. We don't need to learn another programming language; we can use the one we already know and love.
In this chapter, we will start working with Django and Django REST Framework, and we will create a RESTful Web API that performs CRUD (Create, Read, Update, and Delete) operations on a simple SQLite database. We will:
Imagine that we have to start working on a mobile App that has to interact with a RESTful API to perform CRUD operations with games. We don't want to spend time choosing and configuring the most appropriate ORM ( Object-Relational Mapping); we just want to finish the RESTful API as soon as possible to start interacting with it via our mobile App. We really want the games to persist in a database but we don't need it to be production-ready, and therefore, we can use the simplest possible relational database, as long as we don't have to spend time making complex installations or configurations.
Django REST framework, also known as DRF, will allow us to easily accomplish this task and start making HTTP requests to our first version of our RESTful Web Service. In this case, we will work with a very simple SQLite database, the default database for a new Django REST framework project.
First, we must specify the requirements for our main resource: a game. We need the following attributes or fields for a game:
bool
value indicating whether the game was played at least once by a player or notIn addition, we want our database to save a timestamp with the date and time in which the game was inserted in the database.
The following table shows the HTTP verbs, the scope, and the semantics for the methods that our first version of the API must support. Each method is composed by an HTTP verb and a scope and all the methods have a well defined meaning for all games and collections.
HTTP verb |
Scope |
Semantics |
|
Collection of games |
Retrieve all the stored games in the collection, sorted by their name in ascending order |
|
Game |
Retrieve a single game |
|
Collection of games |
Create a new game in the collection |
|
Game |
Update an existing game |
|
Game |
Delete an existing game |
3.144.39.133