Skip to content

Commit 5d51ab4

Browse files
Add redirects example
1 parent 180ee00 commit 5d51ab4

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

examples/redirects.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""This example demonstrates how to perform different kinds of redirects using hug"""
2+
import hug
3+
4+
5+
@hug.get()
6+
def sum_two_numbers(number_1: int, number_2: int):
7+
"""I'll be redirecting to this using a variety of approaches below"""
8+
return number_1 + number_2
9+
10+
11+
@hug.post()
12+
def internal_redirection_automatic(number_1: int, number_2: int):
13+
"""This will redirect internally to the sum_two_numbers handler
14+
passing along all passed in parameters.
15+
16+
Redirect happens within internally within hug, fully transparent to clients.
17+
"""
18+
print("Internal Redirection Automatic {}, {}".format(number_1, number_2))
19+
return sum_two_numbers
20+
21+
22+
@hug.post()
23+
def internal_redirection_manual(number: int):
24+
"""Instead of normal redirecting: You can manually call other handlers, with computed parameters
25+
and return their results
26+
"""
27+
print("Internal Redirection Manual {}".format(number))
28+
return sum_two_numbers(number, number)
29+
30+
31+
@hug.post()
32+
def redirect(redirect_type: hug.types.one_of((None, "permanent", "found", "see_other")) = None):
33+
"""Hug also fully supports classical HTTP redirects,
34+
providing built in convenience functions for the most common types.
35+
"""
36+
print("HTTP Redirect {}".format(redirect_type))
37+
if not redirect_type:
38+
hug.redirect.to("/sum_two_numbers")
39+
else:
40+
getattr(hug.redirect, redirect_type)("/sum_two_numbers")

0 commit comments

Comments
 (0)