Debian Codegraph @

docs_src/server_sent_events/tutorial001_py310.py

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