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

python-pydantic-v1: Handle response chartset in exception. #19867

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ class ApiClient:
collection_formats)
url += "?" + url_query

def extract_charset(content_type):
match = None
if content_type is not None:
match = re.search(r"charset=([a-zA-Z\-\d]+)[\s;]?", content_type)
return match.group(1) if match else "utf-8"

Copy link
Member

Choose a reason for hiding this comment

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

thanks for the PR.

instead of using this, what about using the function that usually comes with the Python HTTP libraries instead, e.g. https://stackoverflow.com/questions/14592762/a-good-way-to-get-the-charset-encoding-of-an-http-response-in-python ?

(so that we don't need to reinvent the wheel and you may need to add another method in rest.py to return the charset)

try:
# perform request and return response
response_data = {{#asyncio}}await {{/asyncio}}{{#tornado}}yield {{/tornado}}self.request(
Expand All @@ -231,7 +237,12 @@ class ApiClient:
_request_timeout=_request_timeout)
except ApiException as e:
if e.body:
e.body = e.body.decode('utf-8')
try:
charset = extract_charset((e.headers or {}).get('content-type'))
e.body = e.body.decode(charset)
except UnicodeDecodeError:
# Keep original body if charset is not recognized.
pass
raise e

self.last_response = response_data
Expand All @@ -247,12 +258,9 @@ class ApiClient:
if response_type == "bytearray":
response_data.data = response_data.data
else:
match = None
content_type = response_data.getheader('content-type')
if content_type is not None:
match = re.search(r"charset=([a-zA-Z\-\d]+)[\s;]?", content_type)
encoding = match.group(1) if match else "utf-8"
response_data.data = response_data.data.decode(encoding)
charset = extract_charset(content_type)
response_data.data = response_data.data.decode(charset)

# deserialize response data
if response_type == "bytearray":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ def __call_api(
collection_formats)
url += "?" + url_query

def extract_charset(content_type):
match = None
if content_type is not None:
match = re.search(r"charset=([a-zA-Z\-\d]+)[\s;]?", content_type)
return match.group(1) if match else "utf-8"

try:
# perform request and return response
response_data = self.request(
Expand All @@ -218,7 +224,12 @@ def __call_api(
_request_timeout=_request_timeout)
except ApiException as e:
if e.body:
e.body = e.body.decode('utf-8')
try:
charset = extract_charset((e.headers or {}).get('content-type'))
e.body = e.body.decode(charset)
except UnicodeDecodeError:
# Keep original body if charset is not recognized.
pass
raise e

self.last_response = response_data
Expand All @@ -234,12 +245,9 @@ def __call_api(
if response_type == "bytearray":
response_data.data = response_data.data
else:
match = None
content_type = response_data.getheader('content-type')
if content_type is not None:
match = re.search(r"charset=([a-zA-Z\-\d]+)[\s;]?", content_type)
encoding = match.group(1) if match else "utf-8"
response_data.data = response_data.data.decode(encoding)
charset = extract_charset(content_type)
response_data.data = response_data.data.decode(charset)

# deserialize response data
if response_type == "bytearray":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ async def __call_api(
collection_formats)
url += "?" + url_query

def extract_charset(content_type):
match = None
if content_type is not None:
match = re.search(r"charset=([a-zA-Z\-\d]+)[\s;]?", content_type)
return match.group(1) if match else "utf-8"

try:
# perform request and return response
response_data = await self.request(
Expand All @@ -198,7 +204,12 @@ async def __call_api(
_request_timeout=_request_timeout)
except ApiException as e:
if e.body:
e.body = e.body.decode('utf-8')
try:
charset = extract_charset((e.headers or {}).get('content-type'))
e.body = e.body.decode(charset)
except UnicodeDecodeError:
# Keep original body if charset is not recognized.
pass
raise e

self.last_response = response_data
Expand All @@ -214,12 +225,9 @@ async def __call_api(
if response_type == "bytearray":
response_data.data = response_data.data
else:
match = None
content_type = response_data.getheader('content-type')
if content_type is not None:
match = re.search(r"charset=([a-zA-Z\-\d]+)[\s;]?", content_type)
encoding = match.group(1) if match else "utf-8"
response_data.data = response_data.data.decode(encoding)
charset = extract_charset(content_type)
response_data.data = response_data.data.decode(charset)

# deserialize response data
if response_type == "bytearray":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ def __call_api(
collection_formats)
url += "?" + url_query

def extract_charset(content_type):
match = None
if content_type is not None:
match = re.search(r"charset=([a-zA-Z\-\d]+)[\s;]?", content_type)
return match.group(1) if match else "utf-8"

try:
# perform request and return response
response_data = self.request(
Expand All @@ -217,7 +223,12 @@ def __call_api(
_request_timeout=_request_timeout)
except ApiException as e:
if e.body:
e.body = e.body.decode('utf-8')
try:
charset = extract_charset((e.headers or {}).get('content-type'))
e.body = e.body.decode(charset)
except UnicodeDecodeError:
# Keep original body if charset is not recognized.
pass
raise e

self.last_response = response_data
Expand All @@ -233,12 +244,9 @@ def __call_api(
if response_type == "bytearray":
response_data.data = response_data.data
else:
match = None
content_type = response_data.getheader('content-type')
if content_type is not None:
match = re.search(r"charset=([a-zA-Z\-\d]+)[\s;]?", content_type)
encoding = match.group(1) if match else "utf-8"
response_data.data = response_data.data.decode(encoding)
charset = extract_charset(content_type)
response_data.data = response_data.data.decode(charset)

# deserialize response data
if response_type == "bytearray":
Expand Down
Loading