Skip to content

Commit

Permalink
Serialize body in async client (#952)
Browse files Browse the repository at this point in the history
* Serialiaze body in async client

Closes #951

* Support compression as well
  • Loading branch information
therve committed Apr 13, 2022
1 parent 2ce43d5 commit 2728b6d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
25 changes: 22 additions & 3 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,7 +274,21 @@ async def request(
(connection, read) timeouts.
"""
assert not post_params, "not supported for now"
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
29 changes: 29 additions & 0 deletions tests/test_async.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import time

import pytest

Expand Down Expand Up @@ -41,3 +42,31 @@ async def test_basic():
_, code, headers = await api_instance.list_dashboards(_return_http_data_only=False)
assert code == 200
assert headers["Content-Type"] == "application/json"


@pytest.mark.asyncio
async def test_body():
if os.getenv("RECORD", "false").lower() != "none":
pytest.skip("Integration test")
configuration = Configuration()
configuration.api_key["apiKeyAuth"] = os.getenv("DD_TEST_CLIENT_API_KEY", "fake")
configuration.api_key["appKeyAuth"] = os.getenv("DD_TEST_CLIENT_APP_KEY", "fake")
configuration.debug = os.getenv("DEBUG") in {"true", "1", "yes", "on"}
if "DD_TEST_SITE" in os.environ:
configuration.server_index = 2
configuration.server_variables["site"] = os.environ["DD_TEST_SITE"]

body = {
"series": [
{
"metric": "system.load.1",
"points": [[time.time(), 0.7]],
"tags": ["test:async_test"],
},
]
}

async with AsyncApiClient(configuration) as api_client:
api_instance = metrics_api.MetricsApi(api_client)
_, code, headers = await api_instance.submit_metrics(body=body, _return_http_data_only=False)
assert code == 202

0 comments on commit 2728b6d

Please sign in to comment.