Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync streaming interface on responses #695

Merged
merged 6 commits into from
Jan 2, 2020
Merged

Conversation

tomchristie
Copy link
Member

@tomchristie tomchristie commented Dec 30, 2019

Refs #572

Add sync interfaces for streaming on responses.

Draft PR at the moment just to demonstrate how it'll look... The tests will need filling out to.

Related to this will be:

  • Ability to support both sync and async request bodies... We need an IteratorStream in content_streams.py.
  • .next()

@@ -917,18 +979,15 @@ def raw(self): # type: ignore
"""
A byte-iterator over the raw response content.
"""
if hasattr(self, "_raw_content"):
Copy link
Member Author

Choose a reason for hiding this comment

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

We can drop _raw_content here since we're not special casing that anymore.

self._raw_stream = stream
else:
self._raw_stream = ByteStream(body=content or b"")
self.read()
Copy link
Member Author

@tomchristie tomchristie Dec 30, 2019

Choose a reason for hiding this comment

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

This is a little nicer now that we can use a sync .read() inside __init__ rather than having to special-case self._raw_content.

Copy link
Member

@florimondmanca florimondmanca left a comment

Choose a reason for hiding this comment

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

In line with the sync strategy we envisioned for responses (i.e. sync version of .aXYZ() methods), so yup for me for now.

Leads to a tiny bit of code duplication on the client side, but at least that keeps things easy to read and manage. :-)

Happy to give an approval when this gets out of draft!

Edit: updated the PR description to reference #572 👍 (which, btw, looks out of date now).

@tomchristie tomchristie marked this pull request as ready for review December 31, 2019 15:05
@tomchristie tomchristie added the enhancement New feature or request label Dec 31, 2019
@florimondmanca florimondmanca self-requested a review January 1, 2020 14:04
Copy link
Member

@florimondmanca florimondmanca left a comment

Choose a reason for hiding this comment

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

A few small simplification suggestions. This is exciting stuff!

Comment on lines 113 to 114
async for part in (): # pragma: nocover
yield b""
Copy link
Member

Choose a reason for hiding this comment

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

Does this have any actual runtime consequence, or is this to please linters?

Turns out that we can make this work without tricks by dropping the async annotation…

def __aiter__(self) -> typing.AsyncIterator[bytes]:
    raise RuntimeError("Attempted to call a async iterator on an sync stream.")

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice one, yup!

Comment on lines 192 to 193
@pytest.mark.asyncio
async def test_invalid_argument():
Copy link
Member

Choose a reason for hiding this comment

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

We don't need the asyncio marker here, do we? Possible simplification…

def test_invalid_argument():
    with pytest.raises(TypeError):
        encode(123)

@tomchristie
Copy link
Member Author

Thanks for the great review!
I think we may as well get this one in now.

@tomchristie tomchristie merged commit 11e7604 into master Jan 2, 2020
@tomchristie tomchristie deleted the sync-streaming-interface branch January 2, 2020 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants