|
|
@ -11,7 +11,7 @@ from pydantic import AfterValidator, BaseModel, Field, model_validator |
|
|
from starlette.concurrency import iterate_in_threadpool |
|
|
from starlette.concurrency import iterate_in_threadpool |
|
|
from starlette.requests import ClientDisconnect |
|
|
from starlette.requests import ClientDisconnect |
|
|
from starlette.responses import StreamingResponse |
|
|
from starlette.responses import StreamingResponse |
|
|
from starlette.types import Message, Receive, Scope, Send |
|
|
from starlette.types import Receive, Scope, Send |
|
|
|
|
|
|
|
|
try: |
|
|
try: |
|
|
from starlette._utils import collapse_excgroups |
|
|
from starlette._utils import collapse_excgroups |
|
|
@ -23,6 +23,7 @@ except ImportError: |
|
|
def collapse_excgroups(): |
|
|
def collapse_excgroups(): |
|
|
yield |
|
|
yield |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Canonical SSE event schema matching the OpenAPI 3.2 spec |
|
|
# Canonical SSE event schema matching the OpenAPI 3.2 spec |
|
|
# (Section 4.14.4 "Special Considerations for Server-Sent Events") |
|
|
# (Section 4.14.4 "Special Considerations for Server-Sent Events") |
|
|
_SSE_EVENT_SCHEMA: dict[str, Any] = { |
|
|
_SSE_EVENT_SCHEMA: dict[str, Any] = { |
|
|
@ -91,9 +92,7 @@ class EventSourceResponse(StreamingResponse): |
|
|
# ServerSentEvent objects, emit an initial retry field, and add |
|
|
# ServerSentEvent objects, emit an initial retry field, and add |
|
|
# keepalive pings. Also set SSE-appropriate headers. |
|
|
# keepalive pings. Also set SSE-appropriate headers. |
|
|
if content is not None: |
|
|
if content is not None: |
|
|
content = _wrap_content( |
|
|
content = _wrap_content(content, retry=retry, ping_interval=ping_interval) |
|
|
content, retry=retry, ping_interval=ping_interval |
|
|
|
|
|
) |
|
|
|
|
|
if headers is None: |
|
|
if headers is None: |
|
|
headers = {} |
|
|
headers = {} |
|
|
headers.setdefault("Cache-Control", "no-cache") |
|
|
headers.setdefault("Cache-Control", "no-cache") |
|
|
@ -392,7 +391,7 @@ class ServerSentEvent(BaseModel): |
|
|
] = None |
|
|
] = None |
|
|
|
|
|
|
|
|
@model_validator(mode="after") |
|
|
@model_validator(mode="after") |
|
|
def _check_data_exclusive(self) -> "ServerSentEvent": |
|
|
def _check_data_exclusive(self) -> ServerSentEvent: |
|
|
if self.data is not None and self.raw_data is not None: |
|
|
if self.data is not None and self.raw_data is not None: |
|
|
raise ValueError( |
|
|
raise ValueError( |
|
|
"Cannot set both 'data' and 'raw_data' on the same " |
|
|
"Cannot set both 'data' and 'raw_data' on the same " |
|
|
|