Remove prettyprinting unless DEBUG is set in env and add a new
Remove prettyprinting unless DEBUG is set in env and add a new
test for actually making the calls.

file:a/README.rst -> file:b/README.rst
--- a/README.rst
+++ b/README.rst
@@ -32,11 +32,9 @@
 
       googleanalytics.id = UA-1010101-1
       googleanalytics.account = Account name (i.e. data.gov.uk, see top level item at https://www.google.com/analytics)
-      googleanalytics.username = googleaccount@gmail.com
-      googleanalytics.password = googlepassword
       ga-report.period = monthly
 
-   Note that your password will be readable by system administrators on your server. Rather than use sensitive account details, it is suggested you give access to the GA account to a new Google account that you create just for this purpose.
+   Note that your credentials will be readable by system administrators on your server. Rather than use sensitive account details, it is suggested you give access to the GA account to a new Google account that you create just for this purpose.
 
 3. Set up this extension's database tables using a paster command. (Ensure your CKAN pyenv is still activated, run the command from ``src/ckanext-ga-report``, alter the ``--config`` option to point to your site config file)::
 

--- a/ckanext/ga_report/download_analytics.py
+++ b/ckanext/ga_report/download_analytics.py
@@ -1,3 +1,4 @@
+import os
 import logging
 import datetime
 
@@ -94,9 +95,6 @@
         '''Get data from GA for a given time period'''
         start_date = start_date.strftime('%Y-%m-%d')
         end_date = end_date.strftime('%Y-%m-%d')
-        # url
-        #query = 'ga:pagePath=~^%s,ga:pagePath=~^%s' % \
-        #        (PACKAGE_URL, self.resource_url_tag)
         query = 'ga:pagePath=~/dataset/[a-z0-9-]+$'
         metrics = 'ga:uniquePageviews'
         sort = '-ga:uniquePageviews'
@@ -110,27 +108,19 @@
                                  metrics=metrics,
                                  sort=sort,
                                  dimensions="ga:pagePath",
+                                 max_results=10000,
                                  end_date=end_date).execute()
 
-        import pprint
-        pprint.pprint(results)
-        print 'Total results: %s' % results.get('totalResults')
+        if os.getenv('DEBUG'):
+            import pprint
+            pprint.pprint(results)
+            print 'Total results: %s' % results.get('totalResults')
 
         packages = []
         for entry in results.get('rows'):
             (loc,size,) = entry
-            packages.append( ('http:/' + loc,size, '',) )
+            packages.append( ('http:/' + loc,size, '',) ) # Temporary hack
         return dict(url=packages)
-
-    def print_results(self, results):
-        import pprint
-        pprint.pprint(results)
-        if results:
-            print 'Profile: %s' % results.get('profileInfo').get('profileName')
-
-            print 'Total Visits: %s' % results.get('rows', [[-1]])[0][0]
-        else:
-            print 'No results found'
 
     def store(self, period_name, period_complete_day, data):
         if 'url' in data:

--- a/ckanext/ga_report/ga_model.py
+++ b/ckanext/ga_report/ga_model.py
@@ -28,9 +28,10 @@
                              default=make_uuid),
                       Column('period_name', types.UnicodeText),
                       Column('period_complete_day', types.Integer),
-                      Column('visits', types.Integer),
+                      Column('metric', types.UnicodeText),
+                      Column('value', types.UnicodeText),
                       Column('url', types.UnicodeText),
-                      Column('next_page', types.UnicodeText),
+                      Column('department_id', types.UnicodeText),
                 )
 mapper(GA_Url, url_table)
 
@@ -71,21 +72,23 @@
         if dataset:
             publisher_groups = dataset.get_groups('publisher')
             if publisher_groups:
-                return publisher_groups[0].id
+                return publisher_groups[0].name
 
 
 def update_url_stats(period_name, period_complete_day, url_data):
-    table = get_table('ga_url')
     for url, views, next_page in url_data:
         url = _normalize_url(url)
         department_id = _get_department_id_of_url(url)
+
         # see if the row for this url & month is in the table already
-        item = model.Session.query(GA_Url).filter(GA_Url.period_name==period_name).filter(GA_Url.url==url).first()
+        item = model.Session.query(GA_Url).\
+            filter(GA_Url.period_name==period_name).\
+            filter(GA_Url.url==url).\
+            filter(GA_Url.metric == 'Total views').first()
         if item:
-            item.period_name = period_complete_day=period_complete_day
-            item.views = views
+            item.period_name = period_complete_day = period_complete_day
+            item.value = views
             item.department_id = department_id
-            item.next_page = next_page
             model.Session.add(item)
         else:
             # create the row
@@ -93,10 +96,10 @@
                       'period_name': period_name,
                       'period_complete_day': period_complete_day,
                       'url': url,
-                      'views': views,
-                      'department_id': department_id,
-                      'next_page': next_page}
-            obj = GA_Url(**values)
-            model.Session.add(obj)
+                      'value': views,
+                      'metric': 'Total views',
+                      'department_id': department_id
+                     }
+            model.Session.add(GA_Url(**values))
         model.Session.commit()
 

--- /dev/null
+++ b/ckanext/ga_report/tests/test_api.py
@@ -1,1 +1,51 @@
+import os
+import datetime
+from nose.tools import assert_equal
+from ckanext.ga_report.download_analytics import DownloadAnalytics
+from ckanext.ga_report.ga_auth import (init_service, get_profile_id)
+from ckanext.ga_report.ga_model import init_tables
 
+class TestAPI:
+
+    @classmethod
+    def setup_class(cls):
+        if not os.path.exists("token.dat") or not os.path.exists("credentials.json"):
+            print '*' * 60
+            print "Tests may not run without first having run the auth process"
+            print '*' * 60
+        init_tables()
+
+    @classmethod
+    def teardown_class(cls):
+        pass
+
+    def test_latest(self):
+        svc = init_service("token.dat", "credentials.json")
+        try:
+            downloader = DownloadAnalytics(svc, profile_id=get_profile_id(svc))
+            downloader.latest()
+        except Exception as e:
+            assert False, e
+
+
+    def test_since(self):
+        svc = init_service("token.dat", "credentials.json")
+        downloader = DownloadAnalytics(svc, profile_id=get_profile_id(svc))
+        try:
+            downloader.since_date(datetime.datetime.now() - datetime.timedelta(days=-30))
+        except Exception as e:
+            assert False, e
+
+"""
+        downloader = DownloadAnalytics(svc, profile_id=get_profile_id(svc))
+
+        time_period = self.args[1] if self.args and len(self.args) > 1 \
+            else 'latest'
+        if time_period == 'all':
+            downloader.all_()
+        elif time_period == 'latest':
+            downloader.latest()
+        else:
+            since_date = datetime.datetime.strptime(time_period, '%Y-%m-%d')
+            downloader.since_date(since_date)
+"""