Skip to main content

FastAPI first shot

Setup on my Mac (Macbook Pro 15 inch Retina, Mid 2014)

Prerequisite

  • Python 3.6+ (I used 3.7.x. I recently reinstalled OS after cleaning up disk, where stock Python 2.7 was available. I installed Pyenv and then used it to install 3.7.x).
  • I already had a git repo initialized at Github for this project. I checked that out. I use this approach to keep all the source code safe or at a specific place 😀.
  • I set the Python version in .python-version file.
  • I also initialize the virtual environment using pyenv in venv folder.
  • I started the virtual environment.

FastAPI specific dependencies setup

Now I started with basic pip commands to install dependency for the project. I saved dependencies in requirements.txt the file.

Minimal viable code to spin an API Server

FastAPI is as cool as NodeJS or Go Lang (?) to demonstrate the ability to spin an API endpoint up and running in no time. I had the same feeling for the Flask too, which was also super cool.

app/main.py:


from typing import Optional

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def read_root():
    return {"Hello": "World"}


@app.get("/items/{item_id}")
async def read_item(item_id: int, q: Optional[str] = None):
    return {"item_id": item_id, "q": q}


I started using the following command


uvicorn app.main:app --reload

The function read_root() is doing the following stuff:

  1. Create a REST API with GET method at path /.
  2. It returns a JSON response {"Hello": "World"}.
Root API run output on Swagger
Root API run output on Swagger

I got two API endpoints up and running along with auto-generated Swagger documentation and ReDoc documentation. Pretty neat hah!.

Receiving JSON Payload in Request Body

  • We should define a model which will represent the received request body JSON payload. (I am sure we must have some way to handle the raw data, that we will explore later.)
  • Pydantic based model is used in FastAPI which provides Data validation and settings management using python type annotations.

Pydantic Item Model & Usage in a Route


...
from pydantic import BaseModel
...

class Item(BaseModel):
    name: str
    price: float
    is_offer: Optional[bool] = None
...

@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
    return {"item_name": item.name, "item_id": item_id}
...


  • To make the Pydantic model we extend our typical class to use pydantic.BaseModel
  • update_item() function define parameters with type hint which include Pydantic model Item.
  • FastAPI will automatically validate the incoming JSON payload based on the definition of the model.
  • Validation errors etc will automatically handle by FastAPI.

Avoid sending None in Response JSON

Sometimes the records in the database contain None or Null values. It looks pretty bad if we have many None value attributes in Response JSON.

To make sure we are not sending attributes with None values in response, we can use the path operation decorator parameter response_model_exclude_unset.

E.g.


...

class ArtistModel(BaseModel):
	id: str
	name: str
	age: Optional[int]
	

@app.post("/artists/", response_model=List[ArtistModel], response_model_exclude_unset=True)

async def get_artists():

	artists = await db["artists"].find().to_list(1000)

	return artists

...

Note:

  1. Above is a partial code fragment. No import etc.
  2. db["artists"].find().to_list(1000) brings data from MongoDB.
  3. If age the attribute has None value in the database, it will be excluded in response JSON.

Handling Query Parameters

Refer FastAPI documentation Query parameters page for the latest information.

Request URL = http://localhost:8000/artists?pageNumber=1&recordsPerPage=20

Business Rules:

  1. recordsPerPage is optional with a default value of 10.
  2. pageNumber is required.

E.g. Code:

...

@app.post("/artists/", response_model=ArtistModel, response_model_exclude_unset=True)

async def get_artists(pageNumber:int, recordsPerPage: Optional[int] = 10):

	artists = await db["artists"].find(pageNumber).to_list(recordsPerPage)

	return artists
	
...

Note:

  1. We can make recordsPerPage optional without any default value with recordsPerPage: Optional[int] = None.

Creating APIs that saves the data in MongoDB

Following a tutorial at the MongoDB website, I am trying to build a real API.

I created a new MongoDB cluster (500 MB, Free) at MongoDB Atlas Cloud.

I added dependencies in requirements.txt.

I used the source code provided in the tutorial. It has all the code in app.py the file. This approach is fine for POC but in real projects, we bring better structure.

I am able to run the project and save data in MongoDB successfully.

Comments

Popular posts from this blog

Working with request header in Jersey (JAX-RS) guide

In the  previous post , we talked about, how to get parameters and their values from the request query string. In this guide learn how to get request header values in Jersey (JAX-RS) based application. We had tested or used the following tools and technologies in this project: Jersey (v 2.21) Gradle Build System (v 2.9) Spring Boot (v 1.3) Java (v 1.8) Eclipse IDE This is a part of  Jersey (JAX-RS) Restful Web Services Development Guides series. Please read Jersey + Spring Boot getting started guide . Gradle Build File We are using Gradle for our build and dependency management (Using Maven rather than Gradle is a very trivial task). File: build.gradle buildscript { ext { springBootVersion = '1.3.0.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' a

Jersey (JAX-RS) @FormParam HTML form data handling

There are multiple ways for consuming HTML form data (application/x-www-form-urlencoded) in Jersey. Using @FormParam annotation we can inject Form values in the Resource method. We can use it just like other @*Param. Jersey resource method needs to know they have to handle HTML form data, for it we explicitly specify  @Consumes("application/x-www-form-urlencoded") . There are multiple ways in which we can handle HTML form data using Jersey. Injecting Form data using @FormParam is one of them. Use @FormParam Using @FormParam we can inject specific HTML form parameters values in the Resource method. Its use is similar to other @*Param annotations. File: FormParamResource.java package in.geekmj.resource; import javax.ws.rs.Consumes; import javax.ws.rs.FormParam; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.R