python flask restful api_使用 Python 和 Flask 设计 RESTful API

使用 Python 和 Flask 实现 RESTful services

使用 Flask 构建 web services 是十分简单地,比我在 Mega-Tutorial 中构建的完整的服务端的应用程序要简单地多。

在 Flask 中有许多扩展来帮助我们构建 RESTful services,但是在我看来这个任务十分简单,没有必要使用 Flask 扩展。

我们 web service 的客户端需要添加、删除以及修改任务的服务,因此显然我们需要一种方式来存储任务。最直接的方式就是建立一个小型的数据库,但是数据库并不是本文的主体。学习在 Flask 中使用合适的数据库,我强烈建议阅读 Mega-Tutorial。

这里我们直接把任务列表存储在内存中,因此这些任务列表只会在 web 服务器运行中工作,在结束的时候就失效。 这种方式只是适用我们自己开发的 web 服务器,不适用于生产环境的 web 服务器, 这种情况一个合适的数据库的搭建是必须的。

我们现在来实现 web service 的第一个入口:

#!flask/bin/python

from flask import Flask, jsonify

app = Flask(__name__)

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

}

]

@app.route('/todo/api/v1.0/tasks', methods=['GET'])

def get_tasks():

return jsonify({'tasks': tasks})

if __name__ == '__main__':

app.run(debug=True)

正如你所见,没有多大的变化。我们创建一个任务的内存数据库,这里无非就是一个字典和数组。数组中的每一个元素都具有上述定义的任务的属性。

取代了首页,我们现在拥有一个 get_tasks 的函数,访问的 URI 为 /todo/api/v1.0/tasks,并且只允许 GET 的 HTTP 方法。

这个函数的响应不是文本,我们使用 JSON 数据格式来响应,Flask 的 jsonify 函数从我们的数据结构中生成。

使用网页浏览器来测试我们的 web service 不是一个最好的注意,因为网页浏览器上不能轻易地模拟所有的 HTTP 请求的方法。相反,我们会使用 curl。如果你还没有安装 curl 的话,请立即安装它。

通过执行 app.py,启动 web service。接着打开一个新的控制台窗口,运行以下命令:

$ curl -i http://localhost:5000/todo/api/v1.0/tasks

HTTP/1.0 200 OK

Content-Type: application/json

Content-Length: 294

Server: Werkzeug/0.8.3 Python/2.7.3

Date: Mon, 20 May 2013 04:53:53 GMT

{

"tasks": [

{

"description": "Milk, Cheese, Pizza, Fruit, Tylenol",

"done": false,

"id": 1,

"title": "Buy groceries"

},

{

"description": "Need to find a good Python tutorial on the web",

"done": false,

"id": 2,

"title": "Learn Python"

}

]

}

我们已经成功地调用我们的 RESTful service 的一个函数!

现在我们开始编写 GET 方法请求我们的任务资源的第二个版本。这是一个用来返回单独一个任务的函数:

from flask import abort

@app.route('/todo/api/v1.0/tasks/', methods=['GET'])

def get_task(task_id):

task = filter(lambda t: t['id'] == task_id, tasks)

if len(task) == 0:

abort(404)

return jsonify({'task': task[0]})

第二个函数有些意思。这里我们得到了 URL 中任务的 id,接着 Flask 把它转换成 函数中的 task_id 的参数。

我们用这个参数来搜索我们的任务数组。如果我们的数据库中不存在搜索的 id,我们将会返回一个类似 404 的错误,根据 HTTP 规范的意思是 “资源未找到”。

如果我们找到相应的任务,那么我们只需将它用 jsonify 打包成 JSON 格式并将其发送作为响应,就像我们以前那样处理整个任务集合。

调用 curl 请求的结果如下:

$ curl -i http://localhost:5000/todo/api/v1.0/tasks/2

HTTP/1.0 200 OK

Content-Type: application/json

Content-Length: 151

Server: Werkzeug/0.8.3 Python/2.7.3

Date: Mon, 20 May 2013 05:21:50 GMT

{

"task": {

"description": "Need to find a good Python tutorial on the web",

"done": false,

"id": 2,

"title": "Learn Python"

}

}

