-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex_05_rest_simple_dataclass.py
64 lines (51 loc) · 1.68 KB
/
ex_05_rest_simple_dataclass.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
from flask import Flask, request,jsonify
from flask_restful import Resource, Api
from dataclasses import dataclass
app = Flask(__name__)
api = Api(app) #Die Flask API
'''
Für die Serialisierung werden die Werte name, age verwendet, comment nicht
'''
@dataclass
class Person:
id: int
name : str
age : int
def __init__(self, id, name, age, comment):
self.id = id
self.name = name
self.age = age
self.comment = comment
persons = {0 : Person(0, "Hubert", 30, "comment 1"),
1 : Person(1, "Herbert", 40, "comment 2")}
class PersonService(Resource):
def get(self, id):
print("get")
print(persons)
if id in persons:
return jsonify(persons[id])
return {"Message" : "%s not found" % id}
def put(self, id):
data = request.get_json(force=True)
persons[id] = Person(data['id'], data['name'], data['age'], data['comment'])
print("put")
print(persons)
return {"Message": "%s gespeichert" % id}
def delete(self, id):
del persons[id]
return {"Message": "%s gelöscht" % id}
def patch(self, id):
data = request.get_json(force=True)
print('patch')
if 'comment' in data:
print('comment')
persons[id].comment = data['comment']
if 'age' in data:
persons[id].age = data['age']
if 'name' in data:
persons[id].name = data['name']
return {"Message": "%d gepatched" % id}
#Hier passiert das Mapping auf die Klasse
api.add_resource(PersonService, '/person/<int:id>')
if __name__ == '__main__':
app.run(debug=True) #debug=True lädt nach den Änderungen neu