Flask Restful API

2017-10-23 Monday     webserver

这里简单介绍 Flask 中如何使用 Restful-API 以及相关的 REST-API 测试工具。

logo

Flask-Restful

可以通过如下方式安装。

----- 安装最新版本
# pip install flask-restful

----- 手动下载安装指定版本
$ git clone https://github.com/flask-restful/flask-restful.git
$ cd flask-restful
# python setup.py develop

测试

这里使用的是 Pyresttest,该工具依赖 pycurl ,对于 CentOS 可以通过 yum install python-pycurl 命令安装。

---
- config:
    - testset: "Basic tests"
    - timeout: 100  # Increase timeout from the default 10 seconds
- test:
    - name: "Basic get"
    - url: "/api/person/"
- test:
    - name: "Get single person"
    - url: "/api/person/1/"
- test:
    - name: "Delete a single person, verify that works"
    - url: "/api/person/1/"
    - method: 'DELETE'
- test: # create entity by PUT
    - name: "Create/update person"
    - url: "/api/person/1/"
    - method: "PUT"
    - body: '{"first_name": "Gaius","id": 1,"last_name": "Baltar","login": "gbaltar"}'
    - headers: {'Content-Type': 'application/json'}
    - validators:  # This is how we do more complex testing!
        - compare: {header: content-type, comparator: contains, expected:'json'}
        - compare: {jsonpath_mini: 'login', expected: 'gbaltar'}  # JSON extraction
        - compare: {raw_body:"", comparator:contains, expected: 'Baltar' }  # Tests on raw response
- test: # create entity by POST
    - name: "Create person"
    - url: "/api/person/"
    - method: "POST"
    - body: '{"first_name": "William","last_name": "Adama","login": "theadmiral"}'
    - headers: {Content-Type: application/json}

如下的示例以 Github 提供的 API 为例,也就是 https://api.github.com

$ cat tests/restapi/github_smoketest.yaml
---
- config:
    - testset: "Quickstart app tests for github"

- test:
    - name: "Basic smoketest"
    - url: "/"

----- 运行一个github API的基本测试case
$ pyresttest https://api.github.com tests/restapi/github_smoketest.yaml

----- 打印返回的消息
$ pyresttest --print-bodies true https://api.github.com tests/restapi/github_smoketest.yaml

----- 打印详细日志
$ pyresttest --log debug https://api.github.com tests/restapi/github_smoketest.yaml

更多的示例可以参考 Github Pyresttest examples

参考

文档可以参考 Flask Restful,或者 中文文档

另外的 REST API 测试工具可以参考 Hitchhicker



如果喜欢这里的文章,而且又不差钱的话,欢迎打赏个早餐 ^_^


About This Blog

Recent Posts

Categories

Related Links

  • RTEMS
    RTEMS
  • GNU
  • Linux Kernel
  • Arduino

Search


This Site was built by Jin Yang, generated with Jekyll, and hosted on GitHub Pages
©2013-2019 – Jin Yang