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