BaseHTTPMiddleware structure and StreamingResponse() #2801
-
Hello the Starlette community. FastAPI in many areas being a mere Starlette wrapper I had to contend with reading and building code atop Starlette and not FastAPI really. As the code is somewhat late in versions I built the working code, and then when we updated Starlette as the necessarydependance I noticed something interesting in Starlette's recent codebase changes, specifically in the middleware.base file. There's a new _StreamingResponse type alongside the existing StreamingResponse. This caught my attention because it broke my Response detection and caching mechanism. Old code: async def _prepare_response_for_caching_by_type(
self,
response: Response,
cache_key: str,
expiry: int,
) -> Response:
content: bytes | None = None
response_type: CacheResponseType | None = None
background_tasks = BackgroundTasks()
match response:
case StreamingResponse():
content = b"".join(
[
chunk.encode("utf-8") if isinstance(chunk, str) else chunk
async for chunk in response.body_iterator
]
)
... New working code: async def _prepare_response_for_caching_by_type(
self,
response: Response,
cache_key: str,
expiry: int,
) -> Response:
content: bytes | None = None
response_type: CacheResponseType | None = None
background_tasks = BackgroundTasks()
match response:
case StreamingResponse() | _StreamingResponse():
content = b"".join(
[
chunk.encode("utf-8") if isinstance(chunk, str) else chunk
async for chunk in response.body_iterator
]
)
... In fact I could drop the whole StreamingResponse() and just use the new one but I kept both for compatibility. I wanted to understand the changed, why did the Response and why the duplication of code, wouldn't just inheriting from Response avoid this duplication of code, am I missing a piece of context or the inheritance structure could be improved ? There has already been a discussion around it #2731 and d771bb7#r146823236 but remains unanswered. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
I answered this in d771bb7#r146823236: you can't assume the thing returned from a function that declares to return a |
Beta Was this translation helpful? Give feedback.
I answered this in d771bb7#r146823236: you can't assume the thing returned from a function that declares to return a
Response
will be a specific subclass ofResponse
(StreamingResponse
)._StreamingResponse
does inherit fromResponse
.