Skip to content

1995parham-learning/fastapi101

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FastAPI 101

GitHub Workflow Status Codecov GitHub repo size

Introduction

FastAPI is a python framework to have HTTP server with lots of typing, during the project I will try to set up database and also commands for doing common commands.

Description

First, devise the API to accommodate the following operations:

GET /students
Retrieve a list of created students

POST /students
Create a student

DELETE /students/:id
Remove a student identified by the given ID

GET /students/:id
Fetch the details of a student identified by the given ID

For student identification, any unique string can be used. The provided Student model serves as a reference, though alternative models can be utilized for requests and responses.

from datetime import date
from pydantic import BaseModel

class Student(BaseModel):
    id: str  # Unique identification manually generated
    first_name: str  # First name provided by the user
    last_name: str  # Last name provided by the user
    registration_date: date  # Date of registration
    graduation_date: date | None = None  # Graduation date (if applicable)
    average: float  # Average score provided by the user

Implement the design using FastAPI with asynchronous handlers. Utilize pipenv or poetry for project setup, and ensure inclusion of a README detailing how to run the project.

Note

Database usage is unnecessary for now; data can be stored in memory.

Note

Utilize pydantic for requests, responses, and models due to its excellent integration with FastAPI.

To gain a better understanding of asynchronous programming, include sleep in handlers.

import asyncio

await asyncio.sleep(10)
import time

time.sleep(10)

Invoke your APIs multiple times with async and sync sleep, recording response times to observe differences.

Ensure a clean design with separation of logic and views using Dependency Injection.

Write tests for the API based on FastAPI documentation, and leverage GitHub Actions for testing and linting.

Caution

The uvicorn can have multiple workers, test and see your in-memory storage works with them or not? Is there any solution in your mind?

Now you know the issue, so provide the database and use the in-memory store only in tests. For using database, you can use sqlmodel which is the FastAPI friend. After switching to the database, you can update tests based on manual.

When you have the backend API, you need to have a way to communicate the API with your frontend team. I can suggest two solutions here, Postman and Swagger but which one prefered by you?

If you want to write this project with your partner, how did you do that?

How to run?

You can run server using uvicorn directly,

uvicorn app.main:app --reload

or you can run it using the CLI

python main.py serve

How to curl

You can create a user with

curl 127.0.0.1:1378/users/ -H 'Content-Type: application/json' -d '{ "first_name": "Parham", "last_name": "Alvani", "average": 19.0  }'

and then list the created users with

curl 127.0.0.1:1378/users/

About

Learn how use FastAPI and its friends

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages