-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
49 lines (33 loc) · 1.17 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
from flask import Flask, request, jsonify, make_response
from app_service import AppService
import json
app = Flask(__name__)
appService = AppService();
def tasks_to_json_rep(tasks):
'''
Retourne une répouse HTTP au format JSON à partir d'une liste de tâches
tasks : Liste de tâches au format dictionnaire
return : réponse HTML JSON
'''
response = make_response(jsonify(tasks),401)
response.headers["Content-Type"] = "application/json"
return response
@app.route('/')
def home():
return "App Works!!!"
@app.route('/api/tasks')
def tasks():
return tasks_to_json_rep(appService.get_tasks())
@app.route('/api/task', methods=['POST'])
def create_task():
# On récupère les resources au fromat JSON dans un dictionnaire
task = request.get_json()
print(type(task), task, task['id'])
return tasks_to_json_rep(appService.create_task(task))
@app.route('/api/task', methods=['PUT'])
def update_task():
task = request.get_json()
return tasks_to_json_rep(appService.update_task(task))
@app.route('/api/task/<int:id>', methods=['DELETE'])
def delete_task(id):
return tasks_to_json_rep(appService.delete_task(id))