-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.py
More file actions
51 lines (44 loc) · 1.32 KB
/
app.py
File metadata and controls
51 lines (44 loc) · 1.32 KB
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
from flask import Flask, jsonify, request
import mockdb.mockdb_interface as db
app = Flask(__name__)
def create_response(data={}, status=200, message=''):
"""
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
"""
response = {
'success': 200 <= status < 300,
'code': status,
'message': message,
'result': data
}
return jsonify(response), status
"""
~~~~~~~~~~~~ API ~~~~~~~~~~~~
"""
@app.route('/users', methods = ['GET'])
def users():
if request.method == 'GET':
jsondata = db.get('users')
return create_response({'users': jsondata})
@app.route('/')
def hello_world():
return create_response('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)