/gdata/analytics/client.py
Python | 130 lines | 95 code | 8 blank | 27 comment | 0 complexity | 591fa012f3690e338bf56a4287b80add MD5 | raw file
1#!/usr/bin/python 2# 3# Copyright 2009 Google Inc. All Rights Reserved. 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17"""AnalyticsClient extends gdata.client.GDClient to streamline 18Google Analytics Data Export API calls.""" 19 20__author__ = 'api.nickm@google.com (Nick Mihailovski)' 21 22 23import atom.data 24import gdata.client 25import gdata.analytics.data 26import gdata.gauth 27 28 29class AnalyticsClient(gdata.client.GDClient): 30 """Client extension for the Google Analytics API service.""" 31 32 api_version = '2' 33 auth_service = 'analytics' 34 auth_scopes = gdata.gauth.AUTH_SCOPES['analytics'] 35 account_type = 'GOOGLE' 36 37 def __init__(self, auth_token=None, **kwargs): 38 """Constructs a new client for the Google Analytics Data Export API. 39 40 Args: 41 auth_token: gdata.gauth.ClientLoginToken, AuthSubToken, or 42 OAuthToken (optional) Authorizes this client to edit the user's data. 43 kwargs: The other parameters to pass to gdata.client.GDClient 44 constructor. 45 """ 46 47 gdata.client.GDClient.__init__(self, auth_token=auth_token, **kwargs) 48 49 def get_account_feed(self, feed_uri, auth_token=None, **kwargs): 50 """Makes a request to the Analytics API Account Feed. 51 52 Args: 53 feed_uri: str or gdata.analytics.AccountFeedQuery The Analytics Account 54 Feed uri to define what data to retrieve from the API. Can also be 55 used with a gdata.analytics.AccountFeedQuery object. 56 """ 57 58 return self.get_feed(feed_uri, 59 desired_class=gdata.analytics.data.AccountFeed, 60 auth_token=auth_token, 61 **kwargs) 62 63 GetAccountFeed = get_account_feed 64 65 def get_data_feed(self, feed_uri, auth_token=None, **kwargs): 66 """Makes a request to the Analytics API Data Feed. 67 68 Args: 69 feed_uri: str or gdata.analytics.AccountFeedQuery The Analytics Data 70 Feed uri to define what data to retrieve from the API. Can also be 71 used with a gdata.analytics.AccountFeedQuery object. 72 """ 73 74 return self.get_feed(feed_uri, 75 desired_class=gdata.analytics.data.DataFeed, 76 auth_token=auth_token, 77 **kwargs) 78 79 GetDataFeed = get_data_feed 80 81 82class AccountFeedQuery(gdata.client.GDQuery): 83 """Account Feed query class to simplify constructing Account Feed Urls. 84 85 To use this class, you can either pass a dict in the constructor that has 86 all the data feed query parameters as keys. 87 queryUrl = DataFeedQuery({'max-results': '10000'}) 88 89 Alternatively you can add new parameters directly to the query object. 90 queryUrl = DataFeedQuery() 91 queryUrl.query['max-results'] = '10000' 92 93 Args: 94 query: dict (optional) Contains all the GA Data Feed query parameters 95 as keys. 96 """ 97 98 scheme = 'https' 99 host = 'www.google.com' 100 path = '/analytics/feeds/accounts/default' 101 102 def __init__(self, query=None, **kwargs): 103 self.query = query or {} 104 gdata.client.GDQuery(self, **kwargs) 105 106 107class DataFeedQuery(gdata.client.GDQuery): 108 """Data Feed query class to simplify constructing Data Feed Urls. 109 110 To use this class, you can either pass a dict in the constructor that has 111 all the data feed query parameters as keys. 112 queryUrl = DataFeedQuery({'start-date': '2008-10-01'}) 113 114 Alternatively you can add new parameters directly to the query object. 115 queryUrl = DataFeedQuery() 116 queryUrl.query['start-date'] = '2008-10-01' 117 118 Args: 119 query: dict (optional) Contains all the GA Data Feed query parameters 120 as keys. 121 """ 122 123 scheme = 'https' 124 host = 'www.google.com' 125 path = '/analytics/feeds/data' 126 127 def __init__(self, query=None, **kwargs): 128 self.query = query or {} 129 gdata.client.GDQuery(self, **kwargs) 130