-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[rest] change text from a property to a method #20290
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
Changes from 3 commits
07407a1
f17bca0
2160e4d
e68f928
3541364
7e6f83d
bc46726
62a8e5c
59fc8d5
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 |
|---|---|---|
|
|
@@ -262,16 +262,22 @@ def encoding(self) -> Optional[str]: | |
| def encoding(self, value: str) -> None: | ||
| """Sets the response encoding""" | ||
| self._encoding = value | ||
| self._text = None # clear text cache | ||
|
|
||
| @property | ||
| def text(self) -> str: | ||
| """Returns the response body as a string""" | ||
| if self._text is None: | ||
| def text(self, encoding: Optional[str] = None) -> str: | ||
| """Returns the response body as a string | ||
|
|
||
| :param optional[str] encoding: The encoding you want to decode the text with. Can | ||
| also be set independently through our encoding property | ||
| :return: The response's content decoded as a string. | ||
| """ | ||
| if self._text is None or encoding: | ||
| content = self.content | ||
| if not content: | ||
| self._text = "" | ||
| else: | ||
| self._text = decode_to_text(self.encoding, self.content) | ||
| encoding_to_pass = encoding or self.encoding | ||
| self._text = decode_to_text(encoding_to_pass, self.content) | ||
|
Contributor
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. Why use
Contributor
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. Is it because accessing the content property has side-effects?
Contributor
Author
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. Acccessign the
Contributor
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 agree with pylint that the side effects of accessing
Contributor
Author
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. sorry i'm not quite understanding your comment. Does the current code there look better to you?
Contributor
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. Why not just say: if not self.content: # <- this will raise if data is not read. Line 275 is not needed.
self._text = ""
else:
...
Contributor
Author
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. ah i think i was confused bc we're looking at diff code versions. What I currently have is if self._text is None or encoding:
encoding_to_pass = encoding or self.encoding
self._text = decode_to_text(encoding_to_pass, self.content)
return self._textwhich i think solves the issue you brought up (i moved the empty content detection to |
||
| return self._text | ||
|
|
||
| def json(self) -> Any: | ||
|
|
@@ -284,7 +290,7 @@ def json(self) -> Any: | |
| # this will trigger errors if response is not read in | ||
| self.content # pylint: disable=pointless-statement | ||
| if not self._json: | ||
| self._json = loads(self.text) | ||
| self._json = loads(self.text()) | ||
| return self._json | ||
|
|
||
| def raise_for_status(self) -> None: | ||
|
|
@@ -328,7 +334,6 @@ class HttpResponse(_HttpResponseBase): | |
| :ivar str text: The response body as a string. | ||
| :ivar request: The request that resulted in this response. | ||
| :vartype request: ~azure.core.rest.HttpRequest | ||
| :ivar internal_response: The object returned from the HTTP library. | ||
| :ivar str content_type: The content type of the response | ||
| :ivar bool is_closed: Whether the network connection has been closed yet | ||
| :ivar bool is_stream_consumed: When getting a stream response, checks | ||
|
|
@@ -421,7 +426,6 @@ class AsyncHttpResponse(_HttpResponseBase): | |
|
|
||
| :keyword request: The request that resulted in this response. | ||
| :paramtype request: ~azure.core.rest.HttpRequest | ||
| :keyword internal_response: The object returned from the HTTP library. | ||
| :ivar int status_code: The status code of this response | ||
| :ivar mapping headers: The response headers | ||
| :ivar str reason: The reason phrase for this response | ||
|
|
@@ -432,7 +436,6 @@ class AsyncHttpResponse(_HttpResponseBase): | |
| :ivar str text: The response body as a string. | ||
| :ivar request: The request that resulted in this response. | ||
| :vartype request: ~azure.core.rest.HttpRequest | ||
| :ivar internal_response: The object returned from the HTTP library. | ||
| :ivar str content_type: The content type of the response | ||
| :ivar bool is_closed: Whether the network connection has been closed yet | ||
| :ivar bool is_stream_consumed: When getting a stream response, checks | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.