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

Commit 6f00903

Browse files
committed
- Add :class:atomx.models.ScheduledReport
- :meth:`atomx.Atomx.report` accepts ``when`` and ``interval`` to create a :class:`atomx.models.ScheduledReport` - Changed wiki links to https - release 1.4
1 parent 4081b67 commit 6f00903

File tree

6 files changed

+66
-21
lines changed

6 files changed

+66
-21
lines changed

CHANGES.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
1.4 (unreleased)
2-
-----------------
1+
1.4
2+
---
33

44
- Change default API version to `v2`
55
- If :meth:`atomx.Atomx.post` returns a list, auto-convert list of objects
66
to a list of :mod:`atomx.models` models. (Useful for `POST` to the `/domains` endpoint)
7+
- Add :class:`atomx.models.ScheduledReport`
8+
- :meth:`atomx.Atomx.report` accepts ``when`` and ``interval`` to create a
9+
:class:`atomx.models.ScheduledReport`
710

811

912
1.3

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Interface for the atomx rest api.
66
For more information read the full
77
`documentation online <http://atomx-api-python.readthedocs.org/en/latest/index.html>`_,
88
report bugs in `github <https://github.com/atomx/atomx-api-python>`_
9-
or see the `atomx wiki <http://wiki.atomx.com/doku.php?id=api>`_
9+
or see the `atomx wiki <https://wiki.atomx.com/api>`_
1010

1111

1212
Example Usage

