# Restful API

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
103
104
105
106
107
108
109
110
111
from flask import request
from flask import Flask
from flask import jsonify
from flask import abort
from flask import make_response
# flask_httpauth 需要单独安装
from flask_httpauth import HTTPBasicAuth

auth = HTTPBasicAuth()

app = Flask(__name__)


tasks = [
{
'id': 1,
'title': 'Buy groceries',
'description': 'Milk, Cheese, Pizza, Fruit, Tylenol',
'done': False
},
{
'id': 2,
'title': 'Learn Python',
'description': 'Need to find a good Python tutorial on the web',
'done': False
}
]


@app.route('/')
def index():
return "Hello, World!"


@app.route('/todo/api/v1.0/tasks', methods=['GET'])
@auth.login_required
def get_tasks():
return jsonify({'tasks': tasks})


@app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['GET'])
def get_task(task_id):
task = [task for task in tasks if task['id'] == task_id]
if len(task) == 0:
abort(404)
return jsonify({'task': task[0]})


@app.route('/todo/api/v1.0/tasks', methods=['POST'])
def create_task():
print(request.json)
if not request.json or not 'title' in request.json:
abort(400)
task = {
'id': tasks[-1]['id'] + 1,
'title': request.json['title'],
'description': request.json.get('description', ""),
'done': False
}
tasks.append(task)
return jsonify({'task': task}), 201


@app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['PUT'])
def update_task(task_id):
task = [task for task in tasks if task['id'] == task_id]
if len(task) == 0:
abort(404)
if not request.json:
abort(400)
if 'title' in request.json and type(request.json['title']) != str:
abort(400)
if 'description' in request.json and type(request.json['description']) is not str:
abort(400)
if 'done' in request.json and type(request.json['done']) is not bool:
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'])
return jsonify({'task': task[0]})


@app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['DELETE'])
def delete_task(task_id):
task = [task for task in tasks if task['id'] == task_id]
if len(task) == 0:
abort(404)
tasks.remove(task[0])
return jsonify({'result': True})


@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)


@auth.get_password
def get_password(username):
if username == 'root':
return 'root'
return None


@auth.error_handler
def unauthorized():
return make_response(jsonify({'error': 'Unauthorized access'}), 401)


if __name__ == "__main__":
app.run(host='0.0.0.0', port=8888, debug=True)

# VSCode REST Client Test

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
@uri=http://localhost:9999/todo/api/v1.0/
@json=Content-Type: application/json;charset=UTF-8
@auth=Authorization: Basic root root



### GET all tasks
GET {{uri}}tasks
{{auth}}
###
curl "http://localhost:9999/todo/api/v1.0/tasks" -u root:root -i


### GET one task
GET {{uri}}tasks/1
###
GET {{uri}}tasks/10


### Post one task
POST {{uri}}tasks
{{json}}

{
"title": "Read a book"
}


### Put/Update one task
PUT {{uri}}tasks/1
{{json}}

{
"title": "Learn Flask",
"description": "IIS + Flask + Restful"
}


### Delete one task
DELETE {{uri}}tasks/1

# Requests Test

1
2
3
4
5
6
7
import requests

uri = 'http://localhost:9999/todo/api/v1.0/'
auth = ('root', 'root')

response = requests.get(f'{uri}tasks', auth=auth)
print(response.json())
Edited on