Jaymon / endpoints

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

add Controller.pre_handle() and Controller.post_handle() methods #44

Closed Jaymon closed 7 years ago

Jaymon commented 9 years ago

The use case is a Controller that has GET and POST methods, and wants to have common error checking functionality between the two.

This popped up because in one of our controllers we have a foo variable that needs to be checked the same for both http methods and there is no reasonable way to do it, currently, the way I DRY'ed it was by doing:

class Bar(Controller):

    @classmethod
    def validated(cls, val):
        # do validation here
        return val

    @decorators.param('foo', type=validated)
    def POST(self, **kwargs): pass

    @decorators.param('foo', type=validated)
    def GET(self, **kwargs): pass

Which violates the "this is easy to conceptualize and implement" rule of programming

Jaymon commented 7 years ago

There are a couple reasonable ways to handle this:

  1. decorators can support classes also as discussed in this other issue

  2. you could have Call.handle first try Controller.start_handle and then Controller.start_METHOD (eg, Controller.start_GET for GET requests). This would allow you to add shared decorators between GET and POST requests to the start_handle method and ones that should just be confined to a certain request (like all GET requests from which there could be 3 GET methods to choose from) to be added to start_GET).