0
0
FastAPIframework~10 mins

ASGI and async-first architecture in FastAPI - Interactive Code Practice

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

Complete the code to define an async FastAPI route handler.

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/hello")
async def hello():
    return {"message": "Hello, World!"}  # This is an [1] function
Drag options to blanks, or click blank then click option'
Aasync
Bsync
Cclass
Ddecorator
Attempts:
3 left
💡 Hint
Common Mistakes
Using a synchronous function without async keyword.
2fill in blank
medium

Complete the code to await an async function inside a FastAPI route.

FastAPI
from fastapi import FastAPI
import asyncio
app = FastAPI()

async def fetch_data():
    await asyncio.sleep(1)
    return "data"

@app.get("/data")
async def get_data():
    result = [1] fetch_data()
    return {"result": result}
Drag options to blanks, or click blank then click option'
Areturn
Bawait
Cyield
Dcall
Attempts:
3 left
💡 Hint
Common Mistakes
Calling async function without await causing coroutine object return.
3fill in blank
hard

Fix the error in the FastAPI route by choosing the correct decorator for an async function.

FastAPI
from fastapi import FastAPI
app = FastAPI()

[1]("/items")
async def read_items():
    return ["item1", "item2"]
Drag options to blanks, or click blank then click option'
A@app.put
B@app.post
C@app.route
D@app.get
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST or PUT decorators for read-only routes.
4fill in blank
hard

Fill both blanks to create an async FastAPI route that returns JSON with a delay.

FastAPI
from fastapi import FastAPI
import asyncio
app = FastAPI()

@app.[1]("/wait")
async def wait_and_return():
    await asyncio.[2](2)
    return {"status": "done"}
Drag options to blanks, or click blank then click option'
Aget
Bpost
Csleep
Dwait
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST instead of GET for a simple wait route.
Using a non-existent asyncio.wait function.
5fill in blank
hard

Fill all three blanks to create a FastAPI async route that processes query parameters and returns a JSON response.

FastAPI
from fastapi import FastAPI, Query
app = FastAPI()

@app.get("/search")
async def search_items(q: str = [1](..., min_length=[2])):
    return {"query": q, "length": [3](q)}
Drag options to blanks, or click blank then click option'
AQuery
B3
Clen
Dstr
Attempts:
3 left
💡 Hint
Common Mistakes
Not using Query for query parameters.
Using incorrect min_length value.
Using str instead of len to get length.