Jaymon / endpoints

Lightweight REST api backend framework that automatically maps urls to python modules and classes
MIT License
29 stars 10 forks source link

routing not working as expected #94

Closed Jaymon closed 3 years ago

Jaymon commented 4 years ago
class Default(Controller):
    @param('foo')
    @route(lambda req: "foo" in req.kwargs)
    def POST_foo(self, **kwargs):
        pout.v(req)
        return {}

    @param('bar')
    @param('che')
    @route(lambda req: "foo" not in req.kwargs)
    def POST_no_foo(self, **kwargs):
        pout.v(req)
        return {}

I expected POSTing with foo would go to POST_foo and POSTing without foo would go to POST_no_foo but everything was going to POST_no_foo, I'm not sure why.

Jaymon commented 3 years ago

Turns out, order matters, while the above didn't work, this did:

class Default(Controller):
    @route(lambda req: "foo" in req.kwargs)
    @param('foo')
    def POST_foo(self, **kwargs):
        pout.v(req)
        return {}

    @route(lambda req: "foo" not in req.kwargs)
    @param('bar')
    @param('che')
    def POST_no_foo(self, **kwargs):
        pout.v(req)
        return {}

Notice that @route is now the first decorator, or farthest from the actual handle method. What was happening is POST_foo would fail because foo wasn't an argument, so a 400 response was returned because it was missing a parameter.