atomx/__init__.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Atomx(object):
3131
"""Interface for the api on api.atomx.com.
3232
3333
To learn more about the api visit the
34-
`atomx wiki <http://wiki.atomx.com/doku.php?id=api>`_
34+
`atomx wiki <https://wiki.atomx.com/api>`_
3535
3636
:param str email: email address of your atomx user
3737
:param str password: password of your atomx user
@@ -131,11 +131,11 @@ def search(self, query):
131131
for v in search_result[m]]
132132
return search_result
133133

134-
def report(self, scope=None, groups=None, metrics=None, where=None,
135-
from_=None, to=None, timezone='UTC', emails=None, fast=True):
134+
def report(self, scope=None, groups=None, metrics=None, where=None, from_=None, to=None,
135+
timezone='UTC', emails=None, fast=True, when=None, interval=None):
136136
"""Create a report.
137137
138-
See the `reporting atomx wiki <http://wiki.atomx.com/doku.php?id=reporting>`_
138+
See the `reporting atomx wiki <https://wiki.atomx.com/reporting>`_
139139
for details about parameters and available groups, metrics.
140140
141141
:param str scope: either 'advertiser', 'publisher' or 'network' to select the type
@@ -157,14 +157,16 @@ def report(self, scope=None, groups=None, metrics=None, where=None,
157157
:param datetime.datetime to: :class:`datetime.datetime` where the report
158158
should end (exclusive). (defaults to `datetime.now()` if undefined)
159159
:param str timezone: Timezone used for all times. (defaults to `UTC`)
160-
For a supported list see http://wiki.atomx.com/doku.php?id=timezones
160+
For a supported list see https://wiki.atomx.com/timezones
161161
:param emails: One or multiple email addresses that should get
162162
notified once the report is finished and ready to download.
163163
:type emails: str or list
164164
:param bool fast: if `False` the report will always be run against the low level data.
165165
This is useful for billing reports for example.
166166
The default is `True` which means it will always try to use aggregate data
167167
to speed up the query.
168+
:param str when: When should the scheduled report run. (daily, monthly, monday-sunday)
169+
:param str interval: Time period included in the scheduled report ('N days' or 'N month')
168170
:return: A :class:`atomx.models.Report` model
169171
"""
170172
report_json = {'timezone': timezone, 'fast': fast}
@@ -198,19 +200,26 @@ def report(self, scope=None, groups=None, metrics=None, where=None,
198200
if where:
199201
report_json['where'] = where
200202

201-
if from_ is None:
202-
from_ = datetime.now() - timedelta(days=7)
203-
if isinstance(from_, datetime):
204-
report_json['from'] = from_.strftime("%Y-%m-%d %H:00:00")
203+
if when and interval:
204+
is_scheduled_report = True
205+
report_json['when'] = when
206+
report_json['interval'] = interval
205207
else:
206-
report_json['from'] = from_
208+
is_scheduled_report = False
207209

208-
if to is None:
209-
to = datetime.now()
210-
if isinstance(to, datetime):
211-
report_json['to'] = to.strftime("%Y-%m-%d %H:00:00")
212-
else:
213-
report_json['to'] = to
210+
if from_ is None:
211+
from_ = datetime.now() - timedelta(days=7)
212+
if isinstance(from_, datetime):
213+
report_json['from'] = from_.strftime("%Y-%m-%d %H:00:00")
214+
else:
215+
report_json['from'] = from_
216+
217+
if to is None:
218+
to = datetime.now()
219+
if isinstance(to, datetime):
220+
report_json['to'] = to.strftime("%Y-%m-%d %H:00:00")
221+
else:
222+
report_json['to'] = to
214223

215224
if emails:
216225
if not isinstance(emails, list):
@@ -227,6 +236,9 @@ def report(self, scope=None, groups=None, metrics=None, where=None,
227236
del r_json['report']
228237
self.last_response = r_json
229238

239+
if is_scheduled_report:
240+
return models.ScheduledReport(self, query=r.json()['query'], **report)
241+
230242
return models.Report(self, query=r.json()['query'], **report)
231243

232244
def report_status(self, report):
@@ -324,7 +336,7 @@ def get(self, resource, *args, **kwargs):
324336
# is equivalent to atomx.get('advertiser/42/profiles')
325337
326338
:param kwargs: Any argument is passed as URL parameter to the respective api endpoint.
327-
See `API URL Parameters <http://wiki.atomx.com/doku.php?id=api#url_parameters>`_
339+
See `API URL Parameters <https://wiki.atomx.com/api#url_parameters>`_
328340
in the wiki.
329341
330342
Example:
@@ -354,6 +366,11 @@ def get(self, resource, *args, **kwargs):
354366
if isinstance(res, list):
355367
return [getattr(models, model)(self, **m) for m in res]
356368
return getattr(models, model)(self, **res)
369+
elif model_name == 'reporting': # special case for `/reports` status
370+
return {
371+
'reports': [models.Report(self, **m) for m in res['reports']],
372+
'scheduled': [models.ScheduledReport(self, **m) for m in res['scheduled']]
373+
}
357374
return res
358375

359376
def post(self, resource, json, **kwargs):

atomx/models.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,28 @@ def reload(self, session=None):
190190
{'__doc__': ':class:`.AtomxModel` for {}'.format(m)})
191191

192192

193+
class ScheduledReport(object):
194+
"""A report scheduling object that you get back when registering a new scheduled report.
195+
See the `scheduling section in the atomx wiki
196+
<https://wiki.atomx.com/reporting#scheduling_reports>`_.
197+
"""
198+
199+
def __init__(self, session, id, query, **kwargs):
200+
self.session = session
201+
self.id = id
202+
self.query = query
203+
204+
def __repr__(self):
205+
return "ScheduledReport(id='{}', query={})".format(self.id, self.query)
206+
207+
def __eq__(self, other):
208+
return self.id == getattr(other, 'id', 'INVALID')
209+
210+
def delete(self, session=None):
211+
"""Delete scheduled report"""
212+
return self.session.delete('report', self.id)
213+
214+
193215
class Report(object):
194216
"""Represents a `report` you get back from :meth:`atomx.Atomx.report`."""
195217

atomx/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
VERSION = '1.4b'
1+
VERSION = '1.4'
22
API_VERSION = 'v2'

docs/api.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ Models
1919
.. automodule:: atomx.models
2020
:members:
2121

22+
.. autoclass:: atomx.models.ScheduledReport
23+
:members:
24+
2225
.. autoclass:: atomx.models.Report
2326
:members:
2427

0 commit comments

Comments
 (0)