@@ -252,7 +252,7 @@ def report_get(self, report, sort=None, limit=None, offset=None):
252
252
raise APIError (r .json ()['error' ])
253
253
return r .content .decode ()
254
254
255
- def get (self , resource , ** kwargs ):
255
+ def get (self , resource , * args , * *kwargs ):
256
256
"""Returns a list of models from :mod:`.models` if you query for
257
257
multiple models or a single instance of a model from :mod:`.models`
258
258
if you query for a specific `id`
@@ -271,6 +271,8 @@ def get(self, resource, **kwargs):
271
271
Get publisher with id 23::
272
272
273
273
>>> publisher = atomx.get('publisher/23')
274
+ >>>> # or get the same publisher using the id as parameter
275
+ >>> publisher = atomx.get('publisher', 23)
274
276
>>> assert publisher.id == 23
275
277
>>> assert isinstance(publisher, atomx.models.Publisher)
276
278
@@ -281,6 +283,16 @@ def get(self, resource, **kwargs):
281
283
>>> assert isinstance(profiles[0], atomx.models.Profile)
282
284
>>> assert profiles[0].advertiser.id == 42
283
285
286
+ :param args: All non-keyword arguments will get used to compute the ``resource``.
287
+ This makes it easier if you want to work with a variable resource path.
288
+
289
+ .. code-block:: python
290
+
291
+ advertiser_id = 42
292
+ attribute = 'profiles'
293
+ profiles = atomx.get('advertiser', advertiser_id, attribute)
294
+ # is equivalent to atomx.get('advertiser/42/profiles')
295
+
284
296
:param kwargs: Any argument is passed as URL parameter to the respective api endpoint.
285
297
See `API URL Parameters <http://wiki.atomx.com/doku.php?id=api#url_parameters>`_
286
298
in the wiki.
@@ -294,7 +306,10 @@ def get(self, resource, **kwargs):
294
306
295
307
:return: a class from :mod:`.models` or a list of models depending on param `resource`
296
308
"""
297
- r = self .session .get (self .api_endpoint + resource .strip ('/' ), params = kwargs )
309
+ resource = resource .strip ('/' )
310
+ for a in args :
311
+ resource += '/' + str (a )
312
+ r = self .session .get (self .api_endpoint + resource , params = kwargs )
298
313
if not r .ok :
299
314
raise APIError (r .json ()['error' ])
300
315
@@ -346,13 +361,19 @@ def put(self, resource, id, json, **kwargs):
346
361
raise APIError (r_json ['error' ])
347
362
return r_json [r_json ['resource' ]]
348
363
349
- def delete (self , resource , ** kwargs ):
364
+ def delete (self , resource , * args , * *kwargs ):
350
365
"""Send HTTP DELETE to ``resource``.
351
366
352
367
:param resource: Name of the resource to `DELETE`.
368
+ :param args: All non-keyword arguments will be used to compute the final ``resource``.
369
+ :param kwargs: Optional keyword arguments will be passed as query string to the
370
+ delete request.
353
371
:return: message or resource returned by the api.
354
372
"""
355
- r = self .session .delete (self .api_endpoint + resource .strip ('/' ), params = kwargs )
373
+ resource = resource .strip ('/' )
374
+ for a in args :
375
+ resource += '/' + str (a )
376
+ r = self .session .delete (self .api_endpoint + resource , params = kwargs )
356
377
r_json = r .json ()
357
378
if not r .ok :
358
379
raise APIError (r_json ['error' ])
0 commit comments