-
Notifications
You must be signed in to change notification settings - Fork 58
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
Convert functions to accept RequestHandlers #311
Conversation
gramex/handlers/functionhandler.py
Outdated
def wrapper(handler): | ||
args = handler.path_args | ||
if handler.request.method == 'GET': | ||
kwargs = {k: v[0] for k, v in handler.args.items()} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
handler.get_arg
does this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Test all HTTP methods.
- Test all native types including
None
. - Test what happens with default named arguments.
-
?x=1&x=2&x=3
should be doable. Perform the typecasting from the hints.
gramex/handlers/functionhandler.py
Outdated
def wrapper(handler): | ||
args = handler.path_args | ||
if handler.request.method == 'GET': | ||
kwargs = {k: v[0] for k, v in handler.args.items()} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps {k: v if len(v) > 1 else v[0] for k, v in handler.args.items()}
to handle ?x=1&x=2&y=3
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gramex/handlers/functionhandler.py
Outdated
args = [argtype(k) for k in args] | ||
else: | ||
kwargs[arg] = argtype(kwargs[arg]) | ||
return json.dumps(func(*args, **kwargs)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thoughts on using pydantic? See this example https://pydantic-docs.helpmanual.io/usage/validation_decorator/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pratapvardhan this is great... I can offload all the typechecking / casting off to pydantic.validate_arguments
.
Type validation should be on by default, and in that case, pydantic should do it. But users should also be able to turn it off if required, like:
@add_handler(validate=False)
def slow_sum(*numbers: float, delay: int):
s = 0
for i in numbers:
time.sleep(delay)
s += 0
yield s
This ^ snippet won't complain when numbers
is not floats, say.
@sanand0 Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No inputs -- and we can defer this.
@sanand0 items from the last review are done. Some notes on usage are in the docstring for |
pattern: /$YAMLURL/greet
handler: Function
kwargs:
function: greet.birthday
def total_list(items: List[float], start: float) -> float:
return sum(items) + start
|
Also allow POSTing queryparams and path args
These scenarios don't yet work. url:
power:
pattern: /power
handler: FunctionHandler
kwargs:
function: alpha.power
kwargs:
x: 3
message:
pattern: /message
handler: FunctionHandler
kwargs:
function: alpha.messages @handler
def power(x: int, y:float) -> float:
return y ** x
@handler
def messages(s: list):
for msg in s:
yield msg |
@sanand0 Added these two cases, and tests for them. |
I made these changes:
|
Note: Checks failed because Travis couldn't access the Microsoft R repository. Even master fails despite the same commit passing earlier. This is because So I've gone ahead and merged. |
See #304
This PR adds a decorator:
gramex.handlers.functionhandler.add_handler
which wraps any callable to:ToDo:
Not exhaustively tested for POST / DELETE