Prerequisites:
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 List of Books
Description
: Create a new Resource class to return all books.
Steps
:
-
Make a copy of your code from the previous week
-
Create a new resource class
-
Add a new method called get to your resource class
The method must return all books in the dataset which can be ordered (ascending or descending order) by any of columns
You can use query parameters to pass the column name and order type
.
-
You need to use a decorator called
expect()
which accepts a request parser as input; this will automatically help flask-restplus to generate appropriate swagger doc which includes inputs for query parameters
-
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-2: Add a new Book
Description
: Add another endpoint to the resource class which allow users to add new books.
Steps
:
-
Make a copy of your code from the previous activity
-
Create a new method named "post"
Details of a new book should be posted via request payload.
The endpoint must return appropriate error message if the given id (book identifier) already exists in the dataset
-
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.
Activity-3: Improving Swagger Documentation
Description
: So far we did not manually created the swagger documentation; however, still the documentation can be improved by manually adding some information (e.g. API description, parameter description)
Steps
:
-
Make a copy of your code from the previous activity
-
When creating an instance of API class,
specify the title, description, default namespace, and version of the API.
-
Use
@api.response
to indicate which HTTP response code does each method returns and what they mean
-
Use
@api.param
to add descriptions for the parameters
-
Use
@api.doc
to add descriptions for API methods
Resource created Thursday 02 August 2018, 08:22:55 AM, last modified Friday 08 March 2019, 05:35:08 PM.