Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 1 | # Copyright (C) 2010 Google Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | """Client for discovery based APIs |
| 16 | |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame^] | 17 | A client library for Google's discovery based APIs. |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 18 | """ |
| 19 | |
| 20 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
Joe Gregorio | abda96f | 2011-02-11 20:19:33 -0500 | [diff] [blame] | 21 | __all__ = [ |
| 22 | 'build', 'build_from_document' |
| 23 | ] |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 24 | |
| 25 | import httplib2 |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 26 | import logging |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 27 | import os |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 28 | import re |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 29 | import uritemplate |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 30 | import urllib |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 31 | import urlparse |
ade@google.com | c5eb46f | 2010-09-27 23:35:39 +0100 | [diff] [blame] | 32 | try: |
| 33 | from urlparse import parse_qsl |
| 34 | except ImportError: |
| 35 | from cgi import parse_qsl |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 36 | |
Joe Gregorio | b843fa2 | 2010-12-13 16:26:07 -0500 | [diff] [blame] | 37 | from http import HttpRequest |
Joe Gregorio | 034e700 | 2010-12-15 08:45:03 -0500 | [diff] [blame] | 38 | from anyjson import simplejson |
Joe Gregorio | b843fa2 | 2010-12-13 16:26:07 -0500 | [diff] [blame] | 39 | from model import JsonModel |
Joe Gregorio | b843fa2 | 2010-12-13 16:26:07 -0500 | [diff] [blame] | 40 | from errors import UnknownLinkType |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 41 | |
Joe Gregorio | bc2ff9b | 2010-11-08 09:20:48 -0500 | [diff] [blame] | 42 | URITEMPLATE = re.compile('{[^}]*}') |
| 43 | VARNAME = re.compile('[a-zA-Z0-9_-]+') |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 44 | DISCOVERY_URI = ('https://www.googleapis.com/discovery/v0.2beta1/describe/' |
| 45 | '{api}/{apiVersion}') |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 46 | |
| 47 | |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 48 | def key2param(key): |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame^] | 49 | """Converts key names into parameter names. |
| 50 | |
| 51 | For example, converting "max-results" -> "max_results" |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 52 | """ |
| 53 | result = [] |
| 54 | key = list(key) |
| 55 | if not key[0].isalpha(): |
| 56 | result.append('x') |
| 57 | for c in key: |
| 58 | if c.isalnum(): |
| 59 | result.append(c) |
| 60 | else: |
| 61 | result.append('_') |
| 62 | |
| 63 | return ''.join(result) |
| 64 | |
| 65 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 66 | def build(serviceName, version, |
Joe Gregorio | 3fada33 | 2011-01-07 17:07:45 -0500 | [diff] [blame] | 67 | http=None, |
| 68 | discoveryServiceUrl=DISCOVERY_URI, |
| 69 | developerKey=None, |
| 70 | model=JsonModel(), |
| 71 | requestBuilder=HttpRequest): |
Joe Gregorio | abda96f | 2011-02-11 20:19:33 -0500 | [diff] [blame] | 72 | """Construct a Resource for interacting with an API. |
| 73 | |
| 74 | Construct a Resource object for interacting with |
| 75 | an API. The serviceName and version are the |
| 76 | names from the Discovery service. |
| 77 | |
| 78 | Args: |
| 79 | serviceName: string, name of the service |
| 80 | version: string, the version of the service |
| 81 | discoveryServiceUrl: string, a URI Template that points to |
| 82 | the location of the discovery service. It should have two |
| 83 | parameters {api} and {apiVersion} that when filled in |
| 84 | produce an absolute URI to the discovery document for |
| 85 | that service. |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 86 | developerKey: string, key obtained |
| 87 | from https://code.google.com/apis/console |
Joe Gregorio | abda96f | 2011-02-11 20:19:33 -0500 | [diff] [blame] | 88 | model: apiclient.Model, converts to and from the wire format |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 89 | requestBuilder: apiclient.http.HttpRequest, encapsulator for |
| 90 | an HTTP request |
Joe Gregorio | abda96f | 2011-02-11 20:19:33 -0500 | [diff] [blame] | 91 | |
| 92 | Returns: |
| 93 | A Resource object with methods for interacting with |
| 94 | the service. |
| 95 | """ |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 96 | params = { |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 97 | 'api': serviceName, |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 98 | 'apiVersion': version |
| 99 | } |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 100 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 101 | if http is None: |
| 102 | http = httplib2.Http() |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 103 | requested_url = uritemplate.expand(discoveryServiceUrl, params) |
| 104 | logging.info('URL being requested: %s' % requested_url) |
| 105 | resp, content = http.request(requested_url) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 106 | service = simplejson.loads(content) |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 107 | |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame^] | 108 | fn = os.path.join(os.path.dirname(__file__), 'contrib', |
| 109 | serviceName, 'future.json') |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 110 | try: |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame^] | 111 | f = file(fn, 'r') |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 112 | future = f.read() |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 113 | f.close() |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 114 | except IOError: |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 115 | future = None |
| 116 | |
| 117 | return build_from_document(content, discoveryServiceUrl, future, |
| 118 | http, developerKey, model, requestBuilder) |
| 119 | |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 120 | |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 121 | def build_from_document( |
| 122 | service, |
| 123 | base, |
| 124 | future=None, |
| 125 | http=None, |
| 126 | developerKey=None, |
| 127 | model=JsonModel(), |
| 128 | requestBuilder=HttpRequest): |
Joe Gregorio | abda96f | 2011-02-11 20:19:33 -0500 | [diff] [blame] | 129 | """Create a Resource for interacting with an API. |
| 130 | |
| 131 | Same as `build()`, but constructs the Resource object |
| 132 | from a discovery document that is it given, as opposed to |
| 133 | retrieving one over HTTP. |
| 134 | |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 135 | Args: |
| 136 | service: string, discovery document |
| 137 | base: string, base URI for all HTTP requests, usually the discovery URI |
| 138 | future: string, discovery document with future capabilities |
| 139 | auth_discovery: dict, information about the authentication the API supports |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 140 | http: httplib2.Http, An instance of httplib2.Http or something that acts |
| 141 | like it that HTTP requests will be made through. |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 142 | developerKey: string, Key for controlling API usage, generated |
| 143 | from the API Console. |
| 144 | model: Model class instance that serializes and |
| 145 | de-serializes requests and responses. |
| 146 | requestBuilder: Takes an http request and packages it up to be executed. |
Joe Gregorio | abda96f | 2011-02-11 20:19:33 -0500 | [diff] [blame] | 147 | |
| 148 | Returns: |
| 149 | A Resource object with methods for interacting with |
| 150 | the service. |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 151 | """ |
| 152 | |
| 153 | service = simplejson.loads(service) |
| 154 | base = urlparse.urljoin(base, service['restBasePath']) |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 155 | if future: |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 156 | future = simplejson.loads(future) |
| 157 | auth_discovery = future.get('auth', {}) |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 158 | else: |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 159 | future = {} |
| 160 | auth_discovery = {} |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 161 | |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 162 | resource = createResource(http, base, model, requestBuilder, developerKey, |
| 163 | service, future) |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 164 | |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 165 | def auth_method(): |
| 166 | """Discovery information about the authentication the API uses.""" |
| 167 | return auth_discovery |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 168 | |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 169 | setattr(resource, 'auth_discovery', auth_method) |
Joe Gregorio | a2f56e7 | 2010-09-09 15:15:56 -0400 | [diff] [blame] | 170 | |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 171 | return resource |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 172 | |
| 173 | |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 174 | def createResource(http, baseUrl, model, requestBuilder, |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 175 | developerKey, resourceDesc, futureDesc): |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 176 | |
| 177 | class Resource(object): |
| 178 | """A class for interacting with a resource.""" |
| 179 | |
| 180 | def __init__(self): |
| 181 | self._http = http |
| 182 | self._baseUrl = baseUrl |
| 183 | self._model = model |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 184 | self._developerKey = developerKey |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 185 | self._requestBuilder = requestBuilder |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 186 | |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 187 | def createMethod(theclass, methodName, methodDesc, futureDesc): |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 188 | pathUrl = methodDesc['restPath'] |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 189 | pathUrl = re.sub(r'\{', r'{+', pathUrl) |
| 190 | httpMethod = methodDesc['httpMethod'] |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 191 | methodId = methodDesc['rpcMethod'] |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 192 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 193 | argmap = {} |
| 194 | if httpMethod in ['PUT', 'POST']: |
| 195 | argmap['body'] = 'body' |
| 196 | |
| 197 | |
| 198 | required_params = [] # Required parameters |
| 199 | pattern_params = {} # Parameters that must match a regex |
| 200 | query_params = [] # Parameters that will be used in the query string |
| 201 | path_params = {} # Parameters that will be used in the base URL |
Joe Gregorio | 4292c6e | 2010-09-09 14:32:43 -0400 | [diff] [blame] | 202 | if 'parameters' in methodDesc: |
| 203 | for arg, desc in methodDesc['parameters'].iteritems(): |
| 204 | param = key2param(arg) |
| 205 | argmap[param] = arg |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 206 | |
Joe Gregorio | 4292c6e | 2010-09-09 14:32:43 -0400 | [diff] [blame] | 207 | if desc.get('pattern', ''): |
| 208 | pattern_params[param] = desc['pattern'] |
| 209 | if desc.get('required', False): |
| 210 | required_params.append(param) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 211 | if desc.get('restParameterType') == 'query': |
Joe Gregorio | 4292c6e | 2010-09-09 14:32:43 -0400 | [diff] [blame] | 212 | query_params.append(param) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 213 | if desc.get('restParameterType') == 'path': |
Joe Gregorio | 4292c6e | 2010-09-09 14:32:43 -0400 | [diff] [blame] | 214 | path_params[param] = param |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 215 | |
Joe Gregorio | bc2ff9b | 2010-11-08 09:20:48 -0500 | [diff] [blame] | 216 | for match in URITEMPLATE.finditer(pathUrl): |
| 217 | for namematch in VARNAME.finditer(match.group(0)): |
| 218 | name = key2param(namematch.group(0)) |
| 219 | path_params[name] = name |
| 220 | if name in query_params: |
| 221 | query_params.remove(name) |
| 222 | |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 223 | def method(self, **kwargs): |
| 224 | for name in kwargs.iterkeys(): |
| 225 | if name not in argmap: |
| 226 | raise TypeError('Got an unexpected keyword argument "%s"' % name) |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 227 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 228 | for name in required_params: |
Joe Gregorio | fbf9d0d | 2010-08-18 16:50:47 -0400 | [diff] [blame] | 229 | if name not in kwargs: |
| 230 | raise TypeError('Missing required parameter "%s"' % name) |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 231 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 232 | for name, regex in pattern_params.iteritems(): |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 233 | if name in kwargs: |
| 234 | if re.match(regex, kwargs[name]) is None: |
Joe Gregorio | 3bbbf66 | 2010-08-30 16:41:53 -0400 | [diff] [blame] | 235 | raise TypeError( |
| 236 | 'Parameter "%s" value "%s" does not match the pattern "%s"' % |
| 237 | (name, kwargs[name], regex)) |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 238 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 239 | actual_query_params = {} |
| 240 | actual_path_params = {} |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 241 | for key, value in kwargs.iteritems(): |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 242 | if key in query_params: |
| 243 | actual_query_params[argmap[key]] = value |
| 244 | if key in path_params: |
| 245 | actual_path_params[argmap[key]] = value |
| 246 | body_value = kwargs.get('body', None) |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 247 | |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 248 | if self._developerKey: |
| 249 | actual_query_params['key'] = self._developerKey |
| 250 | |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 251 | headers = {} |
Joe Gregorio | 3bbbf66 | 2010-08-30 16:41:53 -0400 | [diff] [blame] | 252 | headers, params, query, body = self._model.request(headers, |
| 253 | actual_path_params, actual_query_params, body_value) |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 254 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 255 | # TODO(ade) This exists to fix a bug in V1 of the Buzz discovery |
| 256 | # document. Base URLs should not contain any path elements. If they do |
| 257 | # then urlparse.urljoin will strip them out This results in an incorrect |
| 258 | # URL which returns a 404 |
ade@google.com | 7ebb2ca | 2010-09-29 16:42:15 +0100 | [diff] [blame] | 259 | url_result = urlparse.urlsplit(self._baseUrl) |
| 260 | new_base_url = url_result.scheme + '://' + url_result.netloc |
| 261 | |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 262 | expanded_url = uritemplate.expand(pathUrl, params) |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 263 | url = urlparse.urljoin(new_base_url, |
| 264 | url_result.path + expanded_url + query) |
Joe Gregorio | fbf9d0d | 2010-08-18 16:50:47 -0400 | [diff] [blame] | 265 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 266 | logging.info('URL being requested: %s' % url) |
Joe Gregorio | abda96f | 2011-02-11 20:19:33 -0500 | [diff] [blame] | 267 | return self._requestBuilder(self._http, |
| 268 | self._model.response, |
| 269 | url, |
| 270 | method=httpMethod, |
| 271 | body=body, |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 272 | headers=headers, |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 273 | methodId=methodId) |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 274 | |
| 275 | docs = ['A description of how to use this function\n\n'] |
| 276 | for arg in argmap.iterkeys(): |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame^] | 277 | required = '' |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 278 | if arg in required_params: |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame^] | 279 | required = ' (required)' |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 280 | docs.append('%s - A parameter%s\n' % (arg, required)) |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 281 | |
| 282 | setattr(method, '__doc__', ''.join(docs)) |
| 283 | setattr(theclass, methodName, method) |
| 284 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 285 | def createNextMethod(theclass, methodName, methodDesc, futureDesc): |
| 286 | methodId = methodDesc['rpcMethod'] + '.next' |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 287 | |
| 288 | def method(self, previous): |
| 289 | """ |
| 290 | Takes a single argument, 'body', which is the results |
| 291 | from the last call, and returns the next set of items |
| 292 | in the collection. |
| 293 | |
| 294 | Returns None if there are no more items in |
| 295 | the collection. |
| 296 | """ |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 297 | if futureDesc['type'] != 'uri': |
| 298 | raise UnknownLinkType(futureDesc['type']) |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 299 | |
| 300 | try: |
| 301 | p = previous |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 302 | for key in futureDesc['location']: |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 303 | p = p[key] |
| 304 | url = p |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 305 | except (KeyError, TypeError): |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 306 | return None |
| 307 | |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 308 | if self._developerKey: |
| 309 | parsed = list(urlparse.urlparse(url)) |
ade@google.com | c5eb46f | 2010-09-27 23:35:39 +0100 | [diff] [blame] | 310 | q = parse_qsl(parsed[4]) |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 311 | q.append(('key', self._developerKey)) |
| 312 | parsed[4] = urllib.urlencode(q) |
| 313 | url = urlparse.urlunparse(parsed) |
| 314 | |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 315 | headers = {} |
| 316 | headers, params, query, body = self._model.request(headers, {}, {}, None) |
| 317 | |
| 318 | logging.info('URL being requested: %s' % url) |
| 319 | resp, content = self._http.request(url, method='GET', headers=headers) |
| 320 | |
Joe Gregorio | abda96f | 2011-02-11 20:19:33 -0500 | [diff] [blame] | 321 | return self._requestBuilder(self._http, |
| 322 | self._model.response, |
| 323 | url, |
| 324 | method='GET', |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 325 | headers=headers, |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 326 | methodId=methodId) |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 327 | |
| 328 | setattr(theclass, methodName, method) |
| 329 | |
| 330 | # Add basic methods to Resource |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 331 | if 'methods' in resourceDesc: |
| 332 | for methodName, methodDesc in resourceDesc['methods'].iteritems(): |
| 333 | if futureDesc: |
| 334 | future = futureDesc['methods'].get(methodName, {}) |
| 335 | else: |
| 336 | future = None |
| 337 | createMethod(Resource, methodName, methodDesc, future) |
| 338 | |
| 339 | # Add in nested resources |
| 340 | if 'resources' in resourceDesc: |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 341 | |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 342 | def createMethod(theclass, methodName, methodDesc, futureDesc): |
| 343 | |
| 344 | def method(self): |
| 345 | return createResource(self._http, self._baseUrl, self._model, |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 346 | self._requestBuilder, self._developerKey, |
| 347 | methodDesc, futureDesc) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 348 | |
| 349 | setattr(method, '__doc__', 'A description of how to use this function') |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 350 | setattr(method, '__is_resource__', True) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 351 | setattr(theclass, methodName, method) |
| 352 | |
| 353 | for methodName, methodDesc in resourceDesc['resources'].iteritems(): |
| 354 | if futureDesc and 'resources' in futureDesc: |
| 355 | future = futureDesc['resources'].get(methodName, {}) |
| 356 | else: |
| 357 | future = {} |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 358 | createMethod(Resource, methodName, methodDesc, |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 359 | future) |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 360 | |
| 361 | # Add <m>_next() methods to Resource |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 362 | if futureDesc and 'methods' in futureDesc: |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 363 | for methodName, methodDesc in futureDesc['methods'].iteritems(): |
| 364 | if 'next' in methodDesc and methodName in resourceDesc['methods']: |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame^] | 365 | createNextMethod(Resource, methodName + '_next', |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 366 | resourceDesc['methods'][methodName], |
| 367 | methodDesc['next']) |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 368 | |
| 369 | return Resource() |