Designing a RESTful API to interact with a simple SQLite database

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 (short for Object-Relational Mapping); we just want to finish the RESTful API as soon as possible to start interacting with it in 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.

We need the shortest possible development time. Django Rest Framework (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:

  • An integer identifier
  • A name or title
  • A release date
  • An ESRB (short for Entertainment Software Rating Board) rating description, such as T (Teen) and EC (Early Childhood). You can read more about the ESRB at http://www.esrb.org
  • A bool value indicating whether the game was played at least once by a player or not
  • An integer indicating the number of times the game was played

In 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 the games and the collection of games:

HTTP verb

Scope

Semantics

GET

Collection of games

Retrieve all the stored games in the collection, sorted by their name in ascending order

GET

Game

Retrieve a single game

POST

Collection of games

Create a new game in the collection

PUT

Game

Update an existing game

DELETE

Game

Delete an existing game

In a RESTful API, each resource has its own unique URL. In our API, each game has its own unique URL.

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

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