Skip to content

Commit 3ac70d1

Browse files
authored
Allow overriding default response headers (#926)
1 parent 2e0dada commit 3ac70d1

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

sdk/python/src/datastar_py/django.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
class DatastarResponse(_StreamingHttpResponse):
2424
"""Respond with 0..N `DatastarEvent`s"""
2525

26+
default_headers: dict[str, str] = SSE_HEADERS.copy()
27+
2628
def __init__(
2729
self,
2830
content: DatastarEvents = None,
@@ -34,7 +36,7 @@ def __init__(
3436
status = status or 204
3537
content = tuple()
3638
else:
37-
headers = {**SSE_HEADERS, **(headers or {})}
39+
headers = {**self.default_headers, **(headers or {})}
3840
if isinstance(content, DatastarEvent):
3941
content = (content,)
4042
super().__init__(content, status=status, headers=headers)

sdk/python/src/datastar_py/litestar.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
class DatastarResponse(Stream):
2626
"""Respond with 0..N `DatastarEvent`s"""
2727

28+
default_headers: dict[str, str] = SSE_HEADERS.copy()
29+
2830
def __init__(
2931
self,
3032
content: DatastarEvents = None,
@@ -40,7 +42,7 @@ def __init__(
4042
status_code = status_code or 204
4143
content = tuple()
4244
else:
43-
headers = {**SSE_HEADERS, **(headers or {})}
45+
headers = {**self.default_headers, **(headers or {})}
4446
if isinstance(content, DatastarEvent):
4547
content = (content,)
4648
super().__init__(

sdk/python/src/datastar_py/quart.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
class DatastarResponse(Response):
2323
"""Respond with 0..N `DatastarEvent`s"""
2424

25+
default_headers: dict[str, str] = SSE_HEADERS.copy()
26+
2527
def __init__(
2628
self,
2729
content: DatastarEvents = None,
@@ -31,7 +33,7 @@ def __init__(
3133
if not content:
3234
status = status or 204
3335
else:
34-
headers = {**SSE_HEADERS, **(headers or {})}
36+
headers = {**self.default_headers, **(headers or {})}
3537
super().__init__(content, status=status, headers=headers)
3638
if isgenerator(content) or isasyncgen(content):
3739
self.timeout = None

sdk/python/src/datastar_py/sanic.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121

2222
class DatastarResponse(HTTPResponse):
23+
default_headers: dict[str, str] = SSE_HEADERS.copy()
24+
2325
def __init__(
2426
self,
2527
content: DatastarEvent | Collection[DatastarEvent] | None = None,
@@ -28,7 +30,9 @@ def __init__(
2830
) -> None:
2931
if not content:
3032
status = status or 204
31-
super().__init__(content, status=status or 200, headers={**SSE_HEADERS, **(headers or {})})
33+
super().__init__(
34+
content, status=status or 200, headers={**self.default_headers, **(headers or {})}
35+
)
3236

3337
async def send(
3438
self,

sdk/python/src/datastar_py/starlette.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
class DatastarResponse(_StreamingResponse):
2424
"""Respond with 0..N `DatastarEvent`s"""
2525

26+
default_headers: dict[str, str] = SSE_HEADERS.copy()
27+
2628
def __init__(
2729
self,
2830
content: DatastarEvents = None,
@@ -35,7 +37,7 @@ def __init__(
3537
content = tuple()
3638
else:
3739
status_code = status_code or 200
38-
headers = {**SSE_HEADERS, **(headers or {})}
40+
headers = {**self.default_headers, **(headers or {})}
3941
if isinstance(content, DatastarEvent):
4042
content = (content,)
4143
super().__init__(content, status_code=status_code, headers=headers, background=background)

0 commit comments

Comments
 (0)