烧瓶中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 stepslib)。这可以通过作为包安装并导入来实现。

状态

现在已经完成了。见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 .

(它也将安装任何依赖的软件包。)

接下来是什么?

有兴趣吗?阅读Wiki并查看code

请使用BitBucket问题向我发送反馈、引发错误或请求 或者根据 允许的bsd 2子句license

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java@Resource和@Autowired返回null,带有方面日志   如何从Java算法的无序字符串中检索原始字符串   如何在Java文本区域中显示多行扫描仪输入   java如何迭代一个arraylist并将符合条件的某些对象复制到另一个arraylist?   在java中存储矩阵并进行比较   使用BND工具的java Gradle多构建平面独立项目   读取java时文件名中的随机字符   java JDBC准备的语句UTF8   java如何在web的子项目中包含源程序包。主项目中的xml   Spring MVC、Tomcat和mysql的java开发环境   java Mous类OnClick侦听器错误   左起第二斜杠后的java剪切字符串   java TestNG处理外部系统依赖关系,需要澄清   java如何在JUnit测试类中注入多个EJB   java无法将“”解析为整数,然后退出   具有HashMap返回空HashMap的java装饰器设计模式   apache和Java之间的“解密失败或坏记录mac”   java在运行基于Spring的应用程序时如何解析占位符?   java swing/batik的奇怪行为   tostring Long在java中不会转换为字符串