/gdata/analytics/client.py

http://radioappz.googlecode.com/ · Python · 130 lines · 67 code · 13 blank · 50 comment · 4 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. """AnalyticsClient extends gdata.client.GDClient to streamline
  17. Google Analytics Data Export API calls."""
  18. __author__ = 'api.nickm@google.com (Nick Mihailovski)'
  19. import atom.data
  20. import gdata.client
  21. import gdata.analytics.data
  22. import gdata.gauth
  23. class AnalyticsClient(gdata.client.GDClient):
  24. """Client extension for the Google Analytics API service."""
  25. api_version = '2'
  26. auth_service = 'analytics'
  27. auth_scopes = gdata.gauth.AUTH_SCOPES['analytics']
  28. account_type = 'GOOGLE'
  29. def __init__(self, auth_token=None, **kwargs):
  30. """Constructs a new client for the Google Analytics Data Export API.
  31. Args:
  32. auth_token: gdata.gauth.ClientLoginToken, AuthSubToken, or
  33. OAuthToken (optional) Authorizes this client to edit the user's data.
  34. kwargs: The other parameters to pass to gdata.client.GDClient
  35. constructor.
  36. """
  37. gdata.client.GDClient.__init__(self, auth_token=auth_token, **kwargs)
  38. def get_account_feed(self, feed_uri, auth_token=None, **kwargs):
  39. """Makes a request to the Analytics API Account Feed.
  40. Args:
  41. feed_uri: str or gdata.analytics.AccountFeedQuery The Analytics Account
  42. Feed uri to define what data to retrieve from the API. Can also be
  43. used with a gdata.analytics.AccountFeedQuery object.
  44. """
  45. return self.get_feed(feed_uri,
  46. desired_class=gdata.analytics.data.AccountFeed,
  47. auth_token=auth_token,
  48. **kwargs)
  49. GetAccountFeed = get_account_feed
  50. def get_data_feed(self, feed_uri, auth_token=None, **kwargs):
  51. """Makes a request to the Analytics API Data Feed.
  52. Args:
  53. feed_uri: str or gdata.analytics.AccountFeedQuery The Analytics Data
  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. return self.get_feed(feed_uri,
  58. desired_class=gdata.analytics.data.DataFeed,
  59. auth_token=auth_token,
  60. **kwargs)
  61. GetDataFeed = get_data_feed
  62. class AccountFeedQuery(gdata.client.GDQuery):
  63. """Account Feed query class to simplify constructing Account Feed Urls.
  64. To use this class, you can either pass a dict in the constructor that has
  65. all the data feed query parameters as keys.
  66. queryUrl = DataFeedQuery({'max-results': '10000'})
  67. Alternatively you can add new parameters directly to the query object.
  68. queryUrl = DataFeedQuery()
  69. queryUrl.query['max-results'] = '10000'
  70. Args:
  71. query: dict (optional) Contains all the GA Data Feed query parameters
  72. as keys.
  73. """
  74. scheme = 'https'
  75. host = 'www.google.com'
  76. path = '/analytics/feeds/accounts/default'
  77. def __init__(self, query=None, **kwargs):
  78. self.query = query or {}
  79. gdata.client.GDQuery(self, **kwargs)
  80. class DataFeedQuery(gdata.client.GDQuery):
  81. """Data Feed query class to simplify constructing Data Feed Urls.
  82. To use this class, you can either pass a dict in the constructor that has
  83. all the data feed query parameters as keys.
  84. queryUrl = DataFeedQuery({'start-date': '2008-10-01'})
  85. Alternatively you can add new parameters directly to the query object.
  86. queryUrl = DataFeedQuery()
  87. queryUrl.query['start-date'] = '2008-10-01'
  88. Args:
  89. query: dict (optional) Contains all the GA Data Feed query parameters
  90. as keys.
  91. """
  92. scheme = 'https'
  93. host = 'www.google.com'
  94. path = '/analytics/feeds/data'
  95. def __init__(self, query=None, **kwargs):
  96. self.query = query or {}
  97. gdata.client.GDQuery(self, **kwargs)