0
0
FastAPIframework~10 mins

Bulk operations 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 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'
Adict
BItem
Clist[Item]
Dstr
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.
2fill in blank
medium

Complete 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'
Asave_item(item)
Bitem.save()
Csave_item()
Ditem()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling save_item without arguments.
Trying to call a method on item that doesn't exist.
3fill in blank
hard

Fix 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'
Adict[int, float]
Btuple[ItemUpdate]
CItemUpdate
Dlist[ItemUpdate]
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.
4fill in blank
hard

Fill 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'
AList
Blist[int]
CSet
Dtuple[int]
Attempts:
3 left
💡 Hint
Common Mistakes
Not importing List from typing.
Using Set or tuple instead of list for the parameter.
5fill in blank
hard

Fill 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'
Adict
Bitem.name
Citem.price
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using list instead of dict for the comprehension.
Swapping key and value in the comprehension.