-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
75 lines (64 loc) · 2.03 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
67
68
69
70
71
72
73
74
75
from flask import Flask, jsonify, request
from flask import url_for
from worker import celery
import celery.states as states
app = Flask(__name__)
BOOKS = [
{
'title': 'On the Road',
'author': 'Jack Kerouac',
'read': True
},
{
'title': 'Harry Potter and the Philosopher\'s Stone',
'author': 'J. K. Rowling',
'read': False
},
{
'title': 'Green Eggs and Ham',
'author': 'Dr. Seuss',
'read': True
}
]
@app.route('/add/<int:param1>/<int:param2>')
def add(param1: int, param2: int) -> str:
task = celery.send_task('tasks.add', args=[param1, param2], kwargs={})
response = f"<a href='{url_for('check_task', task_id=task.id, external=True)}'>check status of {task.id} </a>"
return response
@app.route('/check/<string:task_id>')
def check_task(task_id: str) -> str:
res = celery.AsyncResult(task_id)
if res.state == states.PENDING:
return res.state
else:
return str(res.result)
@app.route('/books', methods=['GET', 'POST'])
def all_books():
response_object = {'status': 'success'}
if request.method == 'POST':
post_data = request.get_json()
BOOKS.append({
'title': post_data.get('title'),
'author': post_data.get('author'),
'read': post_data.get('read')
})
response_object['message'] = 'Book added!'
else:
response_object['books'] = BOOKS
return jsonify(response_object)
@app.route('/books3', methods=['GET', 'POST'])
def all_books2():
response_object = {'status': 'success'}
if request.method == 'POST':
post_data = request.get_json()
BOOKS.append({
'title': post_data.get('title'),
'author': post_data.get('author'),
'read': post_data.get('read')
})
response_object['message'] = 'Book added!'
else:
response_object['books'] = BOOKS
return jsonify(response_object)
if __name__ == '__main__':
app.run(debug = True, host = '0.0.0.0', port=5001)