-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
104 lines (80 loc) · 3.51 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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,
"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)
@app.route("/shows", methods=['GET'])
def get_all_shows():
return create_response({"shows": db.get('shows')})
@app.route("/shows/<id>", methods=['DELETE'])
def delete_show(id):
if db.getById('shows', int(id)) is None:
return create_response(status=404, message="No show with this id exists")
db.deleteById('shows', int(id))
return create_response(message="Show deleted")
# TODO: Implement the rest of the API here!
@app.route("/<id>", methods=['GET'])
def get_show(id):
if db.getById('shows', int(id)) is None:
return create_response(status=404, message="No show with this id exists")
return create_response({'shows': db.getById('shows', int(id))})
@app.route("/shows", methods=['POST'])
def create_show():
data = request.get_json()
if (data["name"] == None) or (data["episodes_seen"] == None):
return create_response(status=422, message="Make sure you enter a valid show name and integer for episodes_seen")
content = {"name": data["name"], "episodes_seen": data["episodes_seen"]}
db.create('shows', content)
id_val = max([i["id"] for i in 'shows'])
return create_response({'shows': db.getById('shows', id_val)})
#@app.route("/shows/<id>", methods=['PUT'])
#def update_show(id):
# data = request.get_json()
# if (data["name"] == None) or (data["episodes_seen"] == None):
# return create_response(status=422, message="Make sure you enter a valid show name and integer for episodes_seen")
# if db.getById('shows', int(id)) is None:
# return create_response(status=404, message="No show with this id exists")
# if (db.getById('shows', int(id))["name"] == data["name"]) and (db.getById('shows', int(id))["episodes_seen"] == data["episodes_seen"])
# db.updateById('shows', id, data)
# return create_response({'shows': db.getById('shows', int(id))})
# return create_response(status=422, message="Make sure you enter a valid show name and integer for episodes_seen")
"""
~~~~~~~~~~~~ END API ~~~~~~~~~~~~
"""
if __name__ == "__main__":
app.run(port=8080, debug=True)