@@ -37,14 +37,19 @@ class Atomx(object):
3737 :param str password: password of your atomx user
3838 :param str api_endpoint: url for connections to the api
3939 (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`)
4042 :return: :class:`.Atomx` session to interact with the api
4143 """
42- def __init__ (self , email , password , api_endpoint = API_ENDPOINT ):
44+ def __init__ (self , email , password , api_endpoint = API_ENDPOINT , save_response = True ):
4345 self .auth_tkt = None
4446 self .user = None
4547 self .email = email
4648 self .password = password
4749 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
4853 self .session = requests .Session ()
4954 self .login ()
5055
@@ -109,9 +114,15 @@ def search(self, query):
109114 :return: dict with list of :mod:`.models` as values
110115 """
111116 r = self .session .get (self .api_endpoint + 'search' , params = {'q' : query })
117+ r_json = r .json ()
112118 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+
115126 # convert publisher, creative dicts etc from search result to Atomx.model
116127 for m in search_result .keys ():
117128 model_name = get_model_name (m )
@@ -207,9 +218,16 @@ def report(self, scope=None, groups=None, metrics=None, where=None,
207218 report_json ['emails' ] = emails
208219
209220 r = self .session .post (self .api_endpoint + 'report' , json = report_json )
221+ r_json = r .json ()
210222 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 )
213231
214232 def report_status (self , report ):
215233 """Get the status for a `report`.
@@ -229,6 +247,10 @@ def report_status(self, report):
229247 r = self .session .get (self .api_endpoint + 'report/' + report_id , params = {'status' : True })
230248 if not r .ok :
231249 raise APIError (r .json ()['error' ])
250+
251+ if self .save_response :
252+ self .last_response = r .json ()
253+
232254 return r .json ()['report' ]
233255
234256 def report_get (self , report , sort = None , limit = None , offset = None ):
@@ -324,6 +346,9 @@ def get(self, resource, *args, **kwargs):
324346 r_json = r .json ()
325347 model_name = r_json ['resource' ]
326348 res = r_json [model_name ]
349+ if self .save_response :
350+ del r_json [model_name ]
351+ self .last_response = r_json
327352 model = get_model_name (model_name )
328353 if model :
329354 if isinstance (res , list ):
@@ -349,7 +374,12 @@ def post(self, resource, json, **kwargs):
349374 r_json = r .json ()
350375 if not r .ok :
351376 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
353383
354384 def put (self , resource , id , json , ** kwargs ):
355385 """Send HTTP PUT to ``resource``/``id`` with ``json`` content.
@@ -367,7 +397,12 @@ def put(self, resource, id, json, **kwargs):
367397 r_json = r .json ()
368398 if not r .ok :
369399 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
371406
372407 def delete (self , resource , * args , ** kwargs ):
373408 """Send HTTP DELETE to ``resource``.
@@ -385,7 +420,12 @@ def delete(self, resource, *args, **kwargs):
385420 r_json = r .json ()
386421 if not r .ok :
387422 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
389429
390430 def save (self , model ):
391431 """Alias for :meth:`.models.AtomxModel.save` with `session` argument."""
0 commit comments