Skip to content

Commit

Permalink
Add init option to add raw response to return values (DataDog#414)
Browse files Browse the repository at this point in the history
* Revert "Add new top level `api` function to get the response object of last request (DataDog#412)"

* Add support for retrieving raw response
  • Loading branch information
nmuesch authored and David Bouchare committed Oct 25, 2019
1 parent 6469bb4 commit 9b2418d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
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

0 comments on commit 9b2418d

Please sign in to comment.