-
Notifications
You must be signed in to change notification settings - Fork 670
refactor: move response_content into backend code #2488
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,16 +4,9 @@ | |
import traceback | ||
import urllib.parse | ||
import warnings | ||
from typing import Any, Callable, Dict, Iterator, Literal, Optional, Tuple, Type, Union | ||
from typing import Any, Dict, Iterator, Literal, Optional, Tuple, Type, Union | ||
|
||
import requests | ||
|
||
from gitlab import types | ||
|
||
|
||
class _StdoutStream: | ||
def __call__(self, chunk: Any) -> None: | ||
print(chunk) | ||
from gitlab import _backends, types | ||
|
||
|
||
def get_content_type(content_type: Optional[str]) -> str: | ||
|
@@ -50,26 +43,14 @@ def format(self, record: logging.LogRecord) -> str: | |
|
||
|
||
def response_content( | ||
response: requests.Response, | ||
streamed: bool, | ||
action: Optional[Callable[[bytes], None]], | ||
chunk_size: int, | ||
*, | ||
iterator: bool, | ||
*args: Any, **kwargs: Any | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer to keep the signature as before. Instead of using *args/**kwargs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My initial idea was to just leave a simple wrapper for backward compatibility but maybe we can just drop it (see #2488 (comment)). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My thought was that this one would maintain the same exact signature as before, and then call the new function with keyword arguments. But! It isn't that important and perfectly fine with me if you want to merge this in as is 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh I misunderstood your comment I think, I thought you wanted to match the signatures. I'll add the changes! |
||
) -> Optional[Union[bytes, Iterator[Any]]]: | ||
if iterator: | ||
return response.iter_content(chunk_size=chunk_size) | ||
|
||
if streamed is False: | ||
return response.content | ||
|
||
if action is None: | ||
action = _StdoutStream() | ||
|
||
for chunk in response.iter_content(chunk_size=chunk_size): | ||
if chunk: | ||
action(chunk) | ||
return None | ||
warn( | ||
"`utils.response_content()` is deprecated and will be removed in a future" | ||
"version.\nUse the current backend's `response_content()` method instead.", | ||
category=DeprecationWarning, | ||
) | ||
return _backends.DefaultBackend.response_content(*args, **kwargs) | ||
|
||
|
||
def _transform_types( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import requests | ||
import responses | ||
|
||
from gitlab import _backends | ||
|
||
|
||
@responses.activate | ||
def test_streamed_response_content_with_requests(capsys): | ||
responses.add( | ||
method="GET", | ||
url="https://example.com", | ||
status=200, | ||
body="test", | ||
content_type="application/octet-stream", | ||
) | ||
|
||
resp = requests.get("https://example.com", stream=True) | ||
_backends.RequestsBackend.response_content( | ||
resp, streamed=True, action=None, chunk_size=1024, iterator=False | ||
) | ||
|
||
captured = capsys.readouterr() | ||
assert "test" in captured.out |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like it might be a good time to make the method require keyword args for all the args. I'm a fan of requiring keyword args 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this would be a breaking change as we still expose it in utils as a non-private function. In that case I'd maybe drop the wrapper entirely and not just deprecate it, as then we don't need to worry about maintaining the signature twice (#2488 (comment)).
Might be worth grouping breaking changes and wait to merge them all together though for a major release.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well I didn't think it would be a breaking change if this is the new function and then the wrapper (with the deprecation) would call this one with keyword args.