I have written a decorator to authenticate calls. It works fine with only one argument, but with more it doesn’t, triggering off a inner() takes exactly 1 argument (2 given). I have a bit of a callback spaghetti since I’m using Tornado, but I’m not sure what’s the best way to do this.
#this works
class FirstHandler(BaseHandler):
@asynchronous
@oauth_machine.auth
def post(self):
print self.user
self.finish()
#this now also does
class SecondHandler(BaseHandler):
@asynchronous
@oauth_machine.auth
def get(self, args):
self.write("ok")
self.finish()
The decorator function(s)
def auth(fn):
def inner(self, *args):
res = get_user_by_credentials(self, fn, args, callback=done_auth)
return inner
def get_user_by_credentials(self, fn, callback):
def onFetchUserCredentials(result, error):
self.user = result
callback(self, fn, args)
email = self.get_argument("email")
password = self.get_argument("password")
settings.DB.users.find_one({'email': email, 'password': password }, callback=onFetchUserCredentials)
def done_auth(result, fn, args):
return fn(result, args)
EDIT :
Updated code to working version.
Thanks!
I thought at first that the problem was quite simple, but then you posted a traceback contradicting the original error message. However, I think the problem is still pretty straightforward, assuming that the traceback error is the correct one. Recall that this:
Is simply syntactic sugar for this:
So when you use
@oauth_machine.authonget, it’s passed via a closure intoinnerasfn.It’s then passed into
get_user_by_credentials, again asfn, which in turn produces another closure, which passesfntocallback.callbackwas defined asdone_authback ininner, so that menas thatfn(i.e. the originalget) is passed there, and then called onresult:But
fn(i.e.get) takes two arguments. You pass it only one, causing an error.