$ curl -i http://localhost:5000/todo/api/v1.0/tasks/3

HTTP/1.0 404 NOT FOUND

Content-Type: text/html

Content-Length: 238

Server: Werkzeug/0.8.3 Python/2.7.3

Date: Mon, 20 May 2013 05:21:52 GMT

404 Not Found

Not Found

The requested URL was not found on the server.

If you entered the URL manually please check your spelling and try again.

当我们请求 id #2 的资源时候,我们获取到了,但是当我们请求 #3 的时候返回了 404 错误。有关错误奇怪的是返回的是 HTML 信息而不是 JSON,这是因为 Flask 按照默认方式生成 404 响应。由于这是一个 Web service 客户端希望我们总是以 JSON 格式回应,所以我们需要改善我们的 404 错误处理程序:

from flask import make_response

@app.errorhandler(404)

def not_found(error):

return make_response(jsonify({'error': 'Not found'}), 404)

我们会得到一个友好的错误提示:

$ curl -i http://localhost:5000/todo/api/v1.0/tasks/3

HTTP/1.0 404 NOT FOUND

Content-Type: application/json

Content-Length: 26

Server: Werkzeug/0.8.3 Python/2.7.3

Date: Mon, 20 May 2013 05:36:54 GMT

{

"error": "Not found"

}

接下来就是 POST 方法,我们用来在我们的任务数据库中插入一个新的任务:

from flask import request

@app.route('/todo/api/v1.0/tasks', methods=['POST'])

def create_task():

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

添加一个新的任务也是相当容易地。只有当请求以 JSON 格式形式,request.json 才会有请求的数据。如果没有数据,或者存在数据但是缺少 title 项,我们将会返回 400,这是表示请求无效。

接着我们会创建一个新的任务字典,使用最后一个任务的 id + 1 作为该任务的 id。我们允许 description 字段缺失,并且假设 done 字段设置成 False。

我们把新的任务添加到我们的任务数组中,并且把新添加的任务和状态 201 响应给客户端。

使用如下的 curl 命令来测试这个新的函数:

$ curl -i -H "Content-Type: application/json" -X POST -d '{"title":"Read a book"}' http://localhost:5000/todo/api/v1.0/tasks

HTTP/1.0 201 Created

Content-Type: application/json

Content-Length: 104

Server: Werkzeug/0.8.3 Python/2.7.3

Date: Mon, 20 May 2013 05:56:21 GMT

{

"task": {

"description": "",

"done": false,

"id": 3,

"title": "Read a book"

}

}

注意:如果你在 Windows 上并且运行 Cygwin 版本的 curl,上面的命令不会有任何问题。然而,如果你使用原生的 curl,命令会有些不同:

curl -i -H "Content-Type: application/json" -X POST -d "{"""title""":"""Read a book"""}" http://localhost:5000/todo/api/v1.0/tasks

当然在完成这个请求后,我们可以得到任务的更新列表:

$ curl -i http://localhost:5000/todo/api/v1.0/tasks

HTTP/1.0 200 OK

Content-Type: application/json

Content-Length: 423

Server: Werkzeug/0.8.3 Python/2.7.3

Date: Mon, 20 May 2013 05:57:44 GMT

{

"tasks": [

{

"description": "Milk, Cheese, Pizza, Fruit, Tylenol",

"done": false,

"id": 1,

"title": "Buy groceries"

},

{

"description": "Need to find a good Python tutorial on the web",

"done": false,

"id": 2,

"title": "Learn Python"

},

{

"description": "",

"done": false,

"id": 3,

"title": "Read a book"

}

]

}

剩下的两个函数如下所示:

@app.route('/todo/api/v1.0/tasks/', methods=['PUT'])

def update_task(task_id):

task = filter(lambda t: t['id'] == task_id, tasks)

if len(task) == 0:

abort(404)

if not request.json:

abort(400)

if 'title' in request.json and type(request.json['title']) != unicode:

abort(400)

if 'description' in request.json and type(request.json['description']) is not unicode:

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/', methods=['DELETE'])

def delete_task(task_id):

task = filter(lambda t: t['id'] == task_id, tasks)

if len(task) == 0:

abort(404)

tasks.remove(task[0])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值