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 :

  1. Create a flask-restplus application
  2. Read the dataset using Pandas as a global variable
  3. 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)
  4. Set the index of the loaded dataframe to be the "Identifier" column ; this will help us to find books with their ids
  5. Replace the spaces in the column names
  6. 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 .
  7. 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 :

  1. Make a copy of your code from the previous activity
  2. 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.
  3. 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 :

  1. Make a copy of your code from the previous activity
  2. 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.
  3. Using the expect() decoder, specify an example payload for the endpoint
  4. Check if the given book identifier exists, and return a proper message if it does not exist
  5. Get the payload of request
  6. Iterate over the key-values of the payload and update the dataframe
    Return error messages if the input includes unexpected keys
  7. 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.


Back to top

COMP9321 18s2 (Data Services Engineering) is powered by WebCMS3
CRICOS Provider No. 00098G