-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
74 lines (57 loc) · 2.26 KB
/
api.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
from flask import Flask, request, jsonify
from flask_restful import Api, Resource
from tasks import create_task, read_all_tasks, update_task, delete_task, get_task_by_id
app = Flask(__name__)
api = Api(app)
class TaskResource(Resource):
def get(self, task_id=None):
if task_id is not None:
task_data = get_task_by_id(task_id)
if task_data:
return jsonify(task_data)
else:
return jsonify({'message': f'Task with ID {task_id} not found'}), 404
tasks = read_all_tasks()
response_data = [
{
'id': task.id,
'name': task.name,
'status': task.status,
'recurring': 'yes' if task.recurring else 'no',
'cron_schedule': task.cron_schedule,
'execution_time': task.execution_time.strftime('%Y-%m-%d %H:%M:%S')
}
for task in tasks
]
return jsonify(response_data)
def post(self):
data = request.get_json()
name = data.get('name')
execution_time = data.get('execution_time')
id = data.get('id')
cron_schedule = data.get('cron_schedule')
task = create_task(id, name, execution_time, cron_schedule)
response_data = {
'id': task.id,
'name': task.name,
'status': task.status,
'recurring': task.recurring
}
if task.recurring:
response_data['cron_schedule'] = task.cron_schedule
else:
response_data['execution_time'] = task.execution_time.strftime('%Y-%m-%d %H:%M:%S')
return response_data, 201
def put(self, task_id):
data = request.get_json()
name = data.get('name')
execution_time = data.get('execution_time')
cron_schedule= data.get('cron_schedule')
update_task(task_id, name, execution_time,cron_schedule)
return {'message': f'Task {task_id} updated successfully'}
def delete(self, task_id):
delete_task(task_id)
return {'message': f'Task {task_id} deleted successfully'}
api.add_resource(TaskResource, '/tasks', '/tasks/<int:task_id>')
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0')