In this lab, you will learn how to build your own RESTful service and document it using Flask-Restplus.
Prerequisites:
REST (Representational State Transfer) is an architecture, which allows developers to build lightweight and scalable web services. REST services allow heterogeneous systems communicate together using JSON objects. It is assumed that you will install and take a look at the following packages in python before heading to activities:
You do not need to learn how to create a flask web application (e.g. Jinja2 is not necessary).
For testing your RESTful APIs, you will need to be familiar with
All of the following activities will be based on the
Book dataset
(
Download the CSV file
)
Activity-1: Get a Book by its ID
Description
: In this activity, you need to read the CSV file using panda, and provide a RESTful API which returns a Book instance from the dataset by its ID.
Steps
:
-
Create a flask-restplus application
-
Read the dataset using Pandas as a global variable
-
Drop the unnecessary columns as you did in Week 3's Lab(Activity-1)
Likewise apply all cleansing techniques in the same Lab (Activity-2)
-
Set the index of the loaded dataframe to be the "Identifier" column
; this will help us to find books with their ids
-
Replace the spaces in the column names
-
Create a Resource class and implement its "get" method:
The given method must accept "id" as an input and return the book matching the id
by querying the dataframe
or
by index
. The response must be a JSON.
When there is no book matching the id, the method must return a proper message and
HTTP response code
.
-
Run the application and browse
http://127.0.0.1:5000/
to test the endpoint. The given URL will show an automatically generated swagger doc for your application; you can use the swagger doc to test your API
Activity-2: Removing a Book by its ID
Description
: Add another endpoint to the service to let users delete rows from the dataset.
Steps
:
-
Make a copy of your code from the previous activity
-
Add a new method called delete to your resource class
Inside the new function, delete the given book by its id
, and return a proper message indicating that the given id has been removed
When there is no book matching the id, the method must return a proper message and HTTP response code.
-
Run the application and browse
http://127.0.0.1:5000/
to test the endpoint. Test your API to see if it works properly.
Activity-3:Update Book Information
Description
: Add another endpoint to the service to let users update book details.
Steps
:
-
Make a copy of your code from the previous activity
-
Add a new method called "put" to your resource class:
By convention, "put" requests are used to update the details of resources; that is why in this activity, you need to add a method called "put" to your resource class.
-
Using the
expect()
decoder, specify an example payload for the endpoint
-
Check if the given book identifier exists,
and return a proper message if it does not exist
-
Get the payload
of request
-
Iterate over the key-values of the payload and update the dataframe
Return error messages if the input includes unexpected keys
-
Run the application and browse
http://127.0.0.1:5000/
to test the endpoint. Test your API to see if it works properly.
Resource created Friday 27 July 2018, 09:16:21 PM, last modified Friday 08 March 2019, 05:34:58 PM.