Skip to content

[core] add HttpRequest and HttpResponse reprs #16972

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

Merged
merged 7 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions sdk/core/azure-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# Release History

## 1.12.0 (Unreleased)
## 1.12.0 (2021-03-08)

### Features

- Added `azure.core.messaging.CloudEvent` model that follows the cloud event spec.
- Added `azure.core.serialization.NULL` sentinel value
- Added `azure.core.serialization.NULL` sentinel value

### Bug Fixes

- Improve `repr`s for `HttpRequest` and `HttpResponse`s #16972

## 1.11.0 (2021-02-08)

Expand Down
13 changes: 12 additions & 1 deletion sdk/core/azure-core/azure/core/pipeline/transport/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ def __init__(self, method, url, headers=None, files=None, data=None):
self.multipart_mixed_info = None # type: Optional[Tuple]

def __repr__(self):
return "<HttpRequest [%s]>" % (self.method)
return "<HttpRequest [{}], url: '{}'>".format(
self.method, self.url
)

def __deepcopy__(self, memo=None):
try:
Expand Down Expand Up @@ -592,6 +594,15 @@ def raise_for_status(self):
if self.status_code >= 400:
raise HttpResponseError(response=self)

def __repr__(self):
# there doesn't have to be a content type
content_type_str = (
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick: I would exclude the content type key/value altogether in the repr if we don't know what it is (i.e. not add , Content-Type: to the output...

", Content-Type: {}".format(self.content_type) if self.content_type else ""
)
return "<{}: {} {}{}>".format(
type(self).__name__, self.status_code, self.reason, content_type_str
)


class HttpResponse(_HttpResponseBase): # pylint: disable=abstract-method
def stream_download(self, pipeline):
Expand Down
11 changes: 11 additions & 0 deletions sdk/core/azure-core/tests/async_tests/test_universal_http_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ def __init__(self, body_bytes, headers=None):
self._body = body_bytes
self._headers = headers
self._cache = {}
self.status = 200
self.reason = "OK"

req_response = MockAiohttpClientResponse(body_bytes, headers)

Expand All @@ -120,3 +122,12 @@ async def test_aiohttp_response_text():
{'Content-Type': 'text/plain'}
)
assert res.text(encoding) == '56', "Encoding {} didn't work".format(encoding)

def test_repr():
res = _create_aiohttp_response(
b'\xef\xbb\xbf56',
{}
)
res.content_type = "text/plain"

assert repr(res) == "<AioHttpTransportResponse: 200 OK, Content-Type: text/plain>"
8 changes: 6 additions & 2 deletions sdk/core/azure-core/tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def test_request_url_with_params(self):
request.format_parameters({"g": "h"})

self.assertIn(request.url, ["a/b/c?g=h&t=y", "a/b/c?t=y&g=h"])

def test_request_url_with_params_as_list(self):

request = HttpRequest("GET", "/")
Expand All @@ -300,7 +300,7 @@ def test_request_url_with_params_with_none_in_list(self):
request.url = "a/b/c?t=y"
with pytest.raises(ValueError):
request.format_parameters({"g": ["h",None]})

def test_request_url_with_params_with_none(self):

request = HttpRequest("GET", "/")
Expand Down Expand Up @@ -328,6 +328,10 @@ def test_request_text(self):
# We want a direct string
assert request.data == "foo"

def test_repr(self):
request = HttpRequest("GET", "hello.com")
assert repr(request) == "<HttpRequest [GET], url: 'hello.com'>"


if __name__ == "__main__":
unittest.main()
7 changes: 7 additions & 0 deletions sdk/core/azure-core/tests/test_requests_universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,10 @@ def test_requests_response_text():
{'Content-Type': 'text/plain'}
)
assert res.text(encoding) == '56', "Encoding {} didn't work".format(encoding)

def test_repr():
res = _create_requests_response(
b'\xef\xbb\xbf56',
{'Content-Type': 'text/plain'}
)
assert repr(res) == "<RequestsTransportResponse: 200 OK, Content-Type: text/plain>"