跳转到主要内容

Flask中REST API的教学蓝图。

项目描述

这是对如何使用Python(版本2.7)和优秀的微网页框架Flask创建REST API的探索。它旨在成为教学蓝图而不是库或实用程序。更简单、更诚实的目标声明是,提供一份关于使用Python和Flask构建的REST API设计和结构的当前(但正在演变)喜好的清晰陈述。

范围包括自动测试、文档、身份验证、功能切换、数据格式、MIME类型和Unicode。由于重点是REST API结构和表达,因此范围不包括ORM和模板引擎等事物。

请参阅Wiki进行讨论和解释。否则,存储库代码是权威的。

请注意,这不仅仅是一个库,而是一种阅读和复制的方法。然而,显然有部分内容是有用的引用(例如,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 .

(这将也会安装任何依赖包。)

接下来是什么?

好奇吗?阅读Wiki并查看代码

请通过bitbucket Issue Tracker发送反馈,提出bug或请求,或根据许可协议(理想情况下创建pull request)进行克隆和改进(BSD 2-Clause 许可)。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

rest-api-blueprint-0.1.tar.gz (83.9 kB 查看哈希值)

上传时间 源代码

由以下支持