-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Smart text serialization #12149
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
Smart text serialization #12149
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 |
|---|---|---|
|
|
@@ -319,6 +319,19 @@ def set_streamed_data_body(self, data): | |
| self.data = data | ||
| self.files = None | ||
|
|
||
| def set_text_body(self, data): | ||
| """Set a text as body of the request. | ||
|
|
||
| :param data: A text to send as body. | ||
| :type data: str | ||
| """ | ||
| if data is None: | ||
| self.data = None | ||
| else: | ||
| self.data = data | ||
| self.headers["Content-Length"] = str(len(self.data)) | ||
| self.files = None | ||
|
|
||
| def set_xml_body(self, data): | ||
| """Set an XML element tree as the body of the request. | ||
|
|
||
|
|
@@ -685,6 +698,11 @@ def _request( | |
| # type: (...) -> HttpRequest | ||
| """Create HttpRequest object. | ||
|
|
||
| If content is not None, guesses will be used to set the right body: | ||
| - If content is an XML tree, will serialize as XML | ||
| - If content-type starts by "text/", set the content as text | ||
| - Else, try JSON serialization | ||
|
|
||
| :param str method: HTTP method (GET, HEAD, etc.) | ||
| :param str url: URL for the request. | ||
| :param dict params: URL query parameters. | ||
|
|
@@ -703,8 +721,15 @@ def _request( | |
| request.headers.update(headers) | ||
|
|
||
| if content is not None: | ||
| content_type = request.headers.get("Content-Type") | ||
| if isinstance(content, ET.Element): | ||
| request.set_xml_body(content) | ||
| # https://github.com/Azure/azure-sdk-for-python/issues/12137 | ||
| # A string is valid JSON, make the difference between text | ||
| # and a plain JSON string. | ||
| # Content-Type is a good indicator of intent from user | ||
| elif content_type and content_type.startswith("text/"): | ||
|
Member
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'm wondering if we've created a bit of a mishmash in how we decide how to serialize/pass request bodies. We are using the
Member
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. Yes, I'm not really happy by the black magic, it's why I took the simplest approach to avoid two much black magic: when you cannot guess at all if this is a string test, or a string token of JSON. |
||
| request.set_text_body(content) | ||
| else: | ||
| try: | ||
| request.set_json_body(content) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.