-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
89 lines (78 loc) · 2.41 KB
/
app.py
File metadata and controls
89 lines (78 loc) · 2.41 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
from flask import Flask, jsonify, request
from flasgger import Swagger
from flasgger.utils import swag_from
from flask_cors import CORS
def main():
"""The main function for this script."""
app.run(host='0.0.0.0', port='443', debug=True)
CORS(app)
app = Flask(__name__)
app.config['SWAGGER'] = {
"title": "Tasks API",
"headers": [
('Access-Control-Allow-Origin', '*'),
('Access-Control-Allow-Methods', "GET, POST, PUT, DELETE, OPTIONS"),
('Access-Control-Allow-Credentials', "false"),
],
"info": {
"title": "Tasks API",
"description": "Manage todo tasks",
"contact": {
"responsibleOrganization": "ME",
"responsibleDeveloper": "Me",
"email": "me@me.com",
"url": "www.me.com",
},
"termsOfService": "http://me.com/terms",
"version": "0.0.1"
},
"schemes": [
"https",
"http"
]
}
Swagger(app)
tasks = [
{
'id': 1,
'title': u'Buy groceries',
'description': u'Milk, Cheese, Pizza, Fruit, Tylenol',
'done': False
},
{
'id': 2,
'title': u'Learn Python',
'description': u'Need to find a good Python tutorial on the web',
'done': False
}
]
@app.route('/todo/api/v1.0/tasks', methods=['GET'])
@swag_from('get_tasks.yml')
def get_tasks():
return jsonify({'tasks': tasks})
@app.route('/todo/api/v1.0/tasks_byID', methods=['GET'])
@swag_from('get_tasks_byID.yml')
def get_tasks_byID():
try:
task_id = int(request.args.get('task_id'))
done = True if request.args.get('done',False) == 'true' else False
task = [task for task in tasks if task['id'] == task_id and task['done'] == done]
return jsonify({'task': task[0]})
except Exception as e:
return jsonify({'message': str(e), 'task' : []})
@app.route('/todo/api/v1.0/tasks_byID', methods=['POST'])
@swag_from('post_task.yml')
def post_task():
try:
task = {
'id': tasks[-1]['id'] + 1,
'title': str(request.args.get('title')),
'description': str(request.args.get('description',"")),
'done': False
}
tasks.append(task)
return jsonify({"status": "New task added", "task": tasks[-1]})
except Exception as e:
return jsonify({'message': str(e), 'task' : []})
if __name__== '__main__':
main()