-
-
Notifications
You must be signed in to change notification settings - Fork 388
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
180ee00
commit 5d51ab4
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""This example demonstrates how to perform different kinds of redirects using hug""" | ||
import hug | ||
|
||
|
||
@hug.get() | ||
def sum_two_numbers(number_1: int, number_2: int): | ||
"""I'll be redirecting to this using a variety of approaches below""" | ||
return number_1 + number_2 | ||
|
||
|
||
@hug.post() | ||
def internal_redirection_automatic(number_1: int, number_2: int): | ||
"""This will redirect internally to the sum_two_numbers handler | ||
passing along all passed in parameters. | ||
Redirect happens within internally within hug, fully transparent to clients. | ||
""" | ||
print("Internal Redirection Automatic {}, {}".format(number_1, number_2)) | ||
return sum_two_numbers | ||
|
||
|
||
@hug.post() | ||
def internal_redirection_manual(number: int): | ||
"""Instead of normal redirecting: You can manually call other handlers, with computed parameters | ||
and return their results | ||
""" | ||
print("Internal Redirection Manual {}".format(number)) | ||
return sum_two_numbers(number, number) | ||
|
||
|
||
@hug.post() | ||
def redirect(redirect_type: hug.types.one_of((None, "permanent", "found", "see_other")) = None): | ||
"""Hug also fully supports classical HTTP redirects, | ||
providing built in convenience functions for the most common types. | ||
""" | ||
print("HTTP Redirect {}".format(redirect_type)) | ||
if not redirect_type: | ||
hug.redirect.to("/sum_two_numbers") | ||
else: | ||
getattr(hug.redirect, redirect_type)("/sum_two_numbers") |