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

add route decorators #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/japronto/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,36 @@ def __finalize(self):
self._reaper = Reaper(self, **self._reaper_settings)
self._matcher = self._router.get_matcher()

def route(self, path, methods=["GET"]):
'''
Shorthand route decorator. Avoids need to register
handlers to the router directly with `app.router.add_route()`.
'''
def decorator(handler):
def wrapper(*args, **kwargs):
return handler(*args, **kwargs)
self.router.add_route(path, wrapper, methods=methods)
return wrapper
return decorator

def get(self, path):
return self.route(path, methods=["GET"])

def post(self, path):
return self.route(path, methods=["POST"])

def put(self, path):
return self.route(path, methods=["PUT"])

def patch(self, path):
return self.route(path, methods=["PATCH"])

def options(self, path):
return self.route(path, methods=["OPTIONS"])

def delete(self, path):
return self.route(path, methods=["DELETE"])

def protocol_error_handler(self, error):
print(error)

Expand Down