烧瓶中rest api的教学蓝图。
rest-api-blueprint的Python项目详细描述
这是对如何最好地使用Python(2.7版)和优秀的micro web框架创建rest api的探索。 Flask。它旨在成为一个教学蓝图 而不是图书馆或公用事业。一个更加平淡无奇和诚实的目标是 提供一个干净的说明,代码ofmy当前(但正在发展)的品味 用python和flask构建的rest api的设计和结构。
范围包括自动测试、文档、身份验证、功能 交换、数据格式、mime类型和unicode。因为重点是rest api 结构和表达式,作用域不包含orm和 模板引擎。
有关讨论和解释,请参见Wiki。否则repository code 权威。
请注意,与其说这是一个库,不如说是一种阅读和复制的方法。 但是,显然有一些部分是有用的(例如bdd steps或lib)。这可以通过作为包安装并导入来实现。
状态
现在已经完成了。见open issues。
快速游览
启动示例应用服务器:
-/code/rest-api-blueprint$ python server.py * Running on http://127.0.0.1:5000/ * Restarting with reloader ...
使用example app:
添加个人详细信息~/code/rest-api-blueprint$ curl -X PUT localhost:5000/v1/people/fred -H 'Content-Type: application/json' -d '{"email": "a@b.c"}' { "status": "ok" }
使用example app:
检索个人详细信息~/code/rest-api-blueprint$ curl -X GET localhost:5000/v1/people/fred -H 'Accept: application/json' { "status": "ok", "result": { "comment": null, "name": "fred", "email": "a@b.c" } }
运行bdd测试(BDD details):
~/code/rest-api-blueprint$ behave Feature: Delete a person # features/delete_person.feature:1 As an API client I want to be able to remove a person Background: Reset and have a valid user # features/delete_person.feature:5 Scenario: Cannot delete a person before they exist # features/delete_person.feature:11 Given I am using version "v1" # features/steps/all.py:14 And I have an empty database # features/steps/all.py:19 And I am a valid API user # features/steps/all.py:27 And I use an Accept header of "application/json" # features/steps/all.py:32 When I send a DELETE request to "people/fred" # features/steps/all.py:101 Then the response status should be "404" # features/steps/all.py:109 And the JSON at path "status" should be "error" # features/steps/all.py:119 And the JSON at path "message" should be "Person does not exist" # features/steps/all.py:119 Scenario: Delete a person # features/delete_person.feature:17 Given I am using version "v1" # features/steps/all.py:14 ...
制作api文档(Doc details):
~/code/rest-api-blueprint$ ./make_apidocs.sh Making output directory... Running Sphinx v1.1.3 loading pickled environment... not yet created building [html]: targets for 2 source files that are out of date updating environment: 2 added, 0 changed, 0 removed reading sources... [100%] people looking for now-outdated files... none found pickling environment... done checking consistency... done preparing documents... done writing output... [100%] people writing additional files... search copying static files... done dumping search index... done dumping object inventory... done build succeeded. Copying ansi stylesheet... done
重定向到联机文档:
~/code/rest-api-blueprint$ curl -X GET localhost:5000/v1/people/fred <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <title>Redirecting...</title> <h1>Redirecting...</h1> <p>You should be redirected automatically to target URL: <a href="static/apidocs/people.html">static/apidocs/people.html</a>. If not click the link.
使用Slumber:
进行交互>>>import slumber >>>api=slumber.API('http://localhost:5000/v1/', append_slash=False) >>>api.people.tim.put({"email": "a@b.c"}) True >>>api.people.tim.get() {u'result': {u'comment': None, u'email': u'a@b.c', u'name': u'tim'}, u'status': u'ok'}
为了提供一个模板打包结构,所有内容都使用 distribute。
运行测试:
python setup.py nosetests
使用pip等构建分发和安装包:
python setup.py sdist
该软件包位于dist/目录中,可以使用
pip install rest-api-blueprint-0.1.tar.gz
在开发过程中安装:
python setup.py develop
或
pip install -e .
(它也将安装任何依赖的软件包。)