comparison rhodecode/controllers/api/__init__.py @ 1504:ed3254ac279b beta

Added optional arguments into API
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 01 Oct 2011 16:44:54 +0300
parents 256e729a94cd
children 4aba7be311e8
comparison
equal deleted inserted replaced
1503:b02aa5f2f066 1504:ed3254ac279b
28 import inspect 28 import inspect
29 import json 29 import json
30 import logging 30 import logging
31 import types 31 import types
32 import urllib 32 import urllib
33 from itertools import izip_longest
33 34
34 from paste.response import replace_header 35 from paste.response import replace_header
35 36
36 from pylons.controllers import WSGIController 37 from pylons.controllers import WSGIController
37 from pylons.controllers.util import Response 38 from pylons.controllers.util import Response
130 except AttributeError, e: 131 except AttributeError, e:
131 return jsonrpc_error(message=str(e)) 132 return jsonrpc_error(message=str(e))
132 133
133 # now that we have a method, add self._req_params to 134 # now that we have a method, add self._req_params to
134 # self.kargs and dispatch control to WGIController 135 # self.kargs and dispatch control to WGIController
135 arglist = inspect.getargspec(self._func)[0][1:] 136 argspec = inspect.getargspec(self._func)
137 arglist = argspec[0][1:]
138 defaults = argspec[3]
139 default_empty = types.NotImplementedType
140 kwarglist = list(izip_longest(reversed(arglist),reversed(defaults),
141 fillvalue=default_empty))
136 142
137 # this is little trick to inject logged in user for 143 # this is little trick to inject logged in user for
138 # perms decorators to work they expect the controller class to have 144 # perms decorators to work they expect the controller class to have
139 # rhodecode_user attribute set 145 # rhodecode_user attribute set
140 self.rhodecode_user = auth_u 146 self.rhodecode_user = auth_u
147 return jsonrpc_error(message='This method [%s] does not support ' 153 return jsonrpc_error(message='This method [%s] does not support '
148 'authentication (missing %s param)' % 154 'authentication (missing %s param)' %
149 (self._func.__name__, USER_SESSION_ATTR)) 155 (self._func.__name__, USER_SESSION_ATTR))
150 156
151 # get our arglist and check if we provided them as args 157 # get our arglist and check if we provided them as args
152 for arg in arglist: 158 for arg,default in kwarglist:
153 if arg == USER_SESSION_ATTR: 159 if arg == USER_SESSION_ATTR:
154 # USER_SESSION_ATTR is something translated from api key and 160 # USER_SESSION_ATTR is something translated from api key and
155 # this is checked before so we don't need validate it 161 # this is checked before so we don't need validate it
156 continue 162 continue
157 163
158 if not self._req_params or arg not in self._req_params: 164 # skip the required param check if it's default value is
159 return jsonrpc_error(message='Missing %s arg in JSON DATA' % arg) 165 # NotImplementedType (default_empty)
166 if not self._req_params or (type(default) == default_empty
167 and arg not in self._req_params):
168 return jsonrpc_error(message=('Missing non optional %s arg '
169 'in JSON DATA') % arg)
160 170
161 self._rpc_args = {USER_SESSION_ATTR:u} 171 self._rpc_args = {USER_SESSION_ATTR:u}
162 self._rpc_args.update(self._req_params) 172 self._rpc_args.update(self._req_params)
163 173
164 self._rpc_args['action'] = self._req_method 174 self._rpc_args['action'] = self._req_method