0
0
FastAPIframework~10 mins

Why project structure matters at scale in FastAPI - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import FastAPI and create an app instance.

FastAPI
from fastapi import [1]

app = [1]()
Drag options to blanks, or click blank then click option'
AFastAPI
BRequest
CResponse
DDepends
Attempts:
3 left
💡 Hint
Common Mistakes
Importing other classes like Request instead of FastAPI
Forgetting to create an instance of FastAPI
2fill in blank
medium

Complete the code to define a simple GET route that returns a greeting.

FastAPI
@app.[1]("/")
async def read_root():
    return {"message": "Hello World"}
Drag options to blanks, or click blank then click option'
Aget
Bpost
Cput
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST instead of GET for a read route
Misspelling the decorator name
3fill in blank
hard

Fix the error in the code to correctly include a router from another module.

FastAPI
from fastapi import FastAPI
from routers import users

app = FastAPI()
app.[1](users.router)
Drag options to blanks, or click blank then click option'
Aregister
Badd_route
Cmount
Dinclude_router
Attempts:
3 left
💡 Hint
Common Mistakes
Using add_route which is not a FastAPI method
Confusing mount with include_router
4fill in blank
hard

Fill both blanks to create a modular project structure with routers and models.

FastAPI
from fastapi import FastAPI
from [1] import router as user_router
from [2] import User

app = FastAPI()
app.include_router(user_router)
Drag options to blanks, or click blank then click option'
Arouters.users
Bmodels.user
Cservices.auth
Dschemas.user
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up routers and models imports
Using wrong folder names
5fill in blank
hard

Fill all three blanks to define a Pydantic model and use it in a POST route.

FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

class [1](BaseModel):
    name: str
    age: int

app = FastAPI()

@app.post("/users")
async def create_user(user: [2]):
    return {"user_name": user.[3]
Drag options to blanks, or click blank then click option'
AUser
Cname
DPerson
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for model and parameter
Returning wrong attribute from user