Flask+MongoDB实现的REST API示例

FlaskPython的一款轻量级Web开发框架,MongoDB是一款高性能,开源的NoSQL数据库.

转自老外的教程,我做了数据库验证登录的修改,别的没动,在部署运行这篇文章之前你需要创建一个包含以下数据库信息的数据库:

数据库名: restdb; 用户名: rest; 密码: towait.com; 集合: stars

不知道怎么创建的同学可以参考前文:

核心文件 mongo.py

from flask import Flask, jsonify, request
from flask_pymongo import PyMongo
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

app.config['MONGO_DBNAME'] = 'restdb'

app.config['MONGO_URI'] = 'mongodb://rest:[email protected]:27017/restdb'

app.url_map.strict_slashes = False # Disable redirecting on POST method from /star to /star/

mongo = PyMongo(app)

class Star(Resource):
    def get(self, name):
        star = mongo.db.stars
        s = star.find_one({'name' : name})
        if s:
            output = {'name' : s['name'], 'distance' : s['distance']}
        else:
            output = "No such name"
        return jsonify({'result' : output})

class StarList(Resource):
    def get(self):
        star = mongo.db.stars
        output = []
        for s in star.find():
            output.append({'name' : s['name'], 'distance' : s['distance']})
        return jsonify({'result' : output})

    def post(self):
        star = mongo.db.stars
        name = request.json['name']
        distance = request.json['distance']
        star_id = star.insert({'name': name, 'distance': distance})
        new_star = star.find_one({'_id': star_id })
        output = {'name' : new_star['name'], 'distance' : new_star['distance']}
        return jsonify({'result' : output})

api.add_resource(StarList, '/star')
api.add_resource(Star, '/star/<string:name>')

if __name__ == '__main__':
    app.run(debug=True)

REST API的调试

推荐使用Chrome插件Advanced REST client

参考资料:

Post Comment