-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_backend.py
72 lines (68 loc) · 1.78 KB
/
sample_backend.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
from flask import Flask
from flask import request
from flask import jsonify
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, world!'
users = {
'users_list' :
[
{
'id' : 'xyz789',
'name' : 'Charlie',
'job': 'Janitor',
},
{
'id' : 'abc123',
'name': 'Mac',
'job': 'Bouncer',
},
{
'id' : 'ppp222',
'name': 'Mac',
'job': 'Professor',
},
{
'id' : 'yat999',
'name': 'Dee',
'job': 'Aspring actress',
},
{
'id' : 'zap555',
'name': 'Dennis',
'job': 'Bartender',
}
]
}
@app.route('/users', methods=['GET', 'POST', 'DELETE'])
def get_users():
if request.method == 'GET':
search_username = request.args.get('name')
if search_username :
subdict = {'users_list' : []}
for user in users['users_list']:
if user['name'] == search_username:
subdict['users_list'].append(user)
return subdict
return users
elif request.method == 'POST':
userToAdd = request.get_json()
users['users_list'].append(userToAdd)
resp = jsonify(success=True)
#resp.status_code = 200 #optionally, you can always set a response code.
# 200 is the default code for a normal response
return resp
elif request.method == 'DELETE':
userToDelete = request.get_json()
users['users_list'].remove(userToDelete)
resp = jsonify(success=True)
return resp
@app.route('/users/<id>')
def get_user(id):
if id:
for user in users['users_list']:
if user['id'] == id:
return user
return ({})
return users