-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
73 lines (57 loc) · 1.87 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import requests
import json
import gzip
from chalice import Chalice, Response
from chalicelib.helpers import compress_response
app = Chalice(app_name="intro-to-serverless-with-aws-chalice-demo")
app.api.binary_types.append("application/json")
@app.route("/")
def index():
return {"hello": "world"}
@app.route("/authors")
def authors():
r = requests.get(
"https://fakerestapi.azurewebsites.net/api/Authors",
headers={"content-type": "application/json"},
)
result = r.json() * 50
return result
@app.route("/authors/compress")
def authors_compress():
r = requests.get(
"https://fakerestapi.azurewebsites.net/api/Authors",
headers={"content-type": "application/json"},
)
result = r.json() * 50
blob = json.dumps(result).encode("utf-8")
payload = gzip.compress(blob)
custom_headers = {"Content-Type": "application/json", "Content-Encoding": "gzip"}
return Response(body=payload, status_code=200, headers=custom_headers)
@app.route("/authors/compress-with-decorator")
@compress_response
def authors_compress_with_decorator():
r = requests.get(
"https://fakerestapi.azurewebsites.net/api/Authors",
headers={"content-type": "application/json"},
)
result = r.json() * 50
return result
# The view function above will return {"hello": "world"}
# whenever you make an HTTP GET request to '/'.
#
# Here are a few more examples:
#
# @app.route('/hello/{name}')
# def hello_name(name):
# # '/hello/james' -> {"hello": "james"}
# return {'hello': name}
#
# @app.route('/users', methods=['POST'])
# def create_user():
# # This is the JSON body the user sent in their POST request.
# user_as_json = app.current_request.json_body
# # We'll echo the json body back to the user in a 'user' key.
# return {'user': user_as_json}
#
# See the README documentation for more examples.
#