Building a Professional RestFul API in Python – Part 1

Before starting this series of posts, I wanted to thank the comments that you have been leaving on my blog, as they encourage me to continue posting things on this site. If you have read the title of this post you will notice that it is not very related to the publications that I have done so far, but yes, I am dedicated to web programming 馃檪

As I go through the project, all the code will be available on my GitHub. The code that I’m going to show in this post is based on the commit: 6917f480f74a3e03c15c37fdbca2ca0f09548ac

To not mix the versions of the dependencies of our project, let’s create a new python environment following the instructions of the following link link

We are gonna use Flask, it’s gonna be useful for creating the routes into our project, install that dependence by following the steps from the link below link

First of all, let’s check the file. That file is the responsible to declare not only the routes for the API, it’s also the place for locating the different methods for the URLs

This may seem overwhelming at first, but you will understand it in 5 minutes, we will follow the code line by line

First of all, we declare the environment of the app, which will be the relative path where flask is installed in our python env, created at the beginning. Then, Flask and our custom LIBs are imported. Next, we do the same importing our routes, the data models and the custom module. So before continue we need to understand the main lib until the moment, which is the file located inside utils folder.

As you can observe all the functions are located inside a “try catch” block, that’s because we are building a professional API 馃槈 In case some of the code returns an error during the execution, the endpoint is not gonna return the technical error to the client, it’s gonna return the human error. That logic can be found on the function “setModuleError and setModuleSuccess” which uses the model “_rest” imported at the beginning of the file (check the image below)

The “setStatus” function is used for checking that the request status that is returned to the client contents a valid status code for being interpreted on the client side (just for verifying). And the other one “flaskResp” is the responsible of creating a valid return construction for being send from the Flask module in our app

Volviendo de nuevo a nuestro archivo, podemos observar en cada Excepci贸n del c贸digo, c贸mo se crea una instancia del nuevo “error de objeto de respuesta” y est谩 listo para devolver los datos del resultado a la ruta ubicada en el archivo

As you can see on the last image, we need to construct the “setFlaskResponse” object explained before for sending the response and the status code assigned if the result is success or not.

On that image I made a first request from the left terminal, then I introduced a syntax error on the code and then I made again the same request on the right terminal. As you can observe from the terminal on the top, you can read the technical error and the human error response sent back to the user.

The next post will be for the signup 馃檪

Join the Conversation

1 Comment

Leave a comment

Your email address will not be published. Required fields are marked *