From ca876e4b9c40f631c8f518fe87dfa3dd29ae0ffc Mon Sep 17 00:00:00 2001 From: Joe Gregorio Date: Tue, 22 Feb 2011 19:39:42 -0500 Subject: [PATCH] Fixed checking for stack query parameters so they actually get added to the query paramters. --- apiclient/discovery.py | 29 +++++++++++++++++++++-------- oauth2client/client.py | 4 ++-- tests/test_discovery.py | 8 ++++++-- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/apiclient/discovery.py b/apiclient/discovery.py index db4dd47..b202913 100644 --- a/apiclient/discovery.py +++ b/apiclient/discovery.py @@ -44,6 +44,8 @@ VARNAME = re.compile('[a-zA-Z0-9_-]+') DISCOVERY_URI = ('https://www.googleapis.com/discovery/v0.3/describe/' '{api}/{apiVersion}') DEFAULT_METHOD_DOC = 'A description of how to use this function' + +# Query parameters that work, but don't appear in discovery STACK_QUERY_PARAMETERS = ['trace'] @@ -219,21 +221,29 @@ def createResource(http, baseUrl, model, requestBuilder, httpMethod = methodDesc['httpMethod'] methodId = methodDesc['rpcMethod'] - argmap = {} + if 'parameters' not in methodDesc: + methodDesc['parameters'] = {} + for name in STACK_QUERY_PARAMETERS: + methodDesc['parameters'][name] = { + 'type': 'string', + 'restParameterType': 'query' + } + if httpMethod in ['PUT', 'POST']: - if 'parameters' not in methodDesc: - methodDesc['parameters'] = {} methodDesc['parameters']['body'] = { 'description': 'The request body.', 'type': 'object', } + argmap = {} # Map from method parameter name to query parameter name required_params = [] # Required parameters pattern_params = {} # Parameters that must match a regex query_params = [] # Parameters that will be used in the query string path_params = {} # Parameters that will be used in the base URL param_type = {} # The type of the parameter - enum_params = {} + enum_params = {} # Allowable enumeration values for each parameter + + if 'parameters' in methodDesc: for arg, desc in methodDesc['parameters'].iteritems(): param = key2param(arg) @@ -260,7 +270,7 @@ def createResource(http, baseUrl, model, requestBuilder, def method(self, **kwargs): for name in kwargs.iterkeys(): - if name not in argmap and name not in STACK_QUERY_PARAMETERS: + if name not in argmap: raise TypeError('Got an unexpected keyword argument "%s"' % name) for name in required_params: @@ -278,16 +288,17 @@ def createResource(http, baseUrl, model, requestBuilder, if name in kwargs: if kwargs[name] not in enums: raise TypeError( - 'Parameter "%s" value "%s" is not in the list of allowed values "%s"' % + 'Parameter "%s" value "%s" is not an allowed value in "%s"' % (name, kwargs[name], str(enums))) actual_query_params = {} actual_path_params = {} for key, value in kwargs.iteritems(): + value_as_str = _to_string(value, param_type.get(key, 'string')) if key in query_params: - actual_query_params[argmap[key]] = _to_string(value, param_type[key]) + actual_query_params[argmap[key]] = value_as_str if key in path_params: - actual_path_params[argmap[key]] = _to_string(value, param_type[key]) + actual_path_params[argmap[key]] = value_as_str body_value = kwargs.get('body', None) if self._developerKey: @@ -321,6 +332,8 @@ def createResource(http, baseUrl, model, requestBuilder, if len(argmap) > 0: docs.append("Args:\n") for arg in argmap.iterkeys(): + if arg in STACK_QUERY_PARAMETERS: + continue required = "" if arg in required_params: required = " (required)" diff --git a/oauth2client/client.py b/oauth2client/client.py index c8d0941..0433600 100644 --- a/oauth2client/client.py +++ b/oauth2client/client.py @@ -37,12 +37,12 @@ class Error(Exception): class FlowExchangeError(Error): - """Error occurred during request.""" + """Error trying to exchange an authorization grant for an access token.""" pass class AccessTokenRefreshError(Error): - """Error occurred during request.""" + """Error trying to refresh an expired access token.""" pass diff --git a/tests/test_discovery.py b/tests/test_discovery.py index 6a48d0e..2e0104c 100644 --- a/tests/test_discovery.py +++ b/tests/test_discovery.py @@ -65,14 +65,14 @@ class Discovery(unittest.TestCase): buzz.activities().list(scope='@myself', userId='me') self.fail() except TypeError, e: - self.assertTrue('not in the list' in str(e)) + self.assertTrue('not an allowed value' in str(e)) # Parameter doesn't match regex try: buzz.activities().list(scope='not@', userId='foo') self.fail() except TypeError, e: - self.assertTrue('not in the list' in str(e)) + self.assertTrue('not an allowed value' in str(e)) # Unexpected parameter try: @@ -108,6 +108,10 @@ class Discovery(unittest.TestCase): zoo = build('zoo', 'v1', self.http) request = zoo.query(trace='html') + parsed = urlparse.urlparse(request.uri) + q = parse_qs(parsed[4]) + self.assertEqual(q['trace'], ['html']) + def test_buzz_resources(self): self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) buzz = build('buzz', 'v1', self.http)