-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
106 lines (88 loc) · 2.91 KB
/
server.py
File metadata and controls
106 lines (88 loc) · 2.91 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
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
105
106
from flask import Flask, request
from flask_cors import CORS
from datetime import datetime
import json
app = Flask('api-workshop')
CORS(app)
mock_db = {
'john_cloudcomputing@student.uml.edu': {
'name': 'John CloudComputing',
'major': 'Computer Science',
'year': 'Senior',
}
}
transactions = []
@app.route('/users', methods=['GET', 'POST', 'PATCH', 'DELETE'])
def users_interaction():
email = request.args.get('email')
name = request.args.get('name')
major = request.args.get('major')
year = request.args.get('year')
time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
status_code = None
if request.method == 'GET':
data = {}
if email in mock_db.keys():
data = {
'email': email,
'name': mock_db[email]['name'],
'major': mock_db[email]['major'],
'year': mock_db[email]['year'],
}
status_code = 200
else:
status_code = 404
transactions.append(f'GET for {email} @ {time}, Status: {status_code}')
return app.response_class(
response=json.dumps(data),
status=status_code,
mimetype='application/json'
)
elif request.method == 'POST':
if None not in [email, name, major, year]:
mock_db[email] = {
'name': name,
'major': major,
'year': year,
}
status_code = 201
else:
status_code = 500
transactions.append(f'POST creating {email} @ {time}, Status: {status_code}')
return app.response_class(status=status_code)
elif request.method == 'PATCH':
new_data = {
'name': name,
'major': major,
'year': year,
}
for key in new_data.keys():
if new_data[key] != None:
mock_db[email][key] = new_data[key]
status_code = 200
transactions.append(f'PATCH on {email} @ {time}, Status: {status_code}')
return app.response_class(status=200)
elif request.method == 'DELETE':
if email in mock_db.keys():
mock_db.pop(email)
status_code = 200
else:
status_code = 500
transactions.append(f'DELETE on {email} @ {time}, Status: {status_code}')
return app.response_class(status=status_code)
@app.route('/database', methods=['GET'])
def database_interaction():
data = [{
'email': key,
'name': mock_db[key]['name'],
'major': mock_db[key]['major'],
'year': mock_db[key]['year']
} for key in mock_db.keys()]
transactions_copy = transactions
transactions_copy.reverse()
return app.response_class(
response=json.dumps({'items': data, 'transactions': transactions_copy}),
status=200,
mimetype='application/json'
)
app.run(host='0.0.0.0', port='5000')