- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
Latest commit
102 lines (89 loc) · 2.91 KB
/
Copy pathapp.py
File metadata and controls
102 lines (89 loc) · 2.91 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
90
91
92
93
94
95
96
97
98
99
100
101
102
fromflaskimportFlask, jsonify, abort, make_response, request
frompip._vendor.appdirsimportunicode
fromflask_httpauthimportHTTPBasicAuth
app=Flask(__name__)
auth=HTTPBasicAuth()
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
}
]
#BASIC AUTH CHECK
@auth.get_password
defget_password(username):
ifusername=='testing':
return'python'
returnNone
@auth.error_handler
defunauthorized():
returnmake_response(jsonify({'error': 'Unauthorized access'}), 401)
@app.route('/')
defindex():
return"Sample REST APIs Building!"
#GET ALL THE TASKS WITH AUTH CHECK
@app.route('/todo/api/v1.0/tasks', methods=['GET'])
@auth.login_required
defget_tasks():
returnjsonify({'tasks': tasks})
#ADD NEW TASK
@app.route('/todo/api/v1.0/tasks', methods=['POST'])
defcreate_task():
ifnotrequest.jsonornot'title'inrequest.json:
abort(400)
task= {
'id': tasks[-1]['id'] +1,
'title': request.json['title'],
'description': request.json.get('description', ""),
'done': False
}
tasks.append(task)
returnjsonify({'task': task}), 201
#SEARCH SPECIFIC TASK WITH ID
@app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['GET'])
defget_task(task_id):
task= [taskfortaskintasksiftask['id'] ==task_id]
iflen(task) ==0:
abort(404)
returnjsonify({'task': task[0]})
#UPDATE TASK WITH GIVEN ID
@app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['PUT'])
defupdate_task(task_id):
task= [taskfortaskintasksiftask['id'] ==task_id]
iflen(task) ==0:
abort(404)
ifnotrequest.json:
abort(400)
if'title'inrequest.jsonandtype(request.json['title']) !=unicode:
abort(400)
if'description'inrequest.jsonandtype(request.json['description']) isnotunicode:
abort(400)
if'done'inrequest.jsonandtype(request.json['done']) isnotbool:
abort(400)
task[0]['title'] =request.json.get('title', task[0]['title'])
task[0]['description'] =request.json.get('description', task[0]['description'])
task[0]['done'] =request.json.get('done', task[0]['done'])
returnjsonify({'task': task[0]})
#DELETE TASK WITH GIVEN ID
@app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['DELETE'])
defdelete_task(task_id):
task= [taskfortaskintasksiftask['id'] ==task_id]
iflen(task) ==0:
abort(404)
tasks.remove(task[0])
returnjsonify({'result': True})
#ERROR HANDLING IN CASE OF 404
@app.errorhandler(404)
defnot_found(error):
returnmake_response(jsonify({'error': 'Not found'}), 404)
#RUN local development server
if__name__=='__main__':
app.run()