FastAPI (contd)
- Fastapi allows asynchronous calls
Async/await
- Asynchronous programming allows to run multiple tasks concurrently without blocking the program. This is useful when dealing with IO-bound operations like
- Waiting for network requests
- reading/writing files
- Interacting with databases
- Refer Here for basic async await example
FastAPI Basics
Defining first API endpoint
- Create a new folder called as books
- Activate a new virtual environment and install fastapi
- Create requirements.txt
pip freeze > requirements.txt - Create a new file called as main.py containing the following code
from fastapi import FastAPI
# create app
app = FastAPI()
# define endpoint
@app.get("/books")
async def get_all_books():
return [{
"book_id": 1,
"title": "Your Brain at Work",
"author": "David Rock"
}]
- refer to classroom video for vscode debugger setup.
- Refer Here for the changes done
Defining and using request and response models
- FastAPI simplifies the process of defining and validating the request and responses using Pydantic
- Lets create a file called as models.py
- Refer Here for the changes done
