Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define an async GET path operation in FastAPI.
FastAPI
from fastapi import FastAPI app = FastAPI() @app.get("/items/{item_id}") async def read_item(item_id: int): return {"item_id": item_id, "message": "This is an async path"} # The function must be [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'def' instead of 'async def' for async path operations.
Trying to use 'sync' which is not a Python keyword.
✗ Incorrect
In FastAPI, to define an asynchronous path operation, the function must be declared with 'async'.
2fill in blank
mediumComplete the code to await an async function inside a FastAPI path operation.
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'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the async function without 'await' causing a coroutine object to be returned.
Using 'async' instead of 'await' inside the function body.
✗ Incorrect
To call an async function and wait for its result, use 'await' before the function call.
3fill in blank
hardFix the error in the async path operation by completing the code.
FastAPI
from fastapi import FastAPI app = FastAPI() @app.get("/users/{user_id}") def get_user(user_id: int): return {"user_id": user_id} # To make this async, add [1] before def
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Writing 'async def' as one token in the blank instead of just 'async'.
Placing 'async' after 'def' which is invalid syntax.
✗ Incorrect
To fix the error, add 'async' before 'def' to declare the function as asynchronous.
4fill in blank
hardFill both blanks to create an async POST path operation that receives JSON data and returns it.
FastAPI
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str price: float @app.post("/items") async def create_item(item: [1]): return [2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dict()' instead of 'item.dict()' which returns an empty dict.
Returning the model instance directly without converting to dict.
✗ Incorrect
The path operation receives an 'Item' model instance and returns its dictionary representation using 'item.dict()'.
5fill in blank
hardFill all three blanks to define an async path operation that waits for a background task and returns a message.
FastAPI
from fastapi import FastAPI import asyncio app = FastAPI() async def background_task(): await asyncio.sleep(2) @app.get("/wait") async def wait_task(): await [1]() return [2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'background_task()' inside await which calls the function twice.
Returning a plain string instead of a dictionary for JSON response.
✗ Incorrect
You await the function name without parentheses to call it, and return a JSON message as a dictionary.