Debian Codegraph @

ninja/compatibility/streaming.py

raw symbols SCIP
1 """Compatibility layer for async streaming responses.
2
3 Django 4.2+ supports passing async iterators to StreamingHttpResponse.
4 On older versions, async generators must be eagerly consumed into a list.
5
6 TODO: When dropping Django < 4.2 support:
7 1. Remove this module entirely.
8 2. In AsyncOperation._async_stream_response (ninja/operation.py),
9 pass the async content generator directly to StreamingHttpResponse
10 and copy temporal_response headers lazily inside the generator:
11
12 async def content_iter():
13 async for chunk in content_gen:
14 yield chunk
15 for key, value in temporal_response.items():
16 if key.lower() != "content-type":
17 response[key] = value
18 for cookie_name, cookie in temporal_response.cookies.items():
19 response.cookies[cookie_name] = cookie
20
21 response = StreamingHttpResponse(
22 content_iter(), content_type=..., status=...,
23 )
24 """
25
26 from typing import Any, Dict
27
28 import django
29 from django.http import HttpResponse, StreamingHttpResponse
30
31 ASYNC_STREAMING = django.VERSION >= (4, 2)
32
33
34 def _copy_temporal_headers(
35 temporal_response: HttpResponse, response: StreamingHttpResponse
36 ) -> None:
37 """Copy headers and cookies from temporal response, skipping Content-Type."""
38 for key, value in temporal_response.items():
39 if key.lower() != "content-type":
40 response[key] = value
41 for cookie_name, cookie in temporal_response.cookies.items():
42 response.cookies[cookie_name] = cookie
43
44
45 if ASYNC_STREAMING:
46
47 async def create_streaming_response(
48 content_gen: Any,
49 *,
50 content_type: str,
51 status: int,
52 temporal_response: HttpResponse,
53 extra_headers: Dict[str, str],
54 ) -> StreamingHttpResponse:
55 """Create a StreamingHttpResponse from an async content generator.
56
57 Django 4.2+: passes the async generator directly and copies
58 temporal response headers/cookies lazily after the generator is exhausted.
59 """
60
61 async def with_lazy_headers() -> Any:
62 async for chunk in content_gen:
63 yield chunk
64 _copy_temporal_headers(temporal_response, response)
65
66 response = StreamingHttpResponse(
67 with_lazy_headers(),
68 content_type=content_type,
69 status=status,
70 )
71 for key, value in extra_headers.items():
72 response[key] = value
73 return response
74
75 else:
76
77 async def create_streaming_response(
78 content_gen: Any,
79 *,
80 content_type: str,
81 status: int,
82 temporal_response: HttpResponse,
83 extra_headers: Dict[str, str],
84 ) -> StreamingHttpResponse:
85 """Create a StreamingHttpResponse from an async content generator.
86
87 Django < 4.2: eagerly consumes the async generator into a list
88 since StreamingHttpResponse does not support async iterators.
89 """
90 chunks = []
91 async for chunk in content_gen:
92 chunks.append(chunk)
93 response = StreamingHttpResponse(
94 iter(chunks),
95 content_type=content_type,
96 status=status,
97 )
98 _copy_temporal_headers(temporal_response, response)
99 for key, value in extra_headers.items():
100 response[key] = value
101 return response