Skip to content

Commit 2728b6d

Browse files
authored
Serialize body in async client (#952)
* Serialiaze body in async client Closes #951 * Support compression as well
1 parent 2ce43d5 commit 2728b6d

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

src/datadog_api_client/rest.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def request(
131131
headers["Content-Type"] = "application/json"
132132
if query_params:
133133
url += "?" + urlencode(query_params)
134-
if ("Content-Type" not in headers) or (re.search("json", headers["Content-Type"], re.IGNORECASE)):
134+
if "Content-Type" not in headers or re.search("json", headers["Content-Type"], re.IGNORECASE):
135135
request_body = None
136136
if body is not None:
137137
request_body = json.dumps(body)
@@ -194,7 +194,12 @@ def request(
194194
# For `GET`, `HEAD`
195195
else:
196196
r = self.pool_manager.request(
197-
method, url, fields=query_params, preload_content=_preload_content, timeout=timeout, headers=headers
197+
method,
198+
url,
199+
fields=query_params,
200+
preload_content=_preload_content,
201+
timeout=timeout,
202+
headers=headers,
198203
)
199204
except urllib3.exceptions.SSLError as e:
200205
msg = "{0}\n{1}".format(type(e).__name__, str(e))
@@ -269,7 +274,21 @@ async def request(
269274
(connection, read) timeouts.
270275
"""
271276
assert not post_params, "not supported for now"
272-
response = await self._client.request(url, method, headers, query_params, body, timeouts=_request_timeout)
277+
request_body = None
278+
if (
279+
"Content-Type" not in headers
280+
or re.search("json", headers["Content-Type"], re.IGNORECASE)
281+
and body is not None
282+
):
283+
request_body = json.dumps(body)
284+
if headers.get("Content-Encoding") == "gzip":
285+
compress = zlib.compressobj(wbits=16 + zlib.MAX_WBITS)
286+
request_body = compress.compress(request_body.encode("utf-8")) + compress.flush()
287+
elif headers.get("Content-Encoding") == "deflate":
288+
request_body = zlib.compress(request_body.encode("utf-8"))
289+
response = await self._client.request(
290+
url, method, headers, query_params, request_body, timeouts=_request_timeout
291+
)
273292

274293
if not 200 <= response.status_code <= 299:
275294
data = b""

tests/test_async.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import time
23

34
import pytest
45

@@ -41,3 +42,31 @@ async def test_basic():
4142
_, code, headers = await api_instance.list_dashboards(_return_http_data_only=False)
4243
assert code == 200
4344
assert headers["Content-Type"] == "application/json"
45+
46+
47+
@pytest.mark.asyncio
48+
async def test_body():
49+
if os.getenv("RECORD", "false").lower() != "none":
50+
pytest.skip("Integration test")
51+
configuration = Configuration()
52+
configuration.api_key["apiKeyAuth"] = os.getenv("DD_TEST_CLIENT_API_KEY", "fake")
53+
configuration.api_key["appKeyAuth"] = os.getenv("DD_TEST_CLIENT_APP_KEY", "fake")
54+
configuration.debug = os.getenv("DEBUG") in {"true", "1", "yes", "on"}
55+
if "DD_TEST_SITE" in os.environ:
56+
configuration.server_index = 2
57+
configuration.server_variables["site"] = os.environ["DD_TEST_SITE"]
58+
59+
body = {
60+
"series": [
61+
{
62+
"metric": "system.load.1",
63+
"points": [[time.time(), 0.7]],
64+
"tags": ["test:async_test"],
65+
},
66+
]
67+
}
68+
69+
async with AsyncApiClient(configuration) as api_client:
70+
api_instance = metrics_api.MetricsApi(api_client)
71+
_, code, headers = await api_instance.submit_metrics(body=body, _return_http_data_only=False)
72+
assert code == 202

0 commit comments

Comments
 (0)