Skip to content

Commit

Permalink
Support compression as well
Browse files Browse the repository at this point in the history
  • Loading branch information
therve committed Apr 12, 2022
1 parent bbe95f7 commit b9695b8
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/datadog_api_client/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def request(
headers["Content-Type"] = "application/json"
if query_params:
url += "?" + urlencode(query_params)
if ("Content-Type" not in headers) or (re.search("json", headers["Content-Type"], re.IGNORECASE)):
if "Content-Type" not in headers or re.search("json", headers["Content-Type"], re.IGNORECASE):
request_body = None
if body is not None:
request_body = json.dumps(body)
Expand Down Expand Up @@ -194,7 +194,12 @@ def request(
# For `GET`, `HEAD`
else:
r = self.pool_manager.request(
method, url, fields=query_params, preload_content=_preload_content, timeout=timeout, headers=headers
method,
url,
fields=query_params,
preload_content=_preload_content,
timeout=timeout,
headers=headers,
)
except urllib3.exceptions.SSLError as e:
msg = "{0}\n{1}".format(type(e).__name__, str(e))
Expand Down Expand Up @@ -269,9 +274,21 @@ async def request(
(connection, read) timeouts.
"""
assert not post_params, "not supported for now"
if body is not None:
body = json.dumps(body)
response = await self._client.request(url, method, headers, query_params, body, timeouts=_request_timeout)
request_body = None
if (
"Content-Type" not in headers
or re.search("json", headers["Content-Type"], re.IGNORECASE)
and body is not None
):
request_body = json.dumps(body)
if headers.get("Content-Encoding") == "gzip":
compress = zlib.compressobj(wbits=16 + zlib.MAX_WBITS)
request_body = compress.compress(request_body.encode("utf-8")) + compress.flush()
elif headers.get("Content-Encoding") == "deflate":
request_body = zlib.compress(request_body.encode("utf-8"))
response = await self._client.request(
url, method, headers, query_params, request_body, timeouts=_request_timeout
)

if not 200 <= response.status_code <= 299:
data = b""
Expand Down

0 comments on commit b9695b8

Please sign in to comment.