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

Add init option to add raw response to return values #414

Merged
merged 5 commits into from
Jun 27, 2019
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
8 changes: 7 additions & 1 deletion datadog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

def initialize(api_key=None, app_key=None, host_name=None, api_host=None,
statsd_host=None, statsd_port=None, statsd_use_default_route=False,
statsd_socket_path=None, statsd_namespace=None, **kwargs):
statsd_socket_path=None, statsd_namespace=None, return_raw_response=False, **kwargs):
"""
Initialize and configure Datadog.api and Datadog.statsd modules

Expand Down Expand Up @@ -68,6 +68,10 @@ def initialize(api_key=None, app_key=None, host_name=None, api_host=None,
:param mute: Mute any ApiError or ClientError before they escape \
from datadog.api.HTTPClient (default: True).
:type mute: boolean

:param return_raw_response: Whether or not to return the raw response object in addition \
to the decoded response content (default: False)
:type return_raw_response: boolean
"""
# API configuration
api._api_key = api_key or api._api_key or os.environ.get('DATADOG_API_KEY', os.environ.get('DD_API_KEY'))
Expand All @@ -93,6 +97,8 @@ def initialize(api_key=None, app_key=None, host_name=None, api_host=None,
if statsd_namespace:
statsd.namespace = text(statsd_namespace)

api._return_raw_response = return_raw_response

# HTTP client and API options
for key, value in iteritems(kwargs):
attribute = "_{}".format(key)
Expand Down
1 change: 0 additions & 1 deletion datadog/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,3 @@
from datadog.api.service_checks import ServiceCheck
from datadog.api.tags import Tag
from datadog.api.users import User
from datadog.api.api_client import get_http_response
25 changes: 7 additions & 18 deletions datadog/api/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,6 @@
from datadog.util.compat import is_p3k

log = logging.getLogger('datadog.api')
_http_response = None


def get_http_response():
"""
Getter for the most recent http request response object
"""
return _http_response


def _set_http_response(response):
global _http_response
_http_response = response


class APIClient(object):
Expand Down Expand Up @@ -95,7 +82,7 @@ def submit(cls, method, path, api_version=None, body=None, attach_host_name=Fals
# Import API, User and HTTP settings
from datadog.api import _api_key, _application_key, _api_host, \
_mute, _host_name, _proxies, _max_retries, _timeout, \
_cacert
_cacert, _return_raw_response

# Check keys and add then to params
if _api_key is None:
Expand Down Expand Up @@ -154,7 +141,6 @@ def submit(cls, method, path, api_version=None, body=None, attach_host_name=Fals

# Format response content
content = result.content
_set_http_response(result)

if content:
try:
Expand All @@ -170,10 +156,13 @@ def submit(cls, method, path, api_version=None, body=None, attach_host_name=Fals
else:
response_obj = None

if response_formatter is None:
return response_obj
if response_formatter is not None:
response_obj = response_formatter(response_obj)

if _return_raw_response:
return response_obj, result
else:
return response_formatter(response_obj)
return response_obj

except HttpTimeout:
cls._timeout_counter += 1
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ def test_initialize_options(self):
initialize(api_key=API_KEY, mute=False)
self.assertRaises(ApiError, MyCreatable.create)


def test_return_raw_response(self):
# Test default initialization sets return_raw_response to False
initialize()
assert not api._return_raw_response
# Assert that we can set this to True
initialize(return_raw_response=True)
assert api._return_raw_response
# Assert we get multiple fields back when set to True
initialize(api_key="aaaaaaaaaa", app_key="123456", return_raw_response=True)
data, raw = api.Monitor.get_all()


def test_default_values(self):
with EnvVars(ignore=[
"DATADOG_API_KEY",
Expand Down