Coders Packet

Python code to create a REST API to log/modify library details in a database

By Srivatsan Sridhar

Python code which creates a REST API to log/modify library logs when a user borrows/renews/returns a book, using flask, flask_mysqldb (python), Postman API tester application and MySQL.

This packet contains source code that creates a REST API to store and access data from an online database (MySQL). Here, we consider the case of librarians who log/modify details when a user borrows/renews/returns a book, and this data can be accessed at any point of time, for instance to check whether the user is still within the due date of returning the book. The python code is used to create endpoints on a local host and obtain user details from a form structure, which is give using Postman.

The required frameworks/libraries are flask framework and flask_mysqldb library of python. We will also be needing the Postman API tester, which is a separate application and MySQL database. We have to first create a MySQL host, corresponding password, database and a table in that database. This can either be done on the MySQL application or on the command prompt by wriitng MySQL queries. You have to enter these details in the code (there is an indication where you have to enter the details). You can name the table anything according to your convenience, but make sure you change it in the code as well (in the MySQL queries). This is the configuration step in python.

This REST API will be hosted on the localhost. For each task (i.e., to borrow/return/renew a book or to fetch detials), you will require a route to an endpoint of the localhost. Each route will take up one of four requests (this is for our case - normally any route can take any combination of requests)- POST (to add data to a database), GET (to obtain data from a database), PUT (to modify existing data on a database) and DELETE (to delete existing data on a database). Within each route the function to be executed when that endpoint is reached is written. For example, in the app route to create a log when a user borrows a book, the method is a post method and the function written is to make sure the data obtained from the form is properly stored in a table in the database which was configured initially. This is done for all four cases. If you wish to modify/add any utility to the existing code by creating a new route, the same process must be followed. Since we are using a utility to integrate MySQL with flask, the SQL queries to add/modify data in the database have to be written in python itself, within the corresponding app route (for more information regarding this utility, kindly refer to the flask_mysqldb documentation).

Once you are done coding the python source file, you have to run it. The code provided will automatically start running once you save the file. Then you have to switch to the Postman application. In the Postman application you have to create a collection, with an endpoint method it will be executing. The endpoint will have to be an endpoint of the localhost. In that collection, you will have to add the form fields that you want to enter. Remember to keep the same name for the form field in Postman and the python dictionary in the code from which you obtain the details. Once you enter the details you want to for a specific endpoint you can send the request and you will receive the output in Postman itself. You can verify whether the code is working by checking if you are getting the correct output. You can now check your MySQL table to see if the data corresponds to all your requests/operations.

This is the basic technique to create a REST API using flask. The same process can be used for other scenarios as well. Some important points are -

1) Make sure you provide the correct SQL credentials.

2) Make sure the values entered in Postman are the same as the corresponding values entered in the python code.

3) Mention the API request(s) correctly in the app route initiation.

4) Make sure the SQL queries which are written in the python source file correctly correspond to the table and field names in the SQL database.

Download Complete Code


No comments yet