Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of $default stage to support HTTP API #40

Closed
wants to merge 2 commits into from

Conversation

kylebarron
Copy link
Contributor

@kylebarron kylebarron commented Apr 2, 2020

Closes #39

Working relative paths:
image

Note that there's lots of scaffolding in this pr to help test that it works, but the only actual change is here:

    def prefix(self):
        """Return the API prefix."""
        if self.apigw_stage:
            if self.apigw_stage == '/$default':
                return self.api_prefix

            return self.apigw_stage + self.api_prefix

@kylebarron kylebarron marked this pull request as ready for review April 2, 2020 03:10
@kylebarron
Copy link
Contributor Author

I did a test with the app.py from the example and everything seems to work fine. (Though with the unmodified app.py file /json and /binary don't work because the catch-all /<string:user> is too high in the file and overrides each endpoint.)

I'm assuming you won't want to merge this PR as-is because of the extra files. I can create a separate PR if you want, or you can just make the modification above yourself if you approve.

@kylebarron
Copy link
Contributor Author

Also, I can't tell.. are you using regex to parse the input event.json?

@vincentsarago
Copy link
Owner

to parse the input event.json?

I'm not sure to understand

@kylebarron
Copy link
Contributor Author

I suppose event is declared to be a Dict... I was just confused why you use so many complex regexes

params_expr = re.compile(r"(<[^>]*>)")
proxy_pattern = re.compile(r"/{(?P<name>.+)\+}$")
param_pattern = re.compile(
r"^<((?P<type>[a-zA-Z0-9_]+)(\((?P<pattern>.+)\))?\:)?(?P<name>[a-zA-Z0-9_]+)>$"
)
regex_pattern = re.compile(
r"^<(?P<type>regex)\((?P<pattern>.+)\):(?P<name>[a-zA-Z0-9_]+)>$"
)
def _path_to_regex(path: str) -> str:
path = f"^{path}$" # full match
path = re.sub(r"<[a-zA-Z0-9_]+>", r"([a-zA-Z0-9_]+)", path)
path = re.sub(r"<string\:[a-zA-Z0-9_]+>", r"([a-zA-Z0-9_]+)", path)
path = re.sub(r"<int\:[a-zA-Z0-9_]+>", r"([0-9]+)", path)
path = re.sub(r"<float\:[a-zA-Z0-9_]+>", "([+-]?[0-9]+.[0-9]+)", path)
path = re.sub(
r"<uuid\:[a-zA-Z0-9_]+>",
"([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})",
path,
)
for regexParam in re.findall(r"(<regex[^>]*>)", path):
matches = regex_pattern.search(regexParam)
expr = matches.groupdict()["pattern"]
path = path.replace(regexParam, f"({expr})")
return path
def _path_to_openapi(path: str) -> str:
for regexParam in re.findall(r"(<regex[^>]*>)", path):
match = regex_pattern.search(regexParam).groupdict()
name = match["name"]
path = path.replace(regexParam, f"<regex:{name}>")
path = re.sub(r"<([a-zA-Z0-9_]+\:)?", "{", path)
return re.sub(r">", "}", path)

@vincentsarago
Copy link
Owner

params_expr, param_pattern, regex_pattern and _path_to_regex() are only to parse the routes and translate event path. It's complex but I'm sure it is still a simple solution ;-)

@kylebarron
Copy link
Contributor Author

Ok well I'll leave those regexes to you!

@vincentsarago
Copy link
Owner

Hey @kylebarron thanks for the PR. I think we can open a new BR with just the change and also update tests and documentation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Http API
2 participants