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 index.py 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 functions.py 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 functions.py, 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 index.py
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 🙂