forked from hack4impact-uiuc/flask-exercise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
61 lines (45 loc) · 1.58 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
from typing import Tuple
from flask import Flask, jsonify, request, Response
import mockdb.mockdb_interface as db
app = Flask(__name__)
def create_response(
data: dict = None, status: int = 200, message: str = ""
) -> Tuple[Response, int]:
"""Wraps response in a consistent format throughout the API.
Format inspired by https://medium.com/@shazow/how-i-design-json-api-responses-71900f00f2db
Modifications included:
- make success a boolean since there's only 2 values
- make message a single string since we will only use one message per response
IMPORTANT: data must be a dictionary where:
- the key is the name of the type of data
- the value is the data itself
:param data <str> optional data
:param status <int> optional status code, defaults to 200
:param message <str> optional message
:returns tuple of Flask Response and int, which is what flask expects for a response
"""
if type(data) is not dict and data is not None:
raise TypeError("Data should be a dictionary 😞")
response = {
"code": status,
"success": 200 <= status < 300,
"message": message,
"result": data,
}
return jsonify(response), status
"""
~~~~~~~~~~~~ API ~~~~~~~~~~~~
"""
@app.route("/")
def hello_world():
return create_response({"content": "hello world!"})
@app.route("/mirror/<name>")
def mirror(name):
data = {"name": name}
return create_response(data)
# TODO: Implement the rest of the API here!
"""
~~~~~~~~~~~~ END API ~~~~~~~~~~~~
"""
if __name__ == "__main__":
app.run(debug=True)