Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

adriangb
Copy link
Contributor

@adriangb adriangb commented Sep 6, 2024

Alternative to #2176

@dmitry-mli
Copy link

FYI, The streaming response used to also do:

            if not isinstance(chunk, (bytes, memoryview)):
                chunk = chunk.encode(self.charset)

It itsn't covered so when it was removed in https://github.com/encode/starlette/pull/2620/files it didn't break

await send({"type": "http.response.body", "body": b"", "more_body": False})

if self.background:
await self.background()
Copy link

@dmitry-mli dmitry-mli Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For full backward compatibility add it to constructor also:

class _StreamingResponse(Response):
    def __init__(
        self,
        content: AsyncContentStream,
        status_code: int = 200,
        headers: typing.Mapping[str, str] | None = None,
        media_type: str | None = None,
        background: BackgroundTask | None = None,                <<<
        info: typing.Mapping[str, typing.Any] | None = None,
    ) -> None:
        self.info = info
        self.body_iterator = content
        self.status_code = status_code
        self.media_type = media_type
        self.background = background                             <<<
        self.init_headers(headers)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because some middleware may be referring to that field. Or below will work also

self.background = None

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No users should be calling the constructor unless they do something like type(response)(...) which is not something we need to support.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, this aligns with the class being marked as private. Then please include

self.background = None

For full backward compatibility, any response object injected into BaseHTTPMiddleware based middleware needs to have the background field (default None)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I see your point now with the edit. Good point. Added.

@Kludex this is another reason to move the background task logic out of responses and a good reason to rework our Response inheritance so that Response and StreamingResponse both inherit from a BaseResponse that has the API and initialization of common bits.

@adriangb adriangb enabled auto-merge (squash) September 6, 2024 23:07
@Kludex Kludex disabled auto-merge September 7, 2024 04:41
Copy link
Owner

@Kludex Kludex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test here?

@adriangb
Copy link
Contributor Author

adriangb commented Sep 7, 2024

There is a test already, am I missing something?

@Kludex Kludex merged commit 53f9dc0 into master Sep 7, 2024
6 checks passed
@Kludex Kludex deleted the simpler branch September 7, 2024 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants