/gdata/health/service.py

http://radioappz.googlecode.com/ · Python · 263 lines · 192 code · 11 blank · 60 comment · 2 complexity · 3180466087854fe3126dc609b0808c38 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. """HealthService extends GDataService to streamline Google Health API access.
  17. HealthService: Provides methods to interact with the profile, profile list,
  18. and register/notices feeds. Extends GDataService.
  19. HealthProfileQuery: Queries the Google Health Profile feed.
  20. HealthProfileListQuery: Queries the Google Health Profile list feed.
  21. """
  22. __author__ = 'api.eric@google.com (Eric Bidelman)'
  23. import atom
  24. import gdata.health
  25. import gdata.service
  26. class HealthService(gdata.service.GDataService):
  27. """Client extension for the Google Health service Document List feed."""
  28. def __init__(self, email=None, password=None, source=None,
  29. use_h9_sandbox=False, server='www.google.com',
  30. additional_headers=None, **kwargs):
  31. """Creates a client for the Google Health service.
  32. Args:
  33. email: string (optional) The user's email address, used for
  34. authentication.
  35. password: string (optional) The user's password.
  36. source: string (optional) The name of the user's application.
  37. use_h9_sandbox: boolean (optional) True to issue requests against the
  38. /h9 developer's sandbox.
  39. server: string (optional) The name of the server to which a connection
  40. will be opened.
  41. additional_headers: dictionary (optional) Any additional headers which
  42. should be included with CRUD operations.
  43. kwargs: The other parameters to pass to gdata.service.GDataService
  44. constructor.
  45. """
  46. service = use_h9_sandbox and 'weaver' or 'health'
  47. gdata.service.GDataService.__init__(
  48. self, email=email, password=password, service=service, source=source,
  49. server=server, additional_headers=additional_headers, **kwargs)
  50. self.ssl = True
  51. self.use_h9_sandbox = use_h9_sandbox
  52. def __get_service(self):
  53. return self.use_h9_sandbox and 'h9' or 'health'
  54. def GetProfileFeed(self, query=None, profile_id=None):
  55. """Fetches the users Google Health profile feed.
  56. Args:
  57. query: HealthProfileQuery or string (optional) A query to use on the
  58. profile feed. If None, a HealthProfileQuery is constructed.
  59. profile_id: string (optional) The profile id to query the profile feed
  60. with when using ClientLogin. Note: this parameter is ignored if
  61. query is set.
  62. Returns:
  63. A gdata.health.ProfileFeed object containing the user's Health profile.
  64. """
  65. if query is None:
  66. projection = profile_id and 'ui' or 'default'
  67. uri = HealthProfileQuery(
  68. service=self.__get_service(), projection=projection,
  69. profile_id=profile_id).ToUri()
  70. elif isinstance(query, HealthProfileQuery):
  71. uri = query.ToUri()
  72. else:
  73. uri = query
  74. return self.GetFeed(uri, converter=gdata.health.ProfileFeedFromString)
  75. def GetProfileListFeed(self, query=None):
  76. """Fetches the users Google Health profile feed.
  77. Args:
  78. query: HealthProfileListQuery or string (optional) A query to use
  79. on the profile list feed. If None, a HealthProfileListQuery is
  80. constructed to /health/feeds/profile/list or /h9/feeds/profile/list.
  81. Returns:
  82. A gdata.health.ProfileListFeed object containing the user's list
  83. of profiles.
  84. """
  85. if not query:
  86. uri = HealthProfileListQuery(service=self.__get_service()).ToUri()
  87. elif isinstance(query, HealthProfileListQuery):
  88. uri = query.ToUri()
  89. else:
  90. uri = query
  91. return self.GetFeed(uri, converter=gdata.health.ProfileListFeedFromString)
  92. def SendNotice(self, subject, body=None, content_type='html',
  93. ccr=None, profile_id=None):
  94. """Sends (posts) a notice to the user's Google Health profile.
  95. Args:
  96. subject: A string representing the message's subject line.
  97. body: string (optional) The message body.
  98. content_type: string (optional) The content type of the notice message
  99. body. This parameter is only honored when a message body is
  100. specified.
  101. ccr: string (optional) The CCR XML document to reconcile into the
  102. user's profile.
  103. profile_id: string (optional) The profile id to work with when using
  104. ClientLogin. Note: this parameter is ignored if query is set.
  105. Returns:
  106. A gdata.health.ProfileEntry object of the posted entry.
  107. """
  108. if body:
  109. content = atom.Content(content_type=content_type, text=body)
  110. else:
  111. content = body
  112. entry = gdata.GDataEntry(
  113. title=atom.Title(text=subject), content=content,
  114. extension_elements=[atom.ExtensionElementFromString(ccr)])
  115. projection = profile_id and 'ui' or 'default'
  116. query = HealthRegisterQuery(service=self.__get_service(),
  117. projection=projection, profile_id=profile_id)
  118. return self.Post(entry, query.ToUri(),
  119. converter=gdata.health.ProfileEntryFromString)
  120. class HealthProfileQuery(gdata.service.Query):
  121. """Object used to construct a URI to query the Google Health profile feed."""
  122. def __init__(self, service='health', feed='feeds/profile',
  123. projection='default', profile_id=None, text_query=None,
  124. params=None, categories=None):
  125. """Constructor for Health profile feed query.
  126. Args:
  127. service: string (optional) The service to query. Either 'health' or 'h9'.
  128. feed: string (optional) The path for the feed. The default value is
  129. 'feeds/profile'.
  130. projection: string (optional) The visibility of the data. Possible values
  131. are 'default' for AuthSub and 'ui' for ClientLogin. If this value
  132. is set to 'ui', the profile_id parameter should also be set.
  133. profile_id: string (optional) The profile id to query. This should only
  134. be used when using ClientLogin.
  135. text_query: str (optional) The contents of the q query parameter. The
  136. contents of the text_query are URL escaped upon conversion to a URI.
  137. Note: this parameter can only be used on the register feed using
  138. ClientLogin.
  139. params: dict (optional) Parameter value string pairs which become URL
  140. params when translated to a URI. These parameters are added to
  141. the query's items.
  142. categories: list (optional) List of category strings which should be
  143. included as query categories. See gdata.service.Query for
  144. additional documentation.
  145. """
  146. self.service = service
  147. self.profile_id = profile_id
  148. self.projection = projection
  149. gdata.service.Query.__init__(self, feed=feed, text_query=text_query,
  150. params=params, categories=categories)
  151. def ToUri(self):
  152. """Generates a URI from the query parameters set in the object.
  153. Returns:
  154. A string containing the URI used to retrieve entries from the Health
  155. profile feed.
  156. """
  157. old_feed = self.feed
  158. self.feed = '/'.join([self.service, old_feed, self.projection])
  159. if self.profile_id:
  160. self.feed += '/' + self.profile_id
  161. self.feed = '/%s' % (self.feed,)
  162. new_feed = gdata.service.Query.ToUri(self)
  163. self.feed = old_feed
  164. return new_feed
  165. class HealthProfileListQuery(gdata.service.Query):
  166. """Object used to construct a URI to query a Health profile list feed."""
  167. def __init__(self, service='health', feed='feeds/profile/list'):
  168. """Constructor for Health profile list feed query.
  169. Args:
  170. service: string (optional) The service to query. Either 'health' or 'h9'.
  171. feed: string (optional) The path for the feed. The default value is
  172. 'feeds/profile/list'.
  173. """
  174. gdata.service.Query.__init__(self, feed)
  175. self.service = service
  176. def ToUri(self):
  177. """Generates a URI from the query parameters set in the object.
  178. Returns:
  179. A string containing the URI used to retrieve entries from the
  180. profile list feed.
  181. """
  182. return '/%s' % ('/'.join([self.service, self.feed]),)
  183. class HealthRegisterQuery(gdata.service.Query):
  184. """Object used to construct a URI to query a Health register/notice feed."""
  185. def __init__(self, service='health', feed='feeds/register',
  186. projection='default', profile_id=None):
  187. """Constructor for Health profile list feed query.
  188. Args:
  189. service: string (optional) The service to query. Either 'health' or 'h9'.
  190. feed: string (optional) The path for the feed. The default value is
  191. 'feeds/register'.
  192. projection: string (optional) The visibility of the data. Possible values
  193. are 'default' for AuthSub and 'ui' for ClientLogin. If this value
  194. is set to 'ui', the profile_id parameter should also be set.
  195. profile_id: string (optional) The profile id to query. This should only
  196. be used when using ClientLogin.
  197. """
  198. gdata.service.Query.__init__(self, feed)
  199. self.service = service
  200. self.projection = projection
  201. self.profile_id = profile_id
  202. def ToUri(self):
  203. """Generates a URI from the query parameters set in the object.
  204. Returns:
  205. A string containing the URI needed to interact with the register feed.
  206. """
  207. old_feed = self.feed
  208. self.feed = '/'.join([self.service, old_feed, self.projection])
  209. new_feed = gdata.service.Query.ToUri(self)
  210. self.feed = old_feed
  211. if self.profile_id:
  212. new_feed += '/' + self.profile_id
  213. return '/%s' % (new_feed,)