From 33c29f7495f94faeae0936593bcffa353cadc9ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Herv=C3=A9?= Date: Mon, 23 Jan 2023 09:16:25 +0100 Subject: [PATCH 1/2] Use header for credentials everywhere All endpoint supports it now, let's use headers everywhere. --- datadog/api/api_client.py | 30 ------------------------------ tests/unit/api/test_api.py | 25 ------------------------- 2 files changed, 55 deletions(-) diff --git a/datadog/api/api_client.py b/datadog/api/api_client.py index 9214a1b15..934359b91 100644 --- a/datadog/api/api_client.py +++ b/datadog/api/api_client.py @@ -127,15 +127,6 @@ def submit( if not api_version: api_version = _api_version - # set api and app keys in params only for some endpoints and thus remove keys from headers - # as they cannot be set in both params and headers - if cls._set_api_and_app_keys_in_params(api_version, path): - params["api_key"] = _api_key - del headers["DD-API-KEY"] - if _application_key: - params["application_key"] = _application_key - del headers["DD-APPLICATION-KEY"] - # Attach host name to body if attach_host_name and body: # Is it a 'series' list of objects ? @@ -297,24 +288,3 @@ def _backoff_status(cls): backed_off_time = now - cls._backoff_timestamp backoff_time_left = cls._backoff_period - backed_off_time return round(backed_off_time, 2), round(backoff_time_left, 2) - - @classmethod - def _set_api_and_app_keys_in_params(cls, api_version, path): - """ - Some endpoints need api and app keys to be set in params only - For these endpoints, api and app keys in headers are ignored - :return: True if this endpoint needs api and app keys params set - """ - constructed_path = construct_path(api_version, path) - - set_of_paths = { - "v1/distribution_points", - "v1/series", - "v1/check_run", - "v1/events", - "v1/screen", - } - if constructed_path in set_of_paths: - return True - - return False diff --git a/tests/unit/api/test_api.py b/tests/unit/api/test_api.py index 7887b4fbe..e44e4ce43 100644 --- a/tests/unit/api/test_api.py +++ b/tests/unit/api/test_api.py @@ -179,31 +179,6 @@ def test_request_parameters(self): assert "api_key" not in options['params'] assert "application_key" not in options['params'] - def test_request_parameters_api_keys_in_params(self): - """ - API parameters are set with `initialize` method. - """ - # Test API, application keys, API host, and some HTTP client options - initialize(api_key=API_KEY, app_key=APP_KEY, api_host=API_HOST) - - # Make a simple API call - MyParamsApiKeyCreatable.create() - - _, options = self.request_mock.call_args() - - # Assert `requests` parameters - self.assertIn('params', options) - - self.assertIn('headers', options) - - # for resources in MyParamsApiKey, api key and application key needs to be in url params - # any api and app keys in headers are ignored - self.assertEqual(options['headers']['Content-Type'], 'application/json') - self.assertEqual(options['params']['api_key'], API_KEY) - self.assertEqual(options['params']['application_key'], APP_KEY) - assert "DD-API-KEY" not in options['headers'] - assert "DD-APPLICATION-KEY" not in options['headers'] - def test_initialize_options(self): """ HTTP client and API options are set with `initialize` method. From 705a26f66cc3f962b2a4cc6041ab7298a2822b4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Herv=C3=A9?= Date: Mon, 23 Jan 2023 14:20:15 +0100 Subject: [PATCH 2/2] Lint --- datadog/api/api_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datadog/api/api_client.py b/datadog/api/api_client.py index 934359b91..db34873bf 100644 --- a/datadog/api/api_client.py +++ b/datadog/api/api_client.py @@ -12,7 +12,7 @@ from datadog.api.exceptions import ClientError, ApiError, HttpBackoff, HttpTimeout, ApiNotInitialized from datadog.api.http_client import resolve_http_client from datadog.util.compat import is_p3k -from datadog.util.format import construct_url, construct_path, normalize_tags +from datadog.util.format import construct_url, normalize_tags log = logging.getLogger("datadog.api")