Skip to content

Commit d72ad76

Browse files
authored
Add init option to add raw response to return values (#414)
* Revert "Add new top level `api` function to get the response object of last request (#412)" * Add support for retrieving raw response
1 parent b6952bd commit d72ad76

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

datadog/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
def initialize(api_key=None, app_key=None, host_name=None, api_host=None,
3333
statsd_host=None, statsd_port=None, statsd_use_default_route=False,
34-
statsd_socket_path=None, statsd_namespace=None, **kwargs):
34+
statsd_socket_path=None, statsd_namespace=None, return_raw_response=False, **kwargs):
3535
"""
3636
Initialize and configure Datadog.api and Datadog.statsd modules
3737
@@ -68,6 +68,10 @@ def initialize(api_key=None, app_key=None, host_name=None, api_host=None,
6868
:param mute: Mute any ApiError or ClientError before they escape \
6969
from datadog.api.HTTPClient (default: True).
7070
:type mute: boolean
71+
72+
:param return_raw_response: Whether or not to return the raw response object in addition \
73+
to the decoded response content (default: False)
74+
:type return_raw_response: boolean
7175
"""
7276
# API configuration
7377
api._api_key = api_key or api._api_key or os.environ.get('DATADOG_API_KEY', os.environ.get('DD_API_KEY'))
@@ -93,6 +97,8 @@ def initialize(api_key=None, app_key=None, host_name=None, api_host=None,
9397
if statsd_namespace:
9498
statsd.namespace = text(statsd_namespace)
9599

100+
api._return_raw_response = return_raw_response
101+
96102
# HTTP client and API options
97103
for key, value in iteritems(kwargs):
98104
attribute = "_{}".format(key)

datadog/api/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,3 @@
3434
from datadog.api.service_checks import ServiceCheck
3535
from datadog.api.tags import Tag
3636
from datadog.api.users import User
37-
from datadog.api.api_client import get_http_response

datadog/api/api_client.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,6 @@
1616
from datadog.util.compat import is_p3k
1717

1818
log = logging.getLogger('datadog.api')
19-
_http_response = None
20-
21-
22-
def get_http_response():
23-
"""
24-
Getter for the most recent http request response object
25-
"""
26-
return _http_response
27-
28-
29-
def _set_http_response(response):
30-
global _http_response
31-
_http_response = response
3219

3320

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

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

155142
# Format response content
156143
content = result.content
157-
_set_http_response(result)
158144

159145
if content:
160146
try:
@@ -170,10 +156,13 @@ def submit(cls, method, path, api_version=None, body=None, attach_host_name=Fals
170156
else:
171157
response_obj = None
172158

173-
if response_formatter is None:
174-
return response_obj
159+
if response_formatter is not None:
160+
response_obj = response_formatter(response_obj)
161+
162+
if _return_raw_response:
163+
return response_obj, result
175164
else:
176-
return response_formatter(response_obj)
165+
return response_obj
177166

178167
except HttpTimeout:
179168
cls._timeout_counter += 1

tests/unit/api/test_api.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,19 @@ def test_initialize_options(self):
129129
initialize(api_key=API_KEY, mute=False)
130130
self.assertRaises(ApiError, MyCreatable.create)
131131

132+
133+
def test_return_raw_response(self):
134+
# Test default initialization sets return_raw_response to False
135+
initialize()
136+
assert not api._return_raw_response
137+
# Assert that we can set this to True
138+
initialize(return_raw_response=True)
139+
assert api._return_raw_response
140+
# Assert we get multiple fields back when set to True
141+
initialize(api_key="aaaaaaaaaa", app_key="123456", return_raw_response=True)
142+
data, raw = api.Monitor.get_all()
143+
144+
132145
def test_default_values(self):
133146
with EnvVars(ignore=[
134147
"DATADOG_API_KEY",

0 commit comments

Comments
 (0)