Browse Source

🎨 Auto format

pull/15428/head
pre-commit-ci-lite[bot] 3 months ago
committed by GitHub
parent
commit
aeabe6c030
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 9
      fastapi/sse.py
  2. 8
      tests/test_sse.py

9
fastapi/sse.py

@ -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 "

8
tests/test_sse.py

@ -463,8 +463,8 @@ def test_direct_use_keepalive_ping():
assert response.status_code == 200 assert response.status_code == 200
text = response.text text = response.text
assert ": ping\n" in text assert ": ping\n" in text
assert "data: \"before-ping\"\n" in text assert 'data: "before-ping"\n' in text
assert "data: \"after-ping\"\n" in text assert 'data: "after-ping"\n' in text
def test_direct_use_no_ping_when_disabled(): def test_direct_use_no_ping_when_disabled():
@ -472,8 +472,8 @@ def test_direct_use_no_ping_when_disabled():
response = direct_client.get("/direct-no-ping") response = direct_client.get("/direct-no-ping")
assert response.status_code == 200 assert response.status_code == 200
assert ": ping\n" not in response.text assert ": ping\n" not in response.text
assert "data: \"msg1\"\n" in response.text assert 'data: "msg1"\n' in response.text
assert "data: \"msg2\"\n" in response.text assert 'data: "msg2"\n' in response.text
def test_direct_use_sync_iterable(): def test_direct_use_sync_iterable():

Loading…
Cancel
Save