mirror of
https://github.com/fastapi/fastapi.git
synced 2026-02-28 04:36:14 -05:00
📝 Add source example for JSON Lines
This commit is contained in:
0
docs_src/stream_json_lines/__init__.py
Normal file
0
docs_src/stream_json_lines/__init__.py
Normal file
41
docs_src/stream_json_lines/tutorial001_py310.py
Normal file
41
docs_src/stream_json_lines/tutorial001_py310.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from collections.abc import AsyncIterable, Iterable
|
||||
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
class Item(BaseModel):
|
||||
name: str
|
||||
description: str | None
|
||||
|
||||
|
||||
items = [
|
||||
Item(name="Plumbus", description="A multi-purpose household device."),
|
||||
Item(name="Portal Gun", description="A portal opening device."),
|
||||
]
|
||||
|
||||
|
||||
@app.get("/items/stream")
|
||||
async def stream_items() -> AsyncIterable[Item]:
|
||||
for item in items:
|
||||
yield item
|
||||
|
||||
|
||||
@app.get("/items/stream-no-async")
|
||||
def stream_items_no_async() -> Iterable[Item]:
|
||||
for item in items:
|
||||
yield item
|
||||
|
||||
|
||||
@app.get("/items/stream-no-annotation")
|
||||
async def stream_items_no_annotation():
|
||||
for item in items:
|
||||
yield item
|
||||
|
||||
|
||||
@app.get("/items/stream-no-async-no-annotation")
|
||||
def stream_items_no_async_no_annotation():
|
||||
for item in items:
|
||||
yield item
|
||||
Reference in New Issue
Block a user