Understanding the tasks performed by each HTTP method

The following table shows the HTTP verbs, the scope, and the semantics for the methods that our new API must support. Each method is composed by an HTTP verb and a scope and all the methods have well-defined meanings for all the resources and collections.

HTTP verb

Scope

Semantics

GET

Collection of game categories

Retrieve all the stored game categories in the collection, sorted by their name in ascending order. Each game category must include a list of URLs for each game resource that belongs to the category.

GET

Game category

Retrieve a single game category. The game category must include a list of URLs for each game resource that belongs to the category.

POST

Collection of game categories

Create a new game category in the collection.

PUT

Game category

Update an existing game category.

PATCH

Game category

Update one or more fields of an existing game category.

DELETE

Game category

Delete an existing game category.

GET

Collection of games

Retrieve all the stored games in the collection, sorted by their name in ascending order. Each game must include its game category description.

GET

Game

Retrieve a single game. The game must include its game category description.

POST

Collection of games

Create a new game in the collection.

PUT

Game category

Update an existing game.

PATCH

Game category

Update one or more fields of an existing game.

DELETE

Game category

Delete an existing game.

GET

Collection of players

Retrieve all the stored players in the collection, sorted by their name in ascending order. Each player must include a list of the registered scores, sorted by score in descending order. The list must include all the details for the score achieved by the player and its related game.

GET

Player

Retrieve a single player. The player must include a list of the registered scores, sorted by score in descending order. The list must include all the details for the score achieved by the player and its related game.

POST

Collection of players

Create a new player in the collection.

PUT

Player

Update an existing player.

PATCH

Player

Update one or more fields of an existing player.

DELETE

Player

Delete an existing player.

GET

Collection of scores

Retrieve all the stored scores in the collection, sorted by score in descending order. Each score must include the player's name that achieved the score and the game's name.

GET

Score

Retrieve a single score. The score must include the player's name that achieved the score and the game's name.

POST

Collection of scores

Create a new score in the collection. The score must be related to an existing player and an existing game.

PUT

Score

Update an existing score.

PATCH

Score

Update one or more fields of an existing score.

DELETE

Score

Delete an existing score.

We want our API to be able to update a single field for an existing resource, and therefore, we will provide an implementation for the PATCH method. The PUT method is meant to replace an entire resource and the PATCH method is meant to apply a delta to an existing resource. In addition, our RESTful API must support the OPTIONS method for all the resources and collection of resources.

We don't want to spend time choosing and configuring the most appropriate ORM, as seen in our previous API; we just want to finish the RESTful API as soon as possible to start interacting with it. We will use all the features and reusable elements included in Django REST Framework to make it easy to build our API. We will work with a PostgreSQL database. However, in case you don't want to spend time installing PostgreSQL, you can skip the changes we make in Django REST Framework ORM configuration and continue working with the default SQLite database.

In the preceding table, we have a huge number of methods and scopes. The following list enumerates the URIs for each scope mentioned in the table, where {id} has to be replaced with the numeric id or the primary key of the resource:

  • Collection of game categories: /game-categories/
  • Game category: /game-category/{id}/
  • Collection of games: /games/
  • Game: /game/{id}/
  • Collection of players: /players/
  • Player: /player/{id}/
  • Collection of scores: /player-scores/
  • Score: /player-score/{id}/

Let's consider that http://localhost:8000/ is the URL for the API running on the Django development server. We have to compose and send an HTTP request with the following HTTP verb (GET) and request URL (http://localhost:8000/game-categories/) to retrieve all the stored game categories in the collection:

GET http://localhost:8000/game-categories/ 
..................Content has been hidden....................

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