Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Commit 703746c

Browse files
committed
:meth:atomx.Atomx.get and delete accept non-keyword arguments that are used to compute the resource path.
1 parent 828f9ef commit 703746c

File tree

5 files changed

+45
-7
lines changed

5 files changed

+45
-7
lines changed

CHANGES.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
----------------
33

44
- add :meth:`atomx.Atomx.delete` to send a ``HTTP DELETE`` request to the api
5-
5+
- :meth:`atomx.Atomx.get` and :meth:`atomx.Atomx.delete` accept non-keyword arguments
6+
that are used to compute the final resource path.
67

78
1.2
89
---

README.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ Example Usage
4747
4848
4949
# you can also get attributes
50-
profiles = atomx.get('advertiser/88/profiles')
50+
profiles = atomx.get('advertiser', 88, 'profiles')
51+
# equivalent is to pass the complete resource path as string instead of arguments
52+
profiles = atomx.get('advertiser/88/profiles') # same as above
5153
# profiles is now a list of `atomx.models.Profile` that you can
5254
# read, update, etc again.
5355
profiles[0].click_frequency_cap_per = 86400

atomx/__init__.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ def report_get(self, report, sort=None, limit=None, offset=None):
252252
raise APIError(r.json()['error'])
253253
return r.content.decode()
254254

255-
def get(self, resource, **kwargs):
255+
def get(self, resource, *args, **kwargs):
256256
"""Returns a list of models from :mod:`.models` if you query for
257257
multiple models or a single instance of a model from :mod:`.models`
258258
if you query for a specific `id`
@@ -271,6 +271,8 @@ def get(self, resource, **kwargs):
271271
Get publisher with id 23::
272272
273273
>>> publisher = atomx.get('publisher/23')
274+
>>>> # or get the same publisher using the id as parameter
275+
>>> publisher = atomx.get('publisher', 23)
274276
>>> assert publisher.id == 23
275277
>>> assert isinstance(publisher, atomx.models.Publisher)
276278
@@ -281,6 +283,16 @@ def get(self, resource, **kwargs):
281283
>>> assert isinstance(profiles[0], atomx.models.Profile)
282284
>>> assert profiles[0].advertiser.id == 42
283285
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+
284296
:param kwargs: Any argument is passed as URL parameter to the respective api endpoint.
285297
See `API URL Parameters <http://wiki.atomx.com/doku.php?id=api#url_parameters>`_
286298
in the wiki.
@@ -294,7 +306,10 @@ def get(self, resource, **kwargs):
294306
295307
:return: a class from :mod:`.models` or a list of models depending on param `resource`
296308
"""
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)
298313
if not r.ok:
299314
raise APIError(r.json()['error'])
300315

@@ -346,13 +361,19 @@ def put(self, resource, id, json, **kwargs):
346361
raise APIError(r_json['error'])
347362
return r_json[r_json['resource']]
348363

349-
def delete(self, resource, **kwargs):
364+
def delete(self, resource, *args, **kwargs):
350365
"""Send HTTP DELETE to ``resource``.
351366
352367
: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.
353371
:return: message or resource returned by the api.
354372
"""
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)
356377
r_json = r.json()
357378
if not r.ok:
358379
raise APIError(r_json['error'])

atomx/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def reload(self, session=None):
154154
if not hasattr(self, 'id'):
155155
raise ModelNotFoundError("Can't reload without 'id' parameter. "
156156
"Forgot to save() first?")
157-
res = session.get(self._resource_name + '/' + str(self.id))
157+
res = session.get(self._resource_name, self.id)
158158
self.__init__(session=session, **res.json)
159159
return self
160160

docs/usage.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ Or query all profiles of advertiser 42 ordered by last updated:
5656
profiles = atomx.get('advertiser/42/profiles', order_by='updated_at.desc')
5757
5858
59+
All non-keyword arguments that you pass :meth:`atomx.Atomx.get` will get used
60+
to compute the ``resource``. This makes it easier if you have the ``id``
61+
(and/or attribute) stored in a variable.
62+
63+
E.g.
64+
65+
.. code-block:: python
66+
67+
advertiser_id = 42
68+
attribute = 'profiles'
69+
profiles = atomx.get('advertiser', advertiser_id, attribute)
70+
# is equivalent to atomx.get('advertiser/42/profiles')
71+
72+
5973
Or get all domains where the hostname contains `atom`:
6074

6175
.. code-block:: python

0 commit comments

Comments
 (0)