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

Commit 3905e3f

Browse files
committed
add report.get method with limit and sort params
fix api.get for advertisers/publishers/networkds_list
1 parent 5a9daa0 commit 3905e3f

3 files changed

Lines changed: 41 additions & 10 deletions

File tree

atomx/__init__.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
import requests
88
from atomx.version import API_VERSION, VERSION
99
from atomx import models
10-
from atomx.utils import get_model_name
10+
from atomx.utils import (
11+
get_model_name,
12+
model_name_to_rest,
13+
)
1114
from atomx.exceptions import (
1215
APIError,
1316
ModelNotFoundError,
@@ -213,7 +216,7 @@ def report_status(self, report):
213216
raise APIError(r.json()['error'])
214217
return r.json()['report']
215218

216-
def report_get(self, report):
219+
def report_get(self, report, limit=None, sort=None):
217220
"""Get the content (csv) of a :class:`.models.Report`
218221
219222
Typically used by calling :meth:`.models.Report.content` or
@@ -229,7 +232,13 @@ def report_get(self, report):
229232
else:
230233
report_id = report
231234

232-
r = self.session.get(self.api_endpoint + 'report/' + report_id)
235+
params = {}
236+
if limit:
237+
params['limit'] = int(limit)
238+
if sort:
239+
params['sort'] = sort
240+
241+
r = self.session.get(self.api_endpoint + 'report/' + report_id, params=params)
233242
if not r.ok:
234243
raise APIError(r.json()['error'])
235244
return r.content.decode()
@@ -286,6 +295,9 @@ def get(self, resource, **kwargs):
286295
model = get_model_name(model_name)
287296
if model:
288297
if isinstance(res, list):
298+
if model_name.endswith('_list'):
299+
# special case for _list requests
300+
res = [{'id': id, 'name': name} for id, name in res]
289301
return [getattr(models, model)(self, **m) for m in res]
290302
return getattr(models, model)(self, **res)
291303
return res

atomx/models.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,19 @@ def status(self):
207207
self.__init__(session=self.session, **status)
208208
return self
209209

210+
def get(self, limit=None, sort=None):
211+
"""Get the first ``limit`` lines of the report ``content``
212+
and in the specified ``sort`` order.
213+
214+
:param int limit: limit the amount of lines to return (defaults to no limit)
215+
:param str sort: defines the sort order of the report content.
216+
``sort`` can be `column_name`[.asc|.desc][,column_name[.asc|.desc]]...
217+
:return: report content
218+
"""
219+
if not self.is_ready:
220+
raise ReportNotReadyError()
221+
return self.session.report_get(self, limit=limit, sort=sort)
222+
210223
@property
211224
def content(self):
212225
"""Returns the content (csv) of the `report`."""

atomx/utils.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,30 @@
44

55
def get_model_name(name):
66
"""Checks that :param:`name` is a valid model.
7-
Converts plural `name` to capitalized singular form and '-' or '_' separation to camelCase.
7+
Converts plural `name` to capitalized singular form,
8+
rstrips '_list' (e.g. `advertisers_list`)
9+
and '-' or '_' separation to camelCase.
810
Returns the name of the model if found, False otherwise.
911
E.g.
1012
11-
>>> check_model_name("countries")
13+
>>> check_model_name('countries')
1214
"Country"
13-
>>> check_model_name("ADVERTISERS")
15+
>>> check_model_name('ADVERTISERS')
1416
"Advertiser"
15-
>>> check_model_name("conversion-pixels")
17+
>>> check_model_name('conversion-pixels')
1618
"ConversionPixel"
17-
>>> check_model_name("operating_system")
19+
>>> check_model_name('operating_system')
1820
"OperatingSystem"
19-
>>> check_model_name("InvalidModel")
21+
>>> check_model_name('InvalidModel')
2022
False
23+
>>> check_model_name('advertisers_list')
24+
"Advertiser"
2125
2226
:param str name: model name to convert
2327
:return: name of the model or False
2428
"""
29+
if name.endswith('_list'):
30+
name = name[:-5]
2531
if '-' in name:
2632
model_name = ''.join(m.capitalize() for m in name.split('-'))
2733
elif '_' in name:
@@ -55,7 +61,7 @@ def model_name_to_rest(name):
5561
5662
>>> assert model_name_to_rest('ConversionPixels') == 'conversion-pixels'
5763
>>> assert model_name_to_rest('OperatingSystem') == 'operating-system'
58-
>>> assert model_name_to_rest('Advertiser') == 'Advertiser'
64+
>>> assert model_name_to_rest('Advertiser') == 'advertiser'
5965
6066
:param str name: Name of the model to convert
6167
:return: :class:`str` with the transformed name.

0 commit comments

Comments
 (0)