Jaymon / endpoints

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

an idea about subparams #56

Closed Jaymon closed 6 years ago

Jaymon commented 7 years ago

Right now, say you had a controller that could have a path:

class Foo(Controller):
    def GET(self, action, **kwargs): pass

where action can be bar or che, so valid requests would be:

I'm wondering if we could codify this with camel casing:

class FooBar(Controller):
    """handle /foo/bar"""
    def GET(self, **kwargs): pas

class FooChe(Controller):
    """handle /foo/che"""
    def GET(self, **kwargs): pas

So camel casing class names would move through path bits. This is inspired by some comments Jarid made about us having to set a few params as optional because we had multiple action path bits that needed different attributes:

class Foo(Controller):
    @param(0, choices=["one", "two"], help="the action value")
    @param("bar", default=None, help="only needed if action is 'one' but needs to be optional")
    @param("che", default=None, help="only needed if action is 'two' but needs to be optional")
    def GET(self, action, **kwargs): pass
Jaymon commented 7 years ago

Another way to do it would be to break out all the param stuff into a Param class, the current interface would remain unchanged but there would be an addition of a subparams flag:

class Foo(Controller):
    @paramval(0, val="one" subparams=[
        Param("bar"),
        Param("che")
    ])
    @paramval(0, val="two" subparams=[
        Param("baz"),
    ])
    def GET(self, action, **kwargs): pass

I think this might be too obtuse an interface though, I'll keep thinking

Jaymon commented 7 years ago
class Foo(Controller):
    @param(0, subparams={
        "one": [
            Param("bar"),
            Param("che")
        ],
        "two": [
            Param("baz")
        ],
    })
    def GET(self, action, **kwargs): pass

So the subparams keys become the choices for the param.

Jaymon commented 6 years ago

I don't think this is applicable anymore, like https://github.com/Jaymon/endpoints/commit/686447354a6a7b9196860ab4eb8acb24676c77c7 states