add python_restful_api.py

master
xianhu 2016-12-29 19:00:43 +08:00
parent 4f3b98f6d9
commit 3c0ddf3530
2 changed files with 77 additions and 0 deletions

View File

@ -35,6 +35,8 @@
### python_version36.py: Python3.6正式版要来了, 你期待哪些新特性? ### python_version36.py: Python3.6正式版要来了, 你期待哪些新特性?
### python_magic_methods: Python进阶: 实例讲解Python中的魔法函数(Magic Methods) ### python_magic_methods: Python进阶: 实例讲解Python中的魔法函数(Magic Methods)
### python_restful_api.py: 利用Python和Flask快速开发RESTful API
=================================================================================================== ===================================================================================================
### 您可以fork该项目, 并在修改后提交Pull request ### 您可以fork该项目, 并在修改后提交Pull request

View File

@ -0,0 +1,75 @@
# _*_ coding: utf-8 _*_
"""
python_restful_api.py by xianhu
"""
from flask import Flask
from flask_restful import reqparse, abort, Api, Resource
app = Flask(__name__)
api = Api(app)
ITEMS = {
'item1': {'name': 'Allen', 'age': 19},
'item2': {'name': 'Lily', 'age': 18},
'item3': {'name': 'James', 'age': 20},
}
def abort_if_item_doesnt_exist(item_id):
if item_id not in ITEMS:
abort(404, message="Item {} doesn't exist".format(item_id))
def get_new_item_id():
for key in ITEMS:
item_id = 'item' + str(int(key.strip('item')) + 1)
if item_id not in ITEMS:
return item_id
parser = reqparse.RequestParser()
parser.add_argument('name', type=str, required=True, help='need name data')
parser.add_argument('age', type=int, required=True, help='need age data')
# 操作put / get / delete单一资源
class Todo(Resource):
def put(self, item_id):
args = parser.parse_args()
item = {'name': args['name'], 'age': args['age']}
ITEMS[item_id] = item
return item, 201
def get(self, item_id):
abort_if_item_doesnt_exist(item_id)
return ITEMS[item_id], 200
def delete(self, item_id):
abort_if_item_doesnt_exist(item_id)
del ITEMS[item_id]
return '', 204
# 操作post / get资源列表
class TodoList(Resource):
def get(self):
return ITEMS, 200
def post(self):
args = parser.parse_args()
item_id = get_new_item_id()
ITEMS[item_id] = {'name': args['name'], 'age': args['age']}
return ITEMS[item_id], 201
# 设置路由
api.add_resource(TodoList, '/items')
api.add_resource(Todo, '/items/<item_id>')
if __name__ == '__main__':
app.run(debug=True)