Skip to content

Commit

Permalink
Update utils.py
Browse files Browse the repository at this point in the history
Sometimes the returned response body is empty. This change prevents the error during frappe response parsing. For example DELETE request returns no content.
  • Loading branch information
tonspar committed Jan 30, 2024
1 parent dc76379 commit edbfb69
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions frappe/integrations/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


def make_request(
method: str, url: str, auth=None, headers=None, data=None, json=None, params=None
method: str, url: str, auth=None, headers=None, data=None, json=None, params=None, response_body=True
):
auth = auth or ""
data = data or {}
Expand All @@ -24,10 +24,14 @@ def make_request(
)
frappe.flags.integration_request.raise_for_status()

if frappe.flags.integration_request.headers.get("content-type") == "text/plain; charset=utf-8":
return parse_qs(frappe.flags.integration_request.text)

return frappe.flags.integration_request.json()
if response_body:
if frappe.flags.integration_request.headers.get("content-type") == "text/plain; charset=utf-8":
return parse_qs(frappe.flags.integration_request.text)

return frappe.flags.integration_request.json()
else:
return True

except Exception as exc:
frappe.log_error()
raise exc
Expand Down Expand Up @@ -55,6 +59,7 @@ def make_post_request(url: str, **kwargs):
* `json`: JSON to be passed in the request.
* `params`: Query parameters to be passed in the request.
* `auth`: Auth credentials.
* `response_body`: False => Prevents an error if response body is empty. Default = True
"""
return make_request("POST", url, **kwargs)

Expand All @@ -69,6 +74,7 @@ def make_put_request(url: str, **kwargs):
* `json`: JSON to be passed in the request.
* `params`: Query parameters to be passed in the request.
* `auth`: Auth credentials.
* `response_body`: False => Prevents an error if response body is empty. Default = True
"""
return make_request("PUT", url, **kwargs)

Expand All @@ -83,6 +89,7 @@ def make_patch_request(url: str, **kwargs):
* `json`: JSON to be passed in the request.
* `params`: Query parameters to be passed in the request.
* `auth`: Auth credentials.
* `response_body`: False => Prevents an error if response body is empty. Default = True
"""
return make_request("PATCH", url, **kwargs)

Expand All @@ -97,6 +104,7 @@ def make_delete_request(url: str, **kwargs):
* `json`: JSON to be passed in the request.
* `params`: Query parameters to be passed in the request.
* `auth`: Auth credentials.
* `response_body`: False => Prevents an error if response body is empty. Default = True
"""
return make_request("DELETE", url, **kwargs)

Expand Down

0 comments on commit edbfb69

Please sign in to comment.