@@ -37,14 +37,19 @@ class Atomx(object):
37
37
:param str password: password of your atomx user
38
38
:param str api_endpoint: url for connections to the api
39
39
(defaults to `https://api.atomx.com/{API_VERSION}`)
40
+ :param bool save_response: If `True` save the last api response meta info
41
+ (without the resource payload) in :attr:`.Atomx.last_response`. (default: `True`)
40
42
:return: :class:`.Atomx` session to interact with the api
41
43
"""
42
- def __init__ (self , email , password , api_endpoint = API_ENDPOINT ):
44
+ def __init__ (self , email , password , api_endpoint = API_ENDPOINT , save_response = True ):
43
45
self .auth_tkt = None
44
46
self .user = None
45
47
self .email = email
46
48
self .password = password
47
49
self .api_endpoint = api_endpoint .rstrip ('/' ) + '/'
50
+ self .save_response = save_response
51
+ #: Contains the response of the last api call, if `save_response` was set `True`
52
+ self .last_response = None
48
53
self .session = requests .Session ()
49
54
self .login ()
50
55
@@ -109,9 +114,15 @@ def search(self, query):
109
114
:return: dict with list of :mod:`.models` as values
110
115
"""
111
116
r = self .session .get (self .api_endpoint + 'search' , params = {'q' : query })
117
+ r_json = r .json ()
112
118
if not r .ok :
113
- raise APIError (r .json ()['error' ])
114
- search_result = r .json ()['search' ]
119
+ raise APIError (r_json ['error' ])
120
+ search_result = r_json ['search' ]
121
+
122
+ if self .save_response :
123
+ del r_json ['search' ]
124
+ self .last_response = r_json
125
+
115
126
# convert publisher, creative dicts etc from search result to Atomx.model
116
127
for m in search_result .keys ():
117
128
model_name = get_model_name (m )
@@ -207,9 +218,16 @@ def report(self, scope=None, groups=None, metrics=None, where=None,
207
218
report_json ['emails' ] = emails
208
219
209
220
r = self .session .post (self .api_endpoint + 'report' , json = report_json )
221
+ r_json = r .json ()
210
222
if not r .ok :
211
- raise APIError (r .json ()['error' ])
212
- return models .Report (self , query = r .json ()['query' ], ** r .json ()['report' ])
223
+ raise APIError (r_json ['error' ])
224
+ report = r_json ['report' ]
225
+
226
+ if self .save_response :
227
+ del r_json ['report' ]
228
+ self .last_response = r_json
229
+
230
+ return models .Report (self , query = r .json ()['query' ], ** report )
213
231
214
232
def report_status (self , report ):
215
233
"""Get the status for a `report`.
@@ -229,6 +247,10 @@ def report_status(self, report):
229
247
r = self .session .get (self .api_endpoint + 'report/' + report_id , params = {'status' : True })
230
248
if not r .ok :
231
249
raise APIError (r .json ()['error' ])
250
+
251
+ if self .save_response :
252
+ self .last_response = r .json ()
253
+
232
254
return r .json ()['report' ]
233
255
234
256
def report_get (self , report , sort = None , limit = None , offset = None ):
@@ -324,6 +346,9 @@ def get(self, resource, *args, **kwargs):
324
346
r_json = r .json ()
325
347
model_name = r_json ['resource' ]
326
348
res = r_json [model_name ]
349
+ if self .save_response :
350
+ del r_json [model_name ]
351
+ self .last_response = r_json
327
352
model = get_model_name (model_name )
328
353
if model :
329
354
if isinstance (res , list ):
@@ -349,7 +374,12 @@ def post(self, resource, json, **kwargs):
349
374
r_json = r .json ()
350
375
if not r .ok :
351
376
raise APIError (r_json ['error' ])
352
- return r_json [r_json ['resource' ]]
377
+ model_name = r_json ['resource' ]
378
+ res = r_json [model_name ]
379
+ if self .save_response :
380
+ del r_json [model_name ]
381
+ self .last_response = r_json
382
+ return res
353
383
354
384
def put (self , resource , id , json , ** kwargs ):
355
385
"""Send HTTP PUT to ``resource``/``id`` with ``json`` content.
@@ -367,7 +397,12 @@ def put(self, resource, id, json, **kwargs):
367
397
r_json = r .json ()
368
398
if not r .ok :
369
399
raise APIError (r_json ['error' ])
370
- return r_json [r_json ['resource' ]]
400
+ model_name = r_json ['resource' ]
401
+ res = r_json [model_name ]
402
+ if self .save_response :
403
+ del r_json [model_name ]
404
+ self .last_response = r_json
405
+ return res
371
406
372
407
def delete (self , resource , * args , ** kwargs ):
373
408
"""Send HTTP DELETE to ``resource``.
@@ -385,7 +420,12 @@ def delete(self, resource, *args, **kwargs):
385
420
r_json = r .json ()
386
421
if not r .ok :
387
422
raise APIError (r_json ['error' ])
388
- return r_json [r_json ['resource' ]]
423
+ model_name = r_json ['resource' ]
424
+ res = r_json [model_name ]
425
+ if self .save_response :
426
+ del r_json [model_name ]
427
+ self .last_response = r_json
428
+ return res
389
429
390
430
def save (self , model ):
391
431
"""Alias for :meth:`.models.AtomxModel.save` with `session` argument."""
0 commit comments