| 1 |
from collections.abc import AsyncIterable, Iterable |
| 2 |
|
| 3 |
from fastapi import FastAPI |
| 4 |
from pydantic import BaseModel |
| 5 |
|
| 6 |
app = FastAPI() |
| 7 |
|
| 8 |
|
| 9 |
class Item(BaseModel): |
| 10 |
name: str |
| 11 |
description: str | None |
| 12 |
|
| 13 |
|
| 14 |
items = [ |
| 15 |
Item(name="Plumbus", description="A multi-purpose household device."), |
| 16 |
Item(name="Portal Gun", description="A portal opening device."), |
| 17 |
Item(name="Meeseeks Box", description="A box that summons a Meeseeks."), |
| 18 |
] |
| 19 |
|
| 20 |
|
| 21 |
@app.get("/items/stream") |
| 22 |
async def stream_items() -> AsyncIterable[Item]: |
| 23 |
for item in items: |
| 24 |
yield item |
| 25 |
|
| 26 |
|
| 27 |
@app.get("/items/stream-no-async") |
| 28 |
def stream_items_no_async() -> Iterable[Item]: |
| 29 |
for item in items: |
| 30 |
yield item |
| 31 |
|
| 32 |
|
| 33 |
@app.get("/items/stream-no-annotation") |
| 34 |
async def stream_items_no_annotation(): |
| 35 |
for item in items: |
| 36 |
yield item |
| 37 |
|
| 38 |
|
| 39 |
@app.get("/items/stream-no-async-no-annotation") |
| 40 |
def stream_items_no_async_no_annotation(): |
| 41 |
for item in items: |
| 42 |
yield item |