From fbf9d0d8d5d5861a2086b4c273a3e965b0d52ed7 Mon Sep 17 00:00:00 2001 From: Joe Gregorio Date: Wed, 18 Aug 2010 16:50:47 -0400 Subject: [PATCH] Add checks for required parameters --- TODO | 7 ++++- apiclient/discovery.py | 11 +++++-- samples/cmdline/three_legged_dance.py | 42 ++++++++++++++------------- 3 files changed, 36 insertions(+), 24 deletions(-) diff --git a/TODO b/TODO index 0152144..e4785c6 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,6 @@ TODO ==== + - Unit tests against copies of current discovery docs - Flag required parameters @@ -8,7 +9,11 @@ TODO - OAuth cmdline sample should start local http server to catch response. - - 'Extra Discovery' for pagination + - Caching of discovery doc + + - Add in 'Extra Discovery' for pagination + + - Layered user-agent header, ala 'my-buzz-client/1.0 google-api-python-client/0.2 httplib2/0.6' - Implement requests as Command objects, either for immediate execution, or for batching. diff --git a/apiclient/discovery.py b/apiclient/discovery.py index 0f9395d..71e901b 100644 --- a/apiclient/discovery.py +++ b/apiclient/discovery.py @@ -31,8 +31,8 @@ import uritemplate class HttpError(Exception): pass -DISCOVERY_URI = 'http://www.googleapis.com/discovery/0.1/describe' + - '{?api,apiVersion}' +DISCOVERY_URI = 'http://www.googleapis.com/discovery/0.1/describe\ +{?api,apiVersion}' def key2method(key): @@ -96,7 +96,7 @@ class JsonModel(object): return simplejson.loads(content)['data'] else: if resp['content-type'] != 'application/json': - raise HttpError("%d %s" % (resp.status, resp.reason)) + raise HttpError('%d %s' % (resp.status, resp.reason)) else: raise HttpError(simplejson.loads(content)['error']) @@ -150,6 +150,7 @@ def createResource(http, baseUrl, model, resourceName, resourceDesc): pathUrl = re.sub(r'\{', r'{+', pathUrl) httpMethod = methodDesc['httpMethod'] args = methodDesc['parameters'].keys() + required = [arg for arg in args if methodDesc['parameters'][arg].get('required', True)] if httpMethod in ['PUT', 'POST']: args.append('body') argmap = dict([(key2param(key), key) for key in args]) @@ -161,11 +162,15 @@ def createResource(http, baseUrl, model, resourceName, resourceDesc): params = dict( [(argmap[key], value) for key, value in kwargs.iteritems()] ) + for name in required: + if name not in kwargs: + raise TypeError('Missing required parameter "%s"' % name) headers = {} headers, params, query, body = self._model.request(headers, params) url = urlparse.urljoin(self._baseUrl, uritemplate.expand(pathUrl, params) + query) + return self._model.response(*self._http.request( url, method=httpMethod, headers=headers, body=body)) diff --git a/samples/cmdline/three_legged_dance.py b/samples/cmdline/three_legged_dance.py index 8e85796..cc0ebf2 100644 --- a/samples/cmdline/three_legged_dance.py +++ b/samples/cmdline/three_legged_dance.py @@ -10,19 +10,21 @@ except ImportError: from cgi import parse_qs, parse_qsl httplib2.debuglevel = 4 -headers = {"user-agent": "jcgregorio-buzz-client", +headers = {'user-agent': 'google-api-client-python-buzz-cmdline/1.0', 'content-type': 'application/x-www-form-urlencoded' } consumer_key = 'anonymous' consumer_secret = 'anonymous' -request_token_url = 'https://www.google.com/accounts/OAuthGetRequestToken' + - '?domain=anonymous&scope=https://www.googleapis.com/auth/buzz' -access_token_url = 'https://www.google.com/accounts/OAuthGetAccessToken' + - '?domain=anonymous&scope=https://www.googleapis.com/auth/buzz' -authorize_url = 'https://www.google.com/buzz/api/auth/OAuthAuthorizeToken' + - '?domain=anonymous&scope=https://www.googleapis.com/auth/buzz' +request_token_url = 'https://www.google.com/accounts/OAuthGetRequestToken\ +?domain=anonymous&scope=https://www.googleapis.com/auth/buzz' + +access_token_url = 'https://www.google.com/accounts/OAuthGetAccessToken\ +?domain=anonymous&scope=https://www.googleapis.com/auth/buzz' + +authorize_url = 'https://www.google.com/buzz/api/auth/OAuthAuthorizeToken\ +?domain=anonymous&scope=https://www.googleapis.com/auth/buzz' consumer = oauth.Consumer(consumer_key, consumer_secret) client = oauth.Client(consumer) @@ -31,17 +33,17 @@ client = oauth.Client(consumer) # having the user authorize an access token and to sign the request to obtain # said access token. -resp, content = client.request(request_token_url, "POST", headers=headers, - body="oauth_callback=oob") +resp, content = client.request(request_token_url, 'POST', headers=headers, + body='oauth_callback=oob') if resp['status'] != '200': print content - raise Exception("Invalid response %s." % resp['status']) + raise Exception('Invalid response %s.' % resp['status']) request_token = dict(parse_qsl(content)) -print "Request Token:" -print " - oauth_token = %s" % request_token['oauth_token'] -print " - oauth_token_secret = %s" % request_token['oauth_token_secret'] +print 'Request Token:' +print ' - oauth_token = %s' % request_token['oauth_token'] +print ' - oauth_token_secret = %s' % request_token['oauth_token_secret'] print # Step 2: Redirect to the provider. Since this is a CLI script we do not @@ -58,7 +60,7 @@ url = (base_url.scheme, base_url.netloc, base_url.path, base_url.params, urllib.urlencode(query, True), base_url.fragment) authorize_url = urlparse.urlunparse(url) -print "Go to the following link in your browser:" +print 'Go to the following link in your browser:' print authorize_url print @@ -81,14 +83,14 @@ token = oauth.Token(request_token['oauth_token'], token.set_verifier(oauth_verifier) client = oauth.Client(consumer, token) -resp, content = client.request(access_token_url, "POST", headers=headers) +resp, content = client.request(access_token_url, 'POST', headers=headers) access_token = dict(parse_qsl(content)) -print "Access Token:" -print " - oauth_token = %s" % access_token['oauth_token'] -print " - oauth_token_secret = %s" % access_token['oauth_token_secret'] +print 'Access Token:' +print ' - oauth_token = %s' % access_token['oauth_token'] +print ' - oauth_token_secret = %s' % access_token['oauth_token_secret'] print -print "You may now access protected resources using the access tokens above." +print 'You may now access protected resources using the access tokens above.' print d = dict( @@ -98,6 +100,6 @@ d = dict( d.update(access_token) -f = open("oauth_token.dat", "w") +f = open('oauth_token.dat', 'w') f.write(simplejson.dumps(d)) f.close()