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 | |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame^] | 25 | import copy |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 26 | import httplib2 |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 27 | import logging |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 28 | import os |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 29 | import re |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 30 | import uritemplate |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 31 | import urllib |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 32 | import urlparse |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 33 | import mimeparse |
Joe Gregorio | 922b78c | 2011-05-26 21:36:34 -0400 | [diff] [blame] | 34 | import mimetypes |
| 35 | |
ade@google.com | c5eb46f | 2010-09-27 23:35:39 +0100 | [diff] [blame] | 36 | try: |
| 37 | from urlparse import parse_qsl |
| 38 | except ImportError: |
| 39 | from cgi import parse_qsl |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 40 | |
Joe Gregorio | 034e700 | 2010-12-15 08:45:03 -0500 | [diff] [blame] | 41 | from anyjson import simplejson |
Joe Gregorio | 922b78c | 2011-05-26 21:36:34 -0400 | [diff] [blame] | 42 | from email.mime.multipart import MIMEMultipart |
| 43 | from email.mime.nonmultipart import MIMENonMultipart |
Joe Gregorio | c0e0fe9 | 2011-03-04 16:16:55 -0500 | [diff] [blame] | 44 | from errors import HttpError |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 45 | from errors import InvalidJsonError |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 46 | from errors import MediaUploadSizeError |
| 47 | from errors import UnacceptableMimeTypeError |
Joe Gregorio | 922b78c | 2011-05-26 21:36:34 -0400 | [diff] [blame] | 48 | from errors import UnknownLinkType |
| 49 | from http import HttpRequest |
| 50 | from model import JsonModel |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 51 | |
Joe Gregorio | bc2ff9b | 2010-11-08 09:20:48 -0500 | [diff] [blame] | 52 | URITEMPLATE = re.compile('{[^}]*}') |
| 53 | VARNAME = re.compile('[a-zA-Z0-9_-]+') |
Joe Gregorio | 6a63a76 | 2011-05-02 22:36:05 -0400 | [diff] [blame] | 54 | DISCOVERY_URI = ('https://www.googleapis.com/discovery/v1/apis/' |
| 55 | '{api}/{apiVersion}/rest') |
Joe Gregorio | c3fae8a | 2011-02-18 14:19:50 -0500 | [diff] [blame] | 56 | DEFAULT_METHOD_DOC = 'A description of how to use this function' |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 57 | |
| 58 | # Query parameters that work, but don't appear in discovery |
Joe Gregorio | 06d852b | 2011-03-25 15:03:10 -0400 | [diff] [blame] | 59 | STACK_QUERY_PARAMETERS = ['trace', 'fields', 'pp', 'prettyPrint', 'userIp', |
Joe Gregorio | 3eecaa9 | 2011-05-17 13:40:12 -0400 | [diff] [blame] | 60 | 'userip', 'strict'] |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 61 | |
Joe Gregorio | d92897c | 2011-07-07 11:44:56 -0400 | [diff] [blame] | 62 | RESERVED_WORDS = [ 'and', 'assert', 'break', 'class', 'continue', 'def', 'del', |
| 63 | 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', |
| 64 | 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', |
| 65 | 'pass', 'print', 'raise', 'return', 'try', 'while' ] |
| 66 | |
| 67 | def _fix_method_name(name): |
| 68 | if name in RESERVED_WORDS: |
| 69 | return name + '_' |
| 70 | else: |
| 71 | return name |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 72 | |
Joe Gregorio | 922b78c | 2011-05-26 21:36:34 -0400 | [diff] [blame] | 73 | def _write_headers(self): |
| 74 | # Utility no-op method for multipart media handling |
| 75 | pass |
| 76 | |
| 77 | |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 78 | def key2param(key): |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 79 | """Converts key names into parameter names. |
| 80 | |
| 81 | For example, converting "max-results" -> "max_results" |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 82 | """ |
| 83 | result = [] |
| 84 | key = list(key) |
| 85 | if not key[0].isalpha(): |
| 86 | result.append('x') |
| 87 | for c in key: |
| 88 | if c.isalnum(): |
| 89 | result.append(c) |
| 90 | else: |
| 91 | result.append('_') |
| 92 | |
| 93 | return ''.join(result) |
| 94 | |
| 95 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 96 | def build(serviceName, version, |
Joe Gregorio | 3fada33 | 2011-01-07 17:07:45 -0500 | [diff] [blame] | 97 | http=None, |
| 98 | discoveryServiceUrl=DISCOVERY_URI, |
| 99 | developerKey=None, |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 100 | model=None, |
Joe Gregorio | 3fada33 | 2011-01-07 17:07:45 -0500 | [diff] [blame] | 101 | requestBuilder=HttpRequest): |
Joe Gregorio | abda96f | 2011-02-11 20:19:33 -0500 | [diff] [blame] | 102 | """Construct a Resource for interacting with an API. |
| 103 | |
| 104 | Construct a Resource object for interacting with |
| 105 | an API. The serviceName and version are the |
| 106 | names from the Discovery service. |
| 107 | |
| 108 | Args: |
| 109 | serviceName: string, name of the service |
| 110 | version: string, the version of the service |
| 111 | discoveryServiceUrl: string, a URI Template that points to |
| 112 | the location of the discovery service. It should have two |
| 113 | parameters {api} and {apiVersion} that when filled in |
| 114 | produce an absolute URI to the discovery document for |
| 115 | that service. |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 116 | developerKey: string, key obtained |
| 117 | from https://code.google.com/apis/console |
Joe Gregorio | abda96f | 2011-02-11 20:19:33 -0500 | [diff] [blame] | 118 | model: apiclient.Model, converts to and from the wire format |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 119 | requestBuilder: apiclient.http.HttpRequest, encapsulator for |
| 120 | an HTTP request |
Joe Gregorio | abda96f | 2011-02-11 20:19:33 -0500 | [diff] [blame] | 121 | |
| 122 | Returns: |
| 123 | A Resource object with methods for interacting with |
| 124 | the service. |
| 125 | """ |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 126 | params = { |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 127 | 'api': serviceName, |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 128 | 'apiVersion': version |
| 129 | } |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 130 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 131 | if http is None: |
| 132 | http = httplib2.Http() |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 133 | requested_url = uritemplate.expand(discoveryServiceUrl, params) |
| 134 | logging.info('URL being requested: %s' % requested_url) |
| 135 | resp, content = http.request(requested_url) |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 136 | if resp.status > 400: |
| 137 | raise HttpError(resp, content, requested_url) |
Joe Gregorio | c0e0fe9 | 2011-03-04 16:16:55 -0500 | [diff] [blame] | 138 | try: |
| 139 | service = simplejson.loads(content) |
| 140 | except ValueError, e: |
Joe Gregorio | 205e73a | 2011-03-12 09:55:31 -0500 | [diff] [blame] | 141 | logging.error('Failed to parse as JSON: ' + content) |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 142 | raise InvalidJsonError() |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 143 | |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 144 | fn = os.path.join(os.path.dirname(__file__), 'contrib', |
| 145 | serviceName, 'future.json') |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 146 | try: |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 147 | f = file(fn, 'r') |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 148 | future = f.read() |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 149 | f.close() |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 150 | except IOError: |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 151 | future = None |
| 152 | |
| 153 | return build_from_document(content, discoveryServiceUrl, future, |
| 154 | http, developerKey, model, requestBuilder) |
| 155 | |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 156 | |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 157 | def build_from_document( |
| 158 | service, |
| 159 | base, |
| 160 | future=None, |
| 161 | http=None, |
| 162 | developerKey=None, |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 163 | model=None, |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 164 | requestBuilder=HttpRequest): |
Joe Gregorio | abda96f | 2011-02-11 20:19:33 -0500 | [diff] [blame] | 165 | """Create a Resource for interacting with an API. |
| 166 | |
| 167 | Same as `build()`, but constructs the Resource object |
| 168 | from a discovery document that is it given, as opposed to |
| 169 | retrieving one over HTTP. |
| 170 | |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 171 | Args: |
| 172 | service: string, discovery document |
| 173 | base: string, base URI for all HTTP requests, usually the discovery URI |
| 174 | future: string, discovery document with future capabilities |
| 175 | auth_discovery: dict, information about the authentication the API supports |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 176 | http: httplib2.Http, An instance of httplib2.Http or something that acts |
| 177 | like it that HTTP requests will be made through. |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 178 | developerKey: string, Key for controlling API usage, generated |
| 179 | from the API Console. |
| 180 | model: Model class instance that serializes and |
| 181 | de-serializes requests and responses. |
| 182 | requestBuilder: Takes an http request and packages it up to be executed. |
Joe Gregorio | abda96f | 2011-02-11 20:19:33 -0500 | [diff] [blame] | 183 | |
| 184 | Returns: |
| 185 | A Resource object with methods for interacting with |
| 186 | the service. |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 187 | """ |
| 188 | |
| 189 | service = simplejson.loads(service) |
Joe Gregorio | 6a63a76 | 2011-05-02 22:36:05 -0400 | [diff] [blame] | 190 | base = urlparse.urljoin(base, service['basePath']) |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 191 | if future: |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 192 | future = simplejson.loads(future) |
| 193 | auth_discovery = future.get('auth', {}) |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 194 | else: |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 195 | future = {} |
| 196 | auth_discovery = {} |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame^] | 197 | schema = service.get('schemas', {}) |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 198 | |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 199 | if model is None: |
Joe Gregorio | f863f7a | 2011-02-24 03:24:44 -0500 | [diff] [blame] | 200 | features = service.get('features', []) |
Joe Gregorio | 266c644 | 2011-02-23 16:08:54 -0500 | [diff] [blame] | 201 | model = JsonModel('dataWrapper' in features) |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 202 | resource = createResource(http, base, model, requestBuilder, developerKey, |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame^] | 203 | service, future, schema) |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 204 | |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 205 | def auth_method(): |
| 206 | """Discovery information about the authentication the API uses.""" |
| 207 | return auth_discovery |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 208 | |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 209 | setattr(resource, 'auth_discovery', auth_method) |
Joe Gregorio | a2f56e7 | 2010-09-09 15:15:56 -0400 | [diff] [blame] | 210 | |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 211 | return resource |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 212 | |
| 213 | |
Joe Gregorio | 61d7e96 | 2011-02-22 22:52:07 -0500 | [diff] [blame] | 214 | def _cast(value, schema_type): |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 215 | """Convert value to a string based on JSON Schema type. |
| 216 | |
| 217 | See http://tools.ietf.org/html/draft-zyp-json-schema-03 for more details on |
| 218 | JSON Schema. |
| 219 | |
| 220 | Args: |
| 221 | value: any, the value to convert |
| 222 | schema_type: string, the type that value should be interpreted as |
| 223 | |
| 224 | Returns: |
| 225 | A string representation of 'value' based on the schema_type. |
| 226 | """ |
| 227 | if schema_type == 'string': |
Joe Gregorio | f863f7a | 2011-02-24 03:24:44 -0500 | [diff] [blame] | 228 | if type(value) == type('') or type(value) == type(u''): |
| 229 | return value |
| 230 | else: |
| 231 | return str(value) |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 232 | elif schema_type == 'integer': |
| 233 | return str(int(value)) |
| 234 | elif schema_type == 'number': |
| 235 | return str(float(value)) |
| 236 | elif schema_type == 'boolean': |
| 237 | return str(bool(value)).lower() |
| 238 | else: |
Joe Gregorio | f863f7a | 2011-02-24 03:24:44 -0500 | [diff] [blame] | 239 | if type(value) == type('') or type(value) == type(u''): |
| 240 | return value |
| 241 | else: |
| 242 | return str(value) |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 243 | |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 244 | MULTIPLIERS = { |
| 245 | "KB": 2**10, |
| 246 | "MB": 2**20, |
| 247 | "GB": 2**30, |
| 248 | "TB": 2**40, |
| 249 | } |
| 250 | |
| 251 | def _media_size_to_long(maxSize): |
| 252 | """Convert a string media size, such as 10GB or 3TB into an integer.""" |
| 253 | units = maxSize[-2:].upper() |
| 254 | multiplier = MULTIPLIERS.get(units, 0) |
| 255 | if multiplier: |
| 256 | return int(maxSize[:-2])*multiplier |
| 257 | else: |
| 258 | return int(maxSize) |
| 259 | |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 260 | |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 261 | def createResource(http, baseUrl, model, requestBuilder, |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame^] | 262 | developerKey, resourceDesc, futureDesc, schema): |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 263 | |
| 264 | class Resource(object): |
| 265 | """A class for interacting with a resource.""" |
| 266 | |
| 267 | def __init__(self): |
| 268 | self._http = http |
| 269 | self._baseUrl = baseUrl |
| 270 | self._model = model |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 271 | self._developerKey = developerKey |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 272 | self._requestBuilder = requestBuilder |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 273 | |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 274 | def createMethod(theclass, methodName, methodDesc, futureDesc): |
Joe Gregorio | d92897c | 2011-07-07 11:44:56 -0400 | [diff] [blame] | 275 | methodName = _fix_method_name(methodName) |
Joe Gregorio | 6a63a76 | 2011-05-02 22:36:05 -0400 | [diff] [blame] | 276 | pathUrl = methodDesc['path'] |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 277 | httpMethod = methodDesc['httpMethod'] |
Joe Gregorio | 6a63a76 | 2011-05-02 22:36:05 -0400 | [diff] [blame] | 278 | methodId = methodDesc['id'] |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 279 | |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 280 | mediaPathUrl = None |
| 281 | accept = [] |
| 282 | maxSize = 0 |
| 283 | if 'mediaUpload' in methodDesc: |
| 284 | mediaUpload = methodDesc['mediaUpload'] |
| 285 | mediaPathUrl = mediaUpload['protocols']['simple']['path'] |
| 286 | accept = mediaUpload['accept'] |
| 287 | maxSize = _media_size_to_long(mediaUpload['maxSize']) |
| 288 | |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 289 | if 'parameters' not in methodDesc: |
| 290 | methodDesc['parameters'] = {} |
| 291 | for name in STACK_QUERY_PARAMETERS: |
| 292 | methodDesc['parameters'][name] = { |
| 293 | 'type': 'string', |
Joe Gregorio | 6a63a76 | 2011-05-02 22:36:05 -0400 | [diff] [blame] | 294 | 'location': 'query' |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 295 | } |
| 296 | |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 297 | if httpMethod in ['PUT', 'POST', 'PATCH']: |
Joe Gregorio | c3fae8a | 2011-02-18 14:19:50 -0500 | [diff] [blame] | 298 | methodDesc['parameters']['body'] = { |
| 299 | 'description': 'The request body.', |
Joe Gregorio | c2a7393 | 2011-02-22 10:17:06 -0500 | [diff] [blame] | 300 | 'type': 'object', |
Joe Gregorio | 1ae3e74 | 2011-02-25 15:17:14 -0500 | [diff] [blame] | 301 | 'required': True, |
Joe Gregorio | c3fae8a | 2011-02-18 14:19:50 -0500 | [diff] [blame] | 302 | } |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 303 | if 'mediaUpload' in methodDesc: |
| 304 | methodDesc['parameters']['media_body'] = { |
| 305 | 'description': 'The filename of the media request body.', |
| 306 | 'type': 'string', |
| 307 | 'required': False, |
| 308 | } |
| 309 | methodDesc['parameters']['body']['required'] = False |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 310 | |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 311 | argmap = {} # Map from method parameter name to query parameter name |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 312 | required_params = [] # Required parameters |
Joe Gregorio | 61d7e96 | 2011-02-22 22:52:07 -0500 | [diff] [blame] | 313 | repeated_params = [] # Repeated parameters |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 314 | pattern_params = {} # Parameters that must match a regex |
| 315 | query_params = [] # Parameters that will be used in the query string |
| 316 | path_params = {} # Parameters that will be used in the base URL |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 317 | param_type = {} # The type of the parameter |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 318 | enum_params = {} # Allowable enumeration values for each parameter |
| 319 | |
| 320 | |
Joe Gregorio | 4292c6e | 2010-09-09 14:32:43 -0400 | [diff] [blame] | 321 | if 'parameters' in methodDesc: |
| 322 | for arg, desc in methodDesc['parameters'].iteritems(): |
| 323 | param = key2param(arg) |
| 324 | argmap[param] = arg |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 325 | |
Joe Gregorio | 4292c6e | 2010-09-09 14:32:43 -0400 | [diff] [blame] | 326 | if desc.get('pattern', ''): |
| 327 | pattern_params[param] = desc['pattern'] |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 328 | if desc.get('enum', ''): |
| 329 | enum_params[param] = desc['enum'] |
Joe Gregorio | 4292c6e | 2010-09-09 14:32:43 -0400 | [diff] [blame] | 330 | if desc.get('required', False): |
| 331 | required_params.append(param) |
Joe Gregorio | 61d7e96 | 2011-02-22 22:52:07 -0500 | [diff] [blame] | 332 | if desc.get('repeated', False): |
| 333 | repeated_params.append(param) |
Joe Gregorio | 6a63a76 | 2011-05-02 22:36:05 -0400 | [diff] [blame] | 334 | if desc.get('location') == 'query': |
Joe Gregorio | 4292c6e | 2010-09-09 14:32:43 -0400 | [diff] [blame] | 335 | query_params.append(param) |
Joe Gregorio | 6a63a76 | 2011-05-02 22:36:05 -0400 | [diff] [blame] | 336 | if desc.get('location') == 'path': |
Joe Gregorio | 4292c6e | 2010-09-09 14:32:43 -0400 | [diff] [blame] | 337 | path_params[param] = param |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 338 | param_type[param] = desc.get('type', 'string') |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 339 | |
Joe Gregorio | bc2ff9b | 2010-11-08 09:20:48 -0500 | [diff] [blame] | 340 | for match in URITEMPLATE.finditer(pathUrl): |
| 341 | for namematch in VARNAME.finditer(match.group(0)): |
| 342 | name = key2param(namematch.group(0)) |
| 343 | path_params[name] = name |
| 344 | if name in query_params: |
| 345 | query_params.remove(name) |
| 346 | |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 347 | def method(self, **kwargs): |
| 348 | for name in kwargs.iterkeys(): |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 349 | if name not in argmap: |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 350 | raise TypeError('Got an unexpected keyword argument "%s"' % name) |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 351 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 352 | for name in required_params: |
Joe Gregorio | fbf9d0d | 2010-08-18 16:50:47 -0400 | [diff] [blame] | 353 | if name not in kwargs: |
| 354 | raise TypeError('Missing required parameter "%s"' % name) |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 355 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 356 | for name, regex in pattern_params.iteritems(): |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 357 | if name in kwargs: |
| 358 | if re.match(regex, kwargs[name]) is None: |
Joe Gregorio | 3bbbf66 | 2010-08-30 16:41:53 -0400 | [diff] [blame] | 359 | raise TypeError( |
| 360 | 'Parameter "%s" value "%s" does not match the pattern "%s"' % |
| 361 | (name, kwargs[name], regex)) |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 362 | |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 363 | for name, enums in enum_params.iteritems(): |
| 364 | if name in kwargs: |
| 365 | if kwargs[name] not in enums: |
| 366 | raise TypeError( |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 367 | 'Parameter "%s" value "%s" is not an allowed value in "%s"' % |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 368 | (name, kwargs[name], str(enums))) |
| 369 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 370 | actual_query_params = {} |
| 371 | actual_path_params = {} |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 372 | for key, value in kwargs.iteritems(): |
Joe Gregorio | 61d7e96 | 2011-02-22 22:52:07 -0500 | [diff] [blame] | 373 | to_type = param_type.get(key, 'string') |
| 374 | # For repeated parameters we cast each member of the list. |
| 375 | if key in repeated_params and type(value) == type([]): |
| 376 | cast_value = [_cast(x, to_type) for x in value] |
| 377 | else: |
| 378 | cast_value = _cast(value, to_type) |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 379 | if key in query_params: |
Joe Gregorio | 61d7e96 | 2011-02-22 22:52:07 -0500 | [diff] [blame] | 380 | actual_query_params[argmap[key]] = cast_value |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 381 | if key in path_params: |
Joe Gregorio | 61d7e96 | 2011-02-22 22:52:07 -0500 | [diff] [blame] | 382 | actual_path_params[argmap[key]] = cast_value |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 383 | body_value = kwargs.get('body', None) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 384 | media_filename = kwargs.get('media_body', None) |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 385 | |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 386 | if self._developerKey: |
| 387 | actual_query_params['key'] = self._developerKey |
| 388 | |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 389 | headers = {} |
Joe Gregorio | 3bbbf66 | 2010-08-30 16:41:53 -0400 | [diff] [blame] | 390 | headers, params, query, body = self._model.request(headers, |
| 391 | actual_path_params, actual_query_params, body_value) |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 392 | |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 393 | expanded_url = uritemplate.expand(pathUrl, params) |
Joe Gregorio | 922b78c | 2011-05-26 21:36:34 -0400 | [diff] [blame] | 394 | url = urlparse.urljoin(self._baseUrl, expanded_url + query) |
| 395 | |
| 396 | if media_filename: |
| 397 | (media_mime_type, encoding) = mimetypes.guess_type(media_filename) |
| 398 | if media_mime_type is None: |
| 399 | raise UnknownFileType(media_filename) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 400 | if not mimeparse.best_match([media_mime_type], ','.join(accept)): |
| 401 | raise UnacceptableMimeTypeError(media_mime_type) |
Joe Gregorio | 922b78c | 2011-05-26 21:36:34 -0400 | [diff] [blame] | 402 | |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 403 | # Check the maxSize |
| 404 | if maxSize > 0 and os.path.getsize(media_filename) > maxSize: |
| 405 | raise MediaUploadSizeError(media_filename) |
| 406 | |
| 407 | # Use the media path uri for media uploads |
| 408 | expanded_url = uritemplate.expand(mediaPathUrl, params) |
| 409 | url = urlparse.urljoin(self._baseUrl, expanded_url + query) |
Joe Gregorio | 922b78c | 2011-05-26 21:36:34 -0400 | [diff] [blame] | 410 | |
| 411 | if body is None: |
| 412 | headers['content-type'] = media_mime_type |
| 413 | # make the body the contents of the file |
| 414 | f = file(media_filename, 'rb') |
| 415 | body = f.read() |
| 416 | f.close() |
| 417 | else: |
| 418 | msgRoot = MIMEMultipart('related') |
| 419 | # msgRoot should not write out it's own headers |
| 420 | setattr(msgRoot, '_write_headers', lambda self: None) |
| 421 | |
| 422 | # attach the body as one part |
| 423 | msg = MIMENonMultipart(*headers['content-type'].split('/')) |
| 424 | msg.set_payload(body) |
| 425 | msgRoot.attach(msg) |
| 426 | |
| 427 | # attach the media as the second part |
| 428 | msg = MIMENonMultipart(*media_mime_type.split('/')) |
| 429 | msg['Content-Transfer-Encoding'] = 'binary' |
| 430 | |
| 431 | f = file(media_filename, 'rb') |
| 432 | msg.set_payload(f.read()) |
| 433 | f.close() |
| 434 | msgRoot.attach(msg) |
| 435 | |
| 436 | body = msgRoot.as_string() |
| 437 | |
| 438 | # must appear after the call to as_string() to get the right boundary |
| 439 | headers['content-type'] = ('multipart/related; ' |
| 440 | 'boundary="%s"') % msgRoot.get_boundary() |
Joe Gregorio | fbf9d0d | 2010-08-18 16:50:47 -0400 | [diff] [blame] | 441 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 442 | logging.info('URL being requested: %s' % url) |
Joe Gregorio | abda96f | 2011-02-11 20:19:33 -0500 | [diff] [blame] | 443 | return self._requestBuilder(self._http, |
| 444 | self._model.response, |
| 445 | url, |
| 446 | method=httpMethod, |
| 447 | body=body, |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 448 | headers=headers, |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 449 | methodId=methodId) |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 450 | |
Joe Gregorio | c3fae8a | 2011-02-18 14:19:50 -0500 | [diff] [blame] | 451 | docs = [methodDesc.get('description', DEFAULT_METHOD_DOC), '\n\n'] |
| 452 | if len(argmap) > 0: |
Joe Gregorio | 61d7e96 | 2011-02-22 22:52:07 -0500 | [diff] [blame] | 453 | docs.append('Args:\n') |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 454 | for arg in argmap.iterkeys(): |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 455 | if arg in STACK_QUERY_PARAMETERS: |
| 456 | continue |
Joe Gregorio | 61d7e96 | 2011-02-22 22:52:07 -0500 | [diff] [blame] | 457 | repeated = '' |
| 458 | if arg in repeated_params: |
| 459 | repeated = ' (repeated)' |
| 460 | required = '' |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 461 | if arg in required_params: |
Joe Gregorio | 61d7e96 | 2011-02-22 22:52:07 -0500 | [diff] [blame] | 462 | required = ' (required)' |
Joe Gregorio | c2a7393 | 2011-02-22 10:17:06 -0500 | [diff] [blame] | 463 | paramdesc = methodDesc['parameters'][argmap[arg]] |
| 464 | paramdoc = paramdesc.get('description', 'A parameter') |
| 465 | paramtype = paramdesc.get('type', 'string') |
Joe Gregorio | 61d7e96 | 2011-02-22 22:52:07 -0500 | [diff] [blame] | 466 | docs.append(' %s: %s, %s%s%s\n' % (arg, paramtype, paramdoc, required, |
| 467 | repeated)) |
Joe Gregorio | c2a7393 | 2011-02-22 10:17:06 -0500 | [diff] [blame] | 468 | enum = paramdesc.get('enum', []) |
| 469 | enumDesc = paramdesc.get('enumDescriptions', []) |
| 470 | if enum and enumDesc: |
| 471 | docs.append(' Allowed values\n') |
| 472 | for (name, desc) in zip(enum, enumDesc): |
| 473 | docs.append(' %s - %s\n' % (name, desc)) |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 474 | |
| 475 | setattr(method, '__doc__', ''.join(docs)) |
| 476 | setattr(theclass, methodName, method) |
| 477 | |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame^] | 478 | # This is a legacy method, as only Buzz and Moderator use the future.json |
| 479 | # functionality for generating _next methods. It will be kept around as long |
| 480 | # as those API versions are around, but no new APIs should depend upon it. |
| 481 | def createNextMethodFromFuture(theclass, methodName, methodDesc, futureDesc): |
Joe Gregorio | d92897c | 2011-07-07 11:44:56 -0400 | [diff] [blame] | 482 | methodName = _fix_method_name(methodName) |
Joe Gregorio | 6a63a76 | 2011-05-02 22:36:05 -0400 | [diff] [blame] | 483 | methodId = methodDesc['id'] + '.next' |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 484 | |
Joe Gregorio | c3fae8a | 2011-02-18 14:19:50 -0500 | [diff] [blame] | 485 | def methodNext(self, previous): |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 486 | """ |
| 487 | Takes a single argument, 'body', which is the results |
| 488 | from the last call, and returns the next set of items |
| 489 | in the collection. |
| 490 | |
| 491 | Returns None if there are no more items in |
| 492 | the collection. |
| 493 | """ |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 494 | if futureDesc['type'] != 'uri': |
| 495 | raise UnknownLinkType(futureDesc['type']) |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 496 | |
| 497 | try: |
| 498 | p = previous |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 499 | for key in futureDesc['location']: |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 500 | p = p[key] |
| 501 | url = p |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 502 | except (KeyError, TypeError): |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 503 | return None |
| 504 | |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 505 | if self._developerKey: |
| 506 | parsed = list(urlparse.urlparse(url)) |
ade@google.com | c5eb46f | 2010-09-27 23:35:39 +0100 | [diff] [blame] | 507 | q = parse_qsl(parsed[4]) |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 508 | q.append(('key', self._developerKey)) |
| 509 | parsed[4] = urllib.urlencode(q) |
| 510 | url = urlparse.urlunparse(parsed) |
| 511 | |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 512 | headers = {} |
| 513 | headers, params, query, body = self._model.request(headers, {}, {}, None) |
| 514 | |
| 515 | logging.info('URL being requested: %s' % url) |
| 516 | resp, content = self._http.request(url, method='GET', headers=headers) |
| 517 | |
Joe Gregorio | abda96f | 2011-02-11 20:19:33 -0500 | [diff] [blame] | 518 | return self._requestBuilder(self._http, |
| 519 | self._model.response, |
| 520 | url, |
| 521 | method='GET', |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 522 | headers=headers, |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 523 | methodId=methodId) |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 524 | |
Joe Gregorio | c3fae8a | 2011-02-18 14:19:50 -0500 | [diff] [blame] | 525 | setattr(theclass, methodName, methodNext) |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 526 | |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame^] | 527 | |
| 528 | def createNextMethod(theclass, methodName, methodDesc, futureDesc): |
| 529 | methodName = _fix_method_name(methodName) |
| 530 | methodId = methodDesc['id'] + '.next' |
| 531 | |
| 532 | def methodNext(self, previous_request, previous_response): |
| 533 | """Retrieves the next page of results. |
| 534 | |
| 535 | Args: |
| 536 | previous_request: The request for the previous page. |
| 537 | previous_response: The response from the request for the previous page. |
| 538 | |
| 539 | Returns: |
| 540 | A request object that you can call 'execute()' on to request the next |
| 541 | page. Returns None if there are no more items in the collection. |
| 542 | """ |
| 543 | # Retrieve nextPageToken from previous_response |
| 544 | # Use as pageToken in previous_request to create new request. |
| 545 | |
| 546 | if 'nextPageToken' not in previous_response: |
| 547 | return None |
| 548 | |
| 549 | request = copy.copy(previous_request) |
| 550 | |
| 551 | pageToken = previous_response['nextPageToken'] |
| 552 | parsed = list(urlparse.urlparse(request.uri)) |
| 553 | q = parse_qsl(parsed[4]) |
| 554 | |
| 555 | # Find and remove old 'pageToken' value from URI |
| 556 | newq = [(key, value) for (key, value) in q if key != 'pageToken'] |
| 557 | newq.append(('pageToken', pageToken)) |
| 558 | parsed[4] = urllib.urlencode(newq) |
| 559 | uri = urlparse.urlunparse(parsed) |
| 560 | |
| 561 | request.uri = uri |
| 562 | |
| 563 | logging.info('URL being requested: %s' % uri) |
| 564 | |
| 565 | return request |
| 566 | |
| 567 | setattr(theclass, methodName, methodNext) |
| 568 | |
| 569 | |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 570 | # Add basic methods to Resource |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 571 | if 'methods' in resourceDesc: |
| 572 | for methodName, methodDesc in resourceDesc['methods'].iteritems(): |
| 573 | if futureDesc: |
| 574 | future = futureDesc['methods'].get(methodName, {}) |
| 575 | else: |
| 576 | future = None |
| 577 | createMethod(Resource, methodName, methodDesc, future) |
| 578 | |
| 579 | # Add in nested resources |
| 580 | if 'resources' in resourceDesc: |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 581 | |
Joe Gregorio | c3fae8a | 2011-02-18 14:19:50 -0500 | [diff] [blame] | 582 | def createResourceMethod(theclass, methodName, methodDesc, futureDesc): |
Joe Gregorio | d92897c | 2011-07-07 11:44:56 -0400 | [diff] [blame] | 583 | methodName = _fix_method_name(methodName) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 584 | |
Joe Gregorio | c3fae8a | 2011-02-18 14:19:50 -0500 | [diff] [blame] | 585 | def methodResource(self): |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 586 | return createResource(self._http, self._baseUrl, self._model, |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 587 | self._requestBuilder, self._developerKey, |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame^] | 588 | methodDesc, futureDesc, schema) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 589 | |
Joe Gregorio | c3fae8a | 2011-02-18 14:19:50 -0500 | [diff] [blame] | 590 | setattr(methodResource, '__doc__', 'A collection resource.') |
| 591 | setattr(methodResource, '__is_resource__', True) |
| 592 | setattr(theclass, methodName, methodResource) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 593 | |
| 594 | for methodName, methodDesc in resourceDesc['resources'].iteritems(): |
| 595 | if futureDesc and 'resources' in futureDesc: |
| 596 | future = futureDesc['resources'].get(methodName, {}) |
| 597 | else: |
| 598 | future = {} |
Joe Gregorio | c3fae8a | 2011-02-18 14:19:50 -0500 | [diff] [blame] | 599 | createResourceMethod(Resource, methodName, methodDesc, future) |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 600 | |
| 601 | # Add <m>_next() methods to Resource |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 602 | if futureDesc and 'methods' in futureDesc: |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 603 | for methodName, methodDesc in futureDesc['methods'].iteritems(): |
| 604 | if 'next' in methodDesc and methodName in resourceDesc['methods']: |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame^] | 605 | createNextMethodFromFuture(Resource, methodName + '_next', |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 606 | resourceDesc['methods'][methodName], |
| 607 | methodDesc['next']) |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame^] | 608 | # Add _next() methods |
| 609 | # Look for response bodies in schema that contain nextPageToken, and methods |
| 610 | # that take a pageToken parameter. |
| 611 | if 'methods' in resourceDesc: |
| 612 | for methodName, methodDesc in resourceDesc['methods'].iteritems(): |
| 613 | if 'response' in methodDesc: |
| 614 | responseSchema = methodDesc['response'] |
| 615 | if '$ref' in responseSchema: |
| 616 | responseSchema = schema[responseSchema['$ref']] |
| 617 | hasNextPageToken = 'nextPageToken' in responseSchema['properties'] |
| 618 | hasPageToken = 'pageToken' in methodDesc.get('parameters', {}) |
| 619 | if hasNextPageToken and hasPageToken: |
| 620 | createNextMethod(Resource, methodName + '_next', |
| 621 | resourceDesc['methods'][methodName], |
| 622 | methodName) |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 623 | |
| 624 | return Resource() |