sloev/python-lambdarest

Support declaring path params when binding a handler

simongarnier opened this issue · 4 comments

Like mentioned in the second point of this comment, declaring path params would allow validation of event path, even when the path contains path params.

Discussion on the implementation can be done here!

sloev commented

To make a solution for the part of having path template to wildcards we can use the following recursive solution:

from collections import defaultdict

def dot_to_dict(dots, end_val):
    dots = dots.split("/")[1:]

    def rec(dots):
        if not dots:
            return end_val
        dot = dots.pop(0)
        if dot[0]=="{" and dot[-1] == "}":
            res = rec(dots)
            return defaultdict(lambda : res )
        return {dot:rec(dots)}

    return rec(dots)

ps = [
    "/foo/{id}/bar",
    "/{id}/foo/bar",
    "/foo/bar/{id}"
]

for p in ps:
    dots = p.split("/")[1:]
    d = dot_to_dict(p, "GOT_TO_THE_HANDLER!!!")
    result = d
    for dot in dots:
        result = result[dot]
    print("p: {}, d: {}, res: {}".format(p, d, result))

outputs:

p: /foo/{id}/bar, d: {'foo': defaultdict(<function <lambda> at 0x10bb47de8>, {'{id}': {'bar': 'GOT_TO_THE_HANDLER!!!'}})}, res: GOT_TO_THE_HANDLER!!!
p: /{id}/foo/bar, d: defaultdict(<function <lambda> at 0x10bb47ed8>, {'{id}': {'foo': {'bar': 'GOT_TO_THE_HANDLER!!!'}}}), res: GOT_TO_THE_HANDLER!!!
p: /foo/bar/{id}, d: {'foo': {'bar': defaultdict(<function <lambda> at 0x10bb47f50>, {'{id}': 'GOT_TO_THE_HANDLER!!!'})}}, res: GOT_TO_THE_HANDLER!!!

Now we just need to find a solution for extracting the path vars during handling and add them to the event object given to the handlers

sloev commented

Any news on this Simon? We could collab on this, how urgent is it for you?

Hey @sloev! This would be a nice to have for my project and the wildcard path works great as a workaround. I'm trying to get my project off the ground right now, but I'll be sure to come back to this when my thing goes into production!

sloev commented

closing, since lack of hands to implement, please reopen this issue if interest mounts