03 - Backend API

We are going to start building the backend using FASTAPI, which is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.

What is API Layer?

API is an application programming interface that allows interactions with different services like Web, Mobile & any other devices. The below diagram gives a quick overview of API Layer. As you see the API layer is common which interacts with the database and has most of the server logic in place. Also, the API layer helps in maintaining the same server logic without duplicating when adding a new interface like mobile or any device which can interact on HTTP protocol.

image.png

Building and maintaining APIs is very important to building any web or mobile applications and takes most of the time. There are many frameworks available that allow building backend without coding, however, there is little room for customization in those platforms or limitations of user passwords getting tied up to platforms. If someone is interested in exploring no-code platforms for the backend then you can check Xano.

To build the API layer we started using FAST API and the following commit gives the code of the same(github.com/svsrikanth2001/Dev_To_DevOps/com..)

Following is an explanation of the code

./ - Main folder

  • requirements.txt - contains the list of packages to be installed for running this
  • main.py - the first which runs webserver - uvicorn. Read about uvicorn here
  • app\api.py - a python file that declares the main route and imports all routers
  • app\routers - code which adds the path where the programs can communicate with each other for specific functionality. We shall also declare the type of call being made whether it is a GET/POST/PUT/DELETE

So let's see what are these types of calls

  1. GET - used to query data from the system Ex: Get the list of students from the system
  2. POST - used to send data to the system and create or update resources Ex: add a new student/ update existing student
  3. PUT - used to send data to the system and create or update resources. The main difference between the POST and PUT calls is, that PUT is idempotent. That is, calling the same endpoint multiple times will always produce the same result. In contrast, calling a POST endpoint multiple times will have the effect of creating the same resource multiple times.
  4. DELETE - deletes the specified resource.

Open the file applicant.py where you will see the end points added with following code

@router.post("/search", response_model=List[ApplicantReadModel], tags=[router_tag]) def search(Applicant: ApplicantSearchModel , db: Session = Depends(get_db)): crud = ApplicantCrud(db_session=db, db_model=ApplicantModel) records = crud.get_by_args(model_object_fields=Applicant) return records router.post specifies that call is of type POST. Response model is list of ApplicantReadModel and input model is ApplicantSeachModel and we have dependency injection. We shall see all the details one at a time Response Model - this specifies how the response will look when the execution call is completed. It is a JSON model and represents data in a specific model so that the program which requested the call can expect a response in the specific schema.

Input model defined in the search method specifies how the POST end point expects the data to process and response. It is also a JSON model

Dependency Injection - its a model of providing the object that an object needs instead of constructing them. Like in this case when we execute API calls we need a database object to interact with database,however no where in the code we declare the database object. It is injected into object using the dependency injection model. If you want to understand mode then please read this link

Models

..\API-Backend\app\models - folder contains the request/response models used which is validated using the pydantic models These models allow the validation of data.

Database

..\API-Backend\app\database - contains all classes required for database interaction

  1. connection.py - class which makes a connection to postgresql database and yields a session
  2. ..\models - contains the class representation of the table
    1. applicant.py - has a class representation of applicant_login
  3. ..\crud - contains the class which handles the crud operations with databases for each table we create a class and inherit the methods of the parent class

To run the code locally do the following

  • setup virtual environment
  • run the file API-Backend\main.py using python file and it should run the server

Open the url - localhost:8081 (which is defined in main.py file) The url which opens up swagger url and allows to test the end points.

Use url - 127.0.0.1:8081/redoc to generate the documentation of the API.

image.png