Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a FastAPI POST endpoint for bulk creating items.
FastAPI
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str price: float @app.post("/items/bulk") async def create_items(items: [1]): return {"created": len(items)}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a single Item instead of a list for bulk operations.
Using dict or str which won't validate the input properly.
✗ Incorrect
The endpoint expects a list of Item objects for bulk creation, so the parameter type should be list[Item].
2fill in blank
mediumComplete the code to process each item in the bulk request asynchronously.
FastAPI
from fastapi import FastAPI from pydantic import BaseModel import asyncio app = FastAPI() class Item(BaseModel): name: str price: float async def save_item(item: Item): await asyncio.sleep(0.1) # simulate saving @app.post("/items/bulk") async def create_items(items: list[Item]): await asyncio.gather(*[[1] for item in items]) return {"created": len(items)}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling save_item without arguments.
Trying to call a method on item that doesn't exist.
✗ Incorrect
We need to call the async function save_item with each item, so save_item(item) is correct.
3fill in blank
hardFix the error in the bulk update endpoint to accept a list of items.
FastAPI
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class ItemUpdate(BaseModel): id: int price: float @app.put("/items/bulk") async def update_items(items: [1]): updated = len(items) return {"updated": updated}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a single ItemUpdate instead of a list.
Using dict or tuple which won't validate the input properly.
✗ Incorrect
For bulk update, the endpoint should accept a list of ItemUpdate objects, so list[ItemUpdate] is correct.
4fill in blank
hardFill both blanks to create a bulk delete endpoint accepting a list of IDs.
FastAPI
from fastapi import FastAPI from typing import [1] app = FastAPI() @app.delete("/items/bulk") async def delete_items(ids: [2]): deleted_count = len(ids) return {"deleted": deleted_count}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not importing List from typing.
Using Set or tuple instead of list for the parameter.
✗ Incorrect
We import List from typing and use list[int] as the parameter type to accept a list of integers.
5fill in blank
hardFill all three blanks to create a bulk response with item names and their updated prices.
FastAPI
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class ItemUpdate(BaseModel): name: str price: float @app.put("/items/bulk") async def update_items(items: list[ItemUpdate]): result = [1]([2]: [3] for item in items) return {"updated_items": result}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using list instead of dict for the comprehension.
Swapping key and value in the comprehension.
✗ Incorrect
We create a dictionary comprehension mapping item names to their updated prices.