Flask中REST API的教学蓝图。
项目描述
这是对如何使用Python(版本2.7)和优秀的微网页框架Flask创建REST API的探索。它旨在成为教学蓝图而不是库或实用程序。更简单、更诚实的目标声明是,提供一份关于使用Python和Flask构建的REST API设计和结构的当前(但正在演变)喜好的清晰陈述。
范围包括自动测试、文档、身份验证、功能切换、数据格式、MIME类型和Unicode。由于重点是REST API结构和表达,因此范围不包括ORM和模板引擎等事物。
请注意,这不仅仅是一个库,而是一种阅读和复制的方法。然而,显然有部分内容是有用的引用(例如,BDD 步骤或lib)。这可以通过安装为包并导入来实现。
状态
现在相当完整。请参阅开放问题。
快速浏览
启动示例应用服务器
-/code/rest-api-blueprint$ python server.py * Running on http://127.0.0.1:5000/ * Restarting with reloader ...
使用示例应用添加人员详情
~/code/rest-api-blueprint$ curl -X PUT localhost:5000/v1/people/fred -H 'Content-Type: application/json' -d '{"email": "a@b.c"}' { "status": "ok" }
使用示例应用检索人员详情
~/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详细信息)
~/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文档(文档详情)
~/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 .
(这将也会安装任何依赖包。)
接下来是什么?
请通过bitbucket Issue Tracker发送反馈,提出bug或请求,或根据许可协议(理想情况下创建pull request)进行克隆和改进(BSD 2-Clause 许可)。
项目详情
关闭
rest-api-blueprint-0.1.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | d67677cbbb0e183a5cda4cff208d349dd4b721f82d2df7dddc5470f7f9f6642d |
|
MD5 | 243888ad62ac888d194881deb55fb892 |
|
BLAKE2b-256 | 3dc1520fc4dbe9146eb1f859073ef06d76485a9eb78f2f008ea7a42591005f12 |