forked from aquaomri/todolist_mock_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
189 lines (167 loc) · 3.59 KB
/
server.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from json import dumps
from werkzeug.exceptions import BadRequest
from flask import (
Flask,
Response,
request
)
from utils import get_random_id
app = Flask(__name__)
tasks = {
get_random_id(): {
"task": "write server mock",
"completed": True
}
}
@app.route(
'/tasks',
methods=['GET']
)
def list_tasks():
li = [
{
"id": task_id,
"task": values["task"],
"completed": values["completed"]
} for task_id, values in tasks.iteritems()
]
return Response(
dumps(li),
mimetype='application/json'
)
@app.route(
'/tasks/<task_id>',
methods=['GET']
)
def get_task(task_id):
if task_id in tasks:
return Response(
dumps(tasks[task_id]),
mimetype='application/json'
)
return Response(
'{}',
mimetype='application/json'
), 404
@app.route(
'/tasks',
methods=['POST']
)
def create_task():
try:
data = request.get_json()
except BadRequest:
return Response(
'{}',
mimetype='application/json'
), 400
required_keys = [
'task',
'completed'
]
for required_key in required_keys:
if required_key not in data:
return Response(
'{}',
mimetype='application/json'
), 400
task_id = get_random_id()
tasks[task_id] = {
'task': data['task'],
'completed': data['completed']
}
response = {
'task_id': task_id
}
return Response(
dumps(response),
mimetype='application/json'
)
@app.route(
'/tasks/<task_id>/completed',
methods=['POST']
)
def mark_task_completed(task_id):
if task_id in tasks:
tasks[task_id]["completed"] = True
return Response(
'{}',
mimetype='application/json'
)
return Response(
'{}',
mimetype='application/json'
), 404
@app.route(
'/tasks/<task_id>/incomplete',
methods=['POST']
)
def mark_task_incomplete(task_id):
if task_id in tasks:
tasks[task_id]["completed"] = False
return Response(
'{}',
mimetype='application/json'
)
return Response(
'{}',
mimetype='application/json'
), 404
@app.route(
'/tasks/<task_id>',
methods=['DELETE']
)
def delete_task(task_id):
if task_id in tasks:
del tasks[task_id]
response = {
"task_id": task_id
}
return Response(
dumps(response),
mimetype='application/json'
)
return Response(
'{}',
mimetype='application/json'
), 404
@app.route(
'/tasks/<task_id>',
methods=['PUT']
)
def modify_task(task_id):
if task_id not in tasks:
return Response(
'{}',
mimetype='application/json'
), 404
try:
data = request.get_json()
except BadRequest:
return Response(
'{}',
mimetype='application/json'
), 400
required_keys = [
'task',
'completed'
]
for required_key in required_keys:
if required_key not in data:
return Response(
'{}',
mimetype='application/json'
), 400
tasks[task_id] = {
'task': data['task'],
'completed': data['completed']
}
response = {
'task_id': task_id
}
return Response(
dumps(response),
mimetype='application/json'
)
if __name__ == "__main__":
pass