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 |
| 28 | import simplejson |
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 |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 32 | |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 33 | |
Joe Gregorio | 41cf797 | 2010-08-18 15:21:06 -0400 | [diff] [blame] | 34 | class HttpError(Exception): |
| 35 | pass |
| 36 | |
Joe Gregorio | 3bbbf66 | 2010-08-30 16:41:53 -0400 | [diff] [blame] | 37 | |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 38 | class UnknownLinkType(Exception): |
| 39 | pass |
| 40 | |
| 41 | DISCOVERY_URI = ('http://www.googleapis.com/discovery/0.1/describe' |
| 42 | '{?api,apiVersion}') |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 43 | |
| 44 | |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 45 | def key2param(key): |
| 46 | """ |
| 47 | max-results -> max_results |
| 48 | """ |
| 49 | result = [] |
| 50 | key = list(key) |
| 51 | if not key[0].isalpha(): |
| 52 | result.append('x') |
| 53 | for c in key: |
| 54 | if c.isalnum(): |
| 55 | result.append(c) |
| 56 | else: |
| 57 | result.append('_') |
| 58 | |
| 59 | return ''.join(result) |
| 60 | |
| 61 | |
| 62 | class JsonModel(object): |
Joe Gregorio | 41cf797 | 2010-08-18 15:21:06 -0400 | [diff] [blame] | 63 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 64 | def request(self, headers, path_params, query_params, body_value): |
| 65 | query = self.build_query(query_params) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 66 | headers['accept'] = 'application/json' |
Joe Gregorio | 6b2e5dc | 2010-09-14 15:11:03 -0400 | [diff] [blame] | 67 | if 'user-agent' in headers: |
| 68 | headers['user-agent'] += ' ' |
| 69 | else: |
| 70 | headers['user-agent'] = '' |
| 71 | headers['user-agent'] += 'google-api-client-python/1.0' |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 72 | if body_value is None: |
| 73 | return (headers, path_params, query, None) |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 74 | else: |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 75 | model = {'data': body_value} |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 76 | headers['content-type'] = 'application/json' |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 77 | return (headers, path_params, query, simplejson.dumps(model)) |
| 78 | |
| 79 | def build_query(self, params): |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 80 | params.update({'alt': 'json', 'prettyprint': 'true'}) |
| 81 | astuples = [] |
| 82 | for key, value in params.iteritems(): |
| 83 | if getattr(value, 'encode', False) and callable(value.encode): |
| 84 | value = value.encode('utf-8') |
| 85 | astuples.append((key, value)) |
| 86 | return '?' + urllib.urlencode(astuples) |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 87 | |
| 88 | def response(self, resp, content): |
Joe Gregorio | 41cf797 | 2010-08-18 15:21:06 -0400 | [diff] [blame] | 89 | # Error handling is TBD, for example, do we retry |
| 90 | # for some operation/error combinations? |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 91 | if resp.status < 300: |
| 92 | return simplejson.loads(content)['data'] |
| 93 | else: |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 94 | logging.debug('Content from bad request was: %s' % content) |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 95 | if resp['content-type'] != 'application/json': |
Joe Gregorio | fbf9d0d | 2010-08-18 16:50:47 -0400 | [diff] [blame] | 96 | raise HttpError('%d %s' % (resp.status, resp.reason)) |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 97 | else: |
| 98 | raise HttpError(simplejson.loads(content)['error']) |
| 99 | |
| 100 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame^] | 101 | def build(serviceName, version, http=None, |
Joe Gregorio | 41cf797 | 2010-08-18 15:21:06 -0400 | [diff] [blame] | 102 | discoveryServiceUrl=DISCOVERY_URI, auth=None, model=JsonModel()): |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 103 | params = { |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 104 | 'api': serviceName, |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 105 | 'apiVersion': version |
| 106 | } |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 107 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame^] | 108 | if http is None: |
| 109 | http = httplib2.Http() |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 110 | requested_url = uritemplate.expand(discoveryServiceUrl, params) |
| 111 | logging.info('URL being requested: %s' % requested_url) |
| 112 | resp, content = http.request(requested_url) |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 113 | d = simplejson.loads(content) |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 114 | service = d['data'][serviceName][version] |
| 115 | |
Joe Gregorio | 3bbbf66 | 2010-08-30 16:41:53 -0400 | [diff] [blame] | 116 | fn = os.path.join(os.path.dirname(__file__), "contrib", |
| 117 | serviceName, "future.json") |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 118 | f = file(fn, "r") |
| 119 | d = simplejson.load(f) |
| 120 | f.close() |
| 121 | future = d['data'][serviceName][version]['resources'] |
Joe Gregorio | a2f56e7 | 2010-09-09 15:15:56 -0400 | [diff] [blame] | 122 | auth_discovery = d['data'][serviceName][version]['auth'] |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 123 | |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 124 | base = service['baseUrl'] |
| 125 | resources = service['resources'] |
| 126 | |
| 127 | class Service(object): |
| 128 | """Top level interface for a service""" |
| 129 | |
| 130 | def __init__(self, http=http): |
| 131 | self._http = http |
| 132 | self._baseUrl = base |
| 133 | self._model = model |
| 134 | |
Joe Gregorio | a2f56e7 | 2010-09-09 15:15:56 -0400 | [diff] [blame] | 135 | def auth_discovery(self): |
| 136 | return auth_discovery |
| 137 | |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 138 | def createMethod(theclass, methodName, methodDesc, futureDesc): |
Joe Gregorio | 41cf797 | 2010-08-18 15:21:06 -0400 | [diff] [blame] | 139 | |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 140 | def method(self, **kwargs): |
| 141 | return createResource(self._http, self._baseUrl, self._model, |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 142 | methodName, methodDesc, futureDesc) |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 143 | |
| 144 | setattr(method, '__doc__', 'A description of how to use this function') |
| 145 | setattr(theclass, methodName, method) |
| 146 | |
| 147 | for methodName, methodDesc in resources.iteritems(): |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 148 | createMethod(Service, methodName, methodDesc, future[methodName]) |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 149 | return Service() |
| 150 | |
| 151 | |
Joe Gregorio | 3bbbf66 | 2010-08-30 16:41:53 -0400 | [diff] [blame] | 152 | def createResource(http, baseUrl, model, resourceName, resourceDesc, |
| 153 | futureDesc): |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 154 | |
| 155 | class Resource(object): |
| 156 | """A class for interacting with a resource.""" |
| 157 | |
| 158 | def __init__(self): |
| 159 | self._http = http |
| 160 | self._baseUrl = baseUrl |
| 161 | self._model = model |
| 162 | |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 163 | def createMethod(theclass, methodName, methodDesc, futureDesc): |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 164 | pathUrl = methodDesc['pathUrl'] |
| 165 | pathUrl = re.sub(r'\{', r'{+', pathUrl) |
| 166 | httpMethod = methodDesc['httpMethod'] |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 167 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 168 | argmap = {} |
| 169 | if httpMethod in ['PUT', 'POST']: |
| 170 | argmap['body'] = 'body' |
| 171 | |
| 172 | |
| 173 | required_params = [] # Required parameters |
| 174 | pattern_params = {} # Parameters that must match a regex |
| 175 | query_params = [] # Parameters that will be used in the query string |
| 176 | path_params = {} # Parameters that will be used in the base URL |
Joe Gregorio | 4292c6e | 2010-09-09 14:32:43 -0400 | [diff] [blame] | 177 | if 'parameters' in methodDesc: |
| 178 | for arg, desc in methodDesc['parameters'].iteritems(): |
| 179 | param = key2param(arg) |
| 180 | argmap[param] = arg |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 181 | |
Joe Gregorio | 4292c6e | 2010-09-09 14:32:43 -0400 | [diff] [blame] | 182 | if desc.get('pattern', ''): |
| 183 | pattern_params[param] = desc['pattern'] |
| 184 | if desc.get('required', False): |
| 185 | required_params.append(param) |
| 186 | if desc.get('parameterType') == 'query': |
| 187 | query_params.append(param) |
| 188 | if desc.get('parameterType') == 'path': |
| 189 | path_params[param] = param |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 190 | |
| 191 | def method(self, **kwargs): |
| 192 | for name in kwargs.iterkeys(): |
| 193 | if name not in argmap: |
| 194 | raise TypeError('Got an unexpected keyword argument "%s"' % name) |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 195 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 196 | for name in required_params: |
Joe Gregorio | fbf9d0d | 2010-08-18 16:50:47 -0400 | [diff] [blame] | 197 | if name not in kwargs: |
| 198 | raise TypeError('Missing required parameter "%s"' % name) |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 199 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 200 | for name, regex in pattern_params.iteritems(): |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 201 | if name in kwargs: |
| 202 | if re.match(regex, kwargs[name]) is None: |
Joe Gregorio | 3bbbf66 | 2010-08-30 16:41:53 -0400 | [diff] [blame] | 203 | raise TypeError( |
| 204 | 'Parameter "%s" value "%s" does not match the pattern "%s"' % |
| 205 | (name, kwargs[name], regex)) |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 206 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 207 | actual_query_params = {} |
| 208 | actual_path_params = {} |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 209 | for key, value in kwargs.iteritems(): |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 210 | if key in query_params: |
| 211 | actual_query_params[argmap[key]] = value |
| 212 | if key in path_params: |
| 213 | actual_path_params[argmap[key]] = value |
| 214 | body_value = kwargs.get('body', None) |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 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 | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 220 | expanded_url = uritemplate.expand(pathUrl, params) |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 221 | url = urlparse.urljoin(self._baseUrl, expanded_url + query) |
Joe Gregorio | fbf9d0d | 2010-08-18 16:50:47 -0400 | [diff] [blame] | 222 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 223 | logging.info('URL being requested: %s' % url) |
Joe Gregorio | 21f1167 | 2010-08-18 17:23:17 -0400 | [diff] [blame] | 224 | resp, content = self._http.request( |
| 225 | url, method=httpMethod, headers=headers, body=body) |
| 226 | |
| 227 | return self._model.response(resp, content) |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 228 | |
| 229 | docs = ['A description of how to use this function\n\n'] |
| 230 | for arg in argmap.iterkeys(): |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 231 | required = "" |
| 232 | if arg in required_params: |
| 233 | required = " (required)" |
| 234 | docs.append('%s - A parameter%s\n' % (arg, required)) |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 235 | |
| 236 | setattr(method, '__doc__', ''.join(docs)) |
| 237 | setattr(theclass, methodName, method) |
| 238 | |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 239 | def createNextMethod(theclass, methodName, methodDesc): |
| 240 | |
| 241 | def method(self, previous): |
| 242 | """ |
| 243 | Takes a single argument, 'body', which is the results |
| 244 | from the last call, and returns the next set of items |
| 245 | in the collection. |
| 246 | |
| 247 | Returns None if there are no more items in |
| 248 | the collection. |
| 249 | """ |
| 250 | if methodDesc['type'] != 'uri': |
| 251 | raise UnknownLinkType(methodDesc['type']) |
| 252 | |
| 253 | try: |
| 254 | p = previous |
| 255 | for key in methodDesc['location']: |
| 256 | p = p[key] |
| 257 | url = p |
| 258 | except KeyError: |
| 259 | return None |
| 260 | |
| 261 | headers = {} |
| 262 | headers, params, query, body = self._model.request(headers, {}, {}, None) |
| 263 | |
| 264 | logging.info('URL being requested: %s' % url) |
| 265 | resp, content = self._http.request(url, method='GET', headers=headers) |
| 266 | |
| 267 | return self._model.response(resp, content) |
| 268 | |
| 269 | setattr(theclass, methodName, method) |
| 270 | |
| 271 | # Add basic methods to Resource |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 272 | for methodName, methodDesc in resourceDesc['methods'].iteritems(): |
Joe Gregorio | 3bbbf66 | 2010-08-30 16:41:53 -0400 | [diff] [blame] | 273 | future = futureDesc['methods'].get(methodName, {}) |
| 274 | createMethod(Resource, methodName, methodDesc, future) |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 275 | |
| 276 | # Add <m>_next() methods to Resource |
| 277 | for methodName, methodDesc in futureDesc['methods'].iteritems(): |
| 278 | if 'next' in methodDesc and methodName in resourceDesc['methods']: |
| 279 | createNextMethod(Resource, methodName + "_next", methodDesc['next']) |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 280 | |
| 281 | return Resource() |