@@ -41,6 +41,7 @@ class Atomx(object):
4141 """
4242 def __init__ (self , email , password , api_endpoint = API_ENDPOINT ):
4343 self .auth_tkt = None
44+ self .user = None
4445 self .email = email
4546 self .password = password
4647 self .api_endpoint = api_endpoint .rstrip ('/' ) + '/'
@@ -72,10 +73,12 @@ def login(self, email=None, password=None):
7273 raise InvalidCredentials
7374 raise APIError (r .json ()['error' ])
7475 self .auth_tkt = r .json ()['auth_tkt' ]
76+ self .user = models .User (** r .json ()['user' ])
7577
7678 def logout (self ):
7779 """Removes authentication token from session."""
7880 self .auth_tkt = None
81+ self .user = None
7982 self .session .get (self .api_endpoint + 'logout' )
8083
8184 def search (self , query ):
@@ -117,22 +120,22 @@ def search(self, query):
117120 for v in search_result [m ]]
118121 return search_result
119122
120- def report (self , scope = None , groups = None , sums = None , where = None ,
123+ def report (self , scope = None , groups = None , metrics = None , where = None ,
121124 from_ = None , to = None , timezone = 'UTC' , fast = True ):
122125 """Create a report.
123126
124127 See the `reporting atomx wiki <http://wiki.atomx.com/doku.php?id=reporting>`_
125- for details about parameters and available groups, sums .
128+ for details about parameters and available groups, metrics .
126129
127- :param str scope: either 'advertiser' or 'publisher ' to select the type of report.
128- If undefined but the groups column have an unambiguous attribute that's
129- unique to a certain scope, it's set automatically .
130+ :param str scope: either 'advertiser', 'publisher' or 'network ' to select the type
131+ of report. If undefined it tries to determine the `scope` automatically based
132+ on the `groups` and `metrics` parameters and the access rights of the api user .
130133 :param list groups: columns to group by.
131- :param list sums : columns to sum on.
134+ :param list metrics : columns to sum on.
132135 :param list where: is a list of expression lists.
133136 An expression list is in the form of ``[column, op, value]``:
134137
135- - ``column`` can be any of the ``groups`` or ``sums `` parameter columns.
138+ - ``column`` can be any of the ``groups`` or ``metrics `` parameter columns.
136139 - ``op`` can be any of ``==``, ``!=``, ``<=``, ``>=``,
137140 ``<``, ``>``, ``in`` or ``not in`` as a string.
138141 - ``value`` is either a number or in case of ``in``
@@ -154,22 +157,26 @@ def report(self, scope=None, groups=None, sums=None, where=None,
154157
155158 if groups :
156159 report_json ['groups' ] = groups
157- if sums :
158- report_json ['sums ' ] = sums
160+ if metrics :
161+ report_json ['metrics ' ] = metrics
159162 elif not groups :
160- raise MissingArgumentError ('Either `groups` or `sums ` have to be set.' )
163+ raise MissingArgumentError ('Either `groups` or `metrics ` have to be set.' )
161164
162165 if scope is None :
163- for i in report_json .get ('groups' , []) + report_json .get ('sums ' , []):
164- if i . split ( '_' )[ 0 ] in [ 'advertiser' , 'campaign' , 'creative' , 'conversion_pixel' ] :
165- scope = 'advertiser '
166+ for i in report_json .get ('groups' , []) + report_json .get ('metrics ' , []):
167+ if '_network' in i :
168+ scope = 'network '
166169 break
167170 else :
168- for i in report_json .get ('groups' , []) + report_json .get ('sums' , []):
169- if i .split ('_' )[0 ] in ['site' , 'placement' , 'user' ]:
170- scope = 'publisher'
171- break
172- else :
171+ user = self .user
172+ if len (user .networks ) > 0 :
173+ pass # user has network access so could be any report (leave scope as None)
174+ elif len (user .publishers ) > 0 and len (user .advertisers ) == 0 :
175+ scope = 'publishers'
176+ elif len (user .advertisers ) > 0 and len (user .publishers ) == 0 :
177+ scope = 'advertisers'
178+
179+ if scope is None :
173180 raise MissingArgumentError ('Unable to detect scope automatically. '
174181 'Please set `scope` parameter.' )
175182 report_json ['scope' ] = scope
0 commit comments