/gdata/calendar_resource/client.py

http://radioappz.googlecode.com/ · Python · 186 lines · 133 code · 13 blank · 40 comment · 5 complexity · c416a19d9d6de3e339c36e7bc9667ce4 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. """CalendarResourceClient simplifies Calendar Resources API calls.
  17. CalendarResourceClient extends gdata.client.GDClient to ease interaction with
  18. the Google Apps Calendar Resources API. These interactions include the ability
  19. to create, retrieve, update, and delete calendar resources in a Google Apps
  20. domain.
  21. """
  22. __author__ = 'Vic Fryzel <vf@google.com>'
  23. import urllib
  24. import atom.data
  25. import gdata.client
  26. import gdata.calendar_resource.data
  27. # Feed URI template. This must end with a /
  28. RESOURCE_FEED_TEMPLATE = '/a/feeds/calendar/resource/%s/%s/'
  29. class CalendarResourceClient(gdata.client.GDClient):
  30. """Client extension for the Google Calendar Resource API service.
  31. Attributes:
  32. host: string The hostname for the Calendar Resouce API service.
  33. api_version: string The version of the Calendar Resource API.
  34. """
  35. host = 'apps-apis.google.com'
  36. api_version = '2.0'
  37. auth_service = 'apps'
  38. auth_scopes = gdata.gauth.AUTH_SCOPES['apps']
  39. def __init__(self, domain, auth_token=None, **kwargs):
  40. """Constructs a new client for the Calendar Resource API.
  41. Args:
  42. domain: string The Google Apps domain with Calendar Resources.
  43. auth_token: (optional) gdata.gauth.ClientLoginToken, AuthSubToken, or
  44. OAuthToken which authorizes this client to edit the calendar resource
  45. data.
  46. kwargs: The other parameters to pass to gdata.client.GDClient constructor.
  47. """
  48. gdata.client.GDClient.__init__(self, auth_token=auth_token, **kwargs)
  49. self.domain = domain
  50. def make_resource_feed_uri(self, resource_id=None, params=None):
  51. """Creates a resource feed URI for the Calendar Resource API.
  52. Using this client's Google Apps domain, create a feed URI for calendar
  53. resources in that domain. If a resource_id is provided, return a URI
  54. for that specific resource. If params are provided, append them as GET
  55. params.
  56. Args:
  57. resource_id: string (optional) The ID of the calendar resource for which
  58. to make a feed URI.
  59. params: dict (optional) key -> value params to append as GET vars to the
  60. URI. Example: params={'start': 'my-resource-id'}
  61. Returns:
  62. A string giving the URI for calendar resources for this client's Google
  63. Apps domain.
  64. """
  65. uri = RESOURCE_FEED_TEMPLATE % (self.api_version, self.domain)
  66. if resource_id:
  67. uri += resource_id
  68. if params:
  69. uri += '?' + urllib.urlencode(params)
  70. return uri
  71. MakeResourceFeedUri = make_resource_feed_uri
  72. def get_resource_feed(self, uri=None, **kwargs):
  73. """Fetches a ResourceFeed of calendar resources at the given URI.
  74. Args:
  75. uri: string The URI of the feed to pull.
  76. Returns:
  77. A ResourceFeed object representing the feed at the given URI.
  78. """
  79. if uri is None:
  80. uri = self.MakeResourceFeedUri()
  81. return self.get_feed(uri,
  82. desired_class=gdata.calendar_resource.data.CalendarResourceFeed,
  83. **kwargs)
  84. GetResourceFeed = get_resource_feed
  85. def get_resource(self, uri=None, resource_id=None, **kwargs):
  86. """Fetches a single calendar resource by resource ID.
  87. Args:
  88. uri: string The base URI of the feed from which to fetch the resource.
  89. resource_id: string The string ID of the Resource to fetch.
  90. Returns:
  91. A Resource object representing the calendar resource with the given
  92. base URI and resource ID.
  93. """
  94. if uri is None:
  95. uri = self.MakeResourceUri(resource_id)
  96. return self.get_entry(uri,
  97. desired_class=gdata.calendar_resource.data.CalendarResourceEntry,
  98. **kwargs)
  99. GetResource = get_resource
  100. def create_resource(self, resource_id, resource_common_name=None,
  101. resource_description=None, resource_type=None, **kwargs):
  102. """Creates a calendar resource with the given properties.
  103. Args:
  104. resource_id: string The resource ID of the calendar resource.
  105. resource_common_name: string (optional) The common name of the resource.
  106. resource_description: string (optional) The description of the resource.
  107. resource_type: string (optional) The type of the resource.
  108. Returns:
  109. gdata.calendar_resource.data.CalendarResourceEntry of the new resource.
  110. """
  111. new_resource = gdata.calendar_resource.data.CalendarResourceEntry(
  112. resource_id=resource_id, resource_common_name=resource_common_name,
  113. resource_description=resource_description, resource_type=resource_type)
  114. return self.post(new_resource, self.MakeResourceFeedUri(), **kwargs)
  115. CreateResource = create_resource
  116. def update_resource(self, resource_id, resource_common_name=None,
  117. resource_description=None, resource_type=None, **kwargs):
  118. """Updates the calendar resource with the given resource ID.
  119. Args:
  120. resource_id: string The resource ID of the calendar resource to update.
  121. resource_common_name: string (optional) The common name to give the
  122. resource.
  123. resource_description: string (optional) The description to give the
  124. resource.
  125. resource_type: string (optional) The type to give the resource.
  126. Returns:
  127. gdata.calendar_resource.data.CalendarResourceEntry of the updated resource.
  128. """
  129. new_resource = gdata.calendar_resource.data.CalendarResourceEntry(
  130. resource_id=resource_id, resource_common_name=resource_common_name,
  131. resource_description=resource_description, resource_type=resource_type)
  132. return self.update(new_resource,
  133. self.MakeResourceFeedUri(resource_id), **kwargs)
  134. UpdateResource = update_resource
  135. def delete_resource(self, resource_id, **kwargs):
  136. """Deletes the calendar resource with the given resource ID.
  137. Args:
  138. resource_id: string The resource ID of the calendar resource to delete.
  139. kwargs: Other parameters to pass to gdata.client.delete()
  140. Returns:
  141. An HTTP response object. See gdata.client.request().
  142. """
  143. return self.delete(self.MakeResourceFeedUri(resource_id),
  144. **kwargs)
  145. DeleteResource = delete_resource