Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use header for credentials everywhere #754

Merged
merged 2 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 1 addition & 31 deletions datadog/api/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 ?
Expand Down Expand Up @@ -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
25 changes: 0 additions & 25 deletions tests/unit/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down