generated from bmstu-rsoi/lab1-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
66 lines (51 loc) · 2 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
from flask import Flask, request, make_response
from src.personInterface import Person
app = Flask(__name__)
@app.route("/")
def index():
return "LR1 PERSONS"
@app.route('/api/v1/persons/<int:person_id>', methods=["GET"])
def get_person(person_id):
person = Person()
person_json = person.get_person(person_id)
if person_json is None:
return make_response(f"Not found Person for ID {person_id}", 400)
response = make_response(person_json, 200)
response.headers['Content-Type'] = 'application/json'
return response
@app.route('/api/v1/persons', methods=["GET"])
def get_all_person():
person = Person()
persons_json = person.get_all_persons()
response = make_response(persons_json, 200)
response.headers['Content-Type'] = 'application/json'
return response
@app.route('/api/v1/persons', methods=["POST"])
def post_person():
new_person = request.json
person = Person()
person_id = person.create_person(new_person)
if person_id is None:
return make_response('Invalid data', 400)
return '', 201, {'location': f'{request.host_url}api/v1/persons/{int(person_id)}'}
@app.route('/api/v1/persons/<int:person_id>', methods=["PATCH"])
def patch_person(person_id):
new_person = request.json
person = Person()
code = person.update_person(new_person, person_id)
if code:
return make_response(f'Not found Person for ID {person_id}', 404)
person_json = person.get_person(person_id)
response = make_response(person_json, 200)
response.headers['Content-Type'] = 'application/json'
return response
@app.route('/api/v1/persons/<int:person_id>', methods=["DELETE"])
def delete_person(person_id):
person = Person()
answer = person.delete_person(person_id)
if answer == 0:
return make_response(f'Person for ID {person_id} not found', 404)
return make_response(f'Person for ID {person_id} was removed', 204)
if __name__ == '__main__':
from waitress import serve
serve(app, host="0.0.0.0", port=3000)