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

[rest] switch base responses to ABCs #20448

Merged
merged 25 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
edab626
switch to protocol
iscai-msft Aug 27, 2021
c2157a5
update changelog
iscai-msft Aug 27, 2021
bdbbb6e
add initial tests
iscai-msft Aug 27, 2021
324f1ae
switch from protocol to abc
iscai-msft Aug 27, 2021
07f4ce2
improve HttpResponse docstrings
iscai-msft Aug 30, 2021
b5d9ade
lint
iscai-msft Aug 30, 2021
312b0fc
HeadersType -> MutableMapping[str, str]
iscai-msft Aug 30, 2021
9a00a7f
remove iter_text and iter_lines
iscai-msft Aug 30, 2021
d7e39c7
update tests
iscai-msft Aug 30, 2021
90da0a4
improve docstrings
iscai-msft Aug 30, 2021
60a8123
Merge branch 'remove_iter_text_lines' of https://github.com/iscai-msf…
iscai-msft Aug 30, 2021
0a1eb95
have base impls handle more code
iscai-msft Aug 30, 2021
0304a0c
add set_read_checks
iscai-msft Aug 30, 2021
47660f3
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-python …
iscai-msft Aug 31, 2021
c428a6b
commit to restart pipelines
iscai-msft Aug 31, 2021
f9f40a1
address xiang's comments
iscai-msft Aug 31, 2021
c8915df
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-python …
iscai-msft Sep 7, 2021
e927262
lint
iscai-msft Sep 8, 2021
7e5804d
clear json cache when encoding is updated
iscai-msft Sep 21, 2021
4609104
make sure content type is empty string if doesn't exist
iscai-msft Sep 21, 2021
8e4febd
update content_type to be None if there is no content type header
iscai-msft Sep 22, 2021
11f4440
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-python …
iscai-msft Sep 22, 2021
590c6f4
fix passing encoding to text method error
iscai-msft Sep 23, 2021
ddfd235
update is_stream_consumed docs
iscai-msft Sep 23, 2021
d314548
remove erroneous committed code
iscai-msft Sep 23, 2021
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
9 changes: 6 additions & 3 deletions sdk/core/azure-core/azure/core/rest/_http_response_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def __init__(self, **kwargs):
self._json = None # this is filled in ContentDecodePolicy, when we deserialize
self._content = None # type: Optional[bytes]
self._text = None # type: Optional[str]
self._last_passed_encoding = None
xiangyan99 marked this conversation as resolved.
Show resolved Hide resolved

@property
def request(self):
Expand Down Expand Up @@ -184,9 +185,11 @@ def text(self, encoding=None):
also be set independently through our encoding property
:return: The response's content decoded as a string.
"""
if self._text is None or encoding:
encoding_to_pass = encoding or self.encoding
self._text = decode_to_text(encoding_to_pass, self.content)
if encoding:
return decode_to_text(encoding, self.content)
if self._text:
return self._text
self._text = decode_to_text(self.encoding, self.content)
return self._text

def json(self):
Expand Down
13 changes: 13 additions & 0 deletions sdk/core/azure-core/tests/test_rest_http_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,19 @@ def test_text_and_encoding(send_request):
assert response.text("latin-1") == u'ð\x9f\x91©' == response.content.decode("latin-1")
assert response.encoding == "utf-16"

def test_passing_encoding_to_text(send_request):
response = send_request(
request=HttpRequest("GET", "/encoding/emoji"),
)
assert response.content == u"👩".encode("utf-8")
assert response.text() == u"👩"

# pass in different encoding
assert response.text("latin-1") == u'ð\x9f\x91©'

# check response.text() still gets us the old value
assert response.text() == u"👩"

def test_initialize_response_abc():
with pytest.raises(TypeError) as ex:
HttpResponse()
Expand Down