Django REST framework 学习纪要 Tutor

作者: 流月0 | 来源:发表于2017-12-04 20:24 被阅读0次

Schemas是一种机器可读的文档,描述了API的端点,它们的URLs和所支持的操作。
Schemas可以用于自动生成文档,也可以用来驱动可以与API交互的动态客户端库。

Core API

为了提供Schemas支持,REST框架使用了Core API。
安装coreapi

pip install coreapi

添加schema

REST框架既支持自定义Schemas视图,也支持自动生成Schemas视图。
由于我们使用的是viewset和route,我们可以简单地自动生成Schemas视图。

现在我们需要通过在URL中配置一个自动生成的Schemas视图,为我们的API添加一个Schemas。
编辑urls.py

from rest_framework.schemas import get_schema_view

schema_view = get_schema_view(title='Pastebin API')

urlpatterns = [
    url(r'^schema/$', schema_view),
    ...
]

我们通过命令行的形式访问该接口,并指定接收格式为corejson

(django_rest_framework) [root@iZuf62kvdczytcyqlvr2pgZ django_rest_framework]# http http://127.0.0.1:80/schema/ Accept:application/coreapi+json
HTTP/1.0 200 OK
Allow: GET, HEAD, OPTIONS
Content-Length: 1498
Content-Type: application/coreapi+json
Date: Fri, 01 Dec 2017 12:04:53 GMT
Server: WSGIServer/0.2 CPython/3.6.3
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN

{
    "_meta": {
        "title": "Pastebin API",
        "url": "http://127.0.0.1/schema/"
    },
    "_type": "document",
    "snippets": {
        "highlight": {
            "_type": "link",
            "action": "get",
...

使用命令行客户端

既然我们的API提供了一个Schemas url,我们可以使用一个动态的客户端来与API进行交互。
为了演示,我们使用Core API客户端。
安装coreapi-cli

pip install coreapi-cli

使用命令行客户端访问schema接口

(django_rest_framework) [root@iZuf62kvdczytcyqlvr2pgZ django_rest_framework]# coreapi get http://127.0.0.1:80/schema/
<Pastebin API "http://127.0.0.1/schema/">
    snippets: {
        list()
        read(id)
        highlight(id)
    }
    users: {
        list()
        read(id)
    }

由于我们没有登录,所以我们现在只能看到只读的接口。

列出现有的所有snippets

(django_rest_framework) [root@iZuf62kvdczytcyqlvr2pgZ django_rest_framework]# coreapi action snippets list
[
    {
        "url": "http://127.0.0.1/snippets/1/",
        "id": 1,
        "highlight": "http://127.0.0.1/snippets/1/highlight/",
        "owner": "song",
        "title": "test_1",
        "code": "print('hello world')",
        "linenos": true,
        "language": "python",
        "style": "friendly"
    },
]

有些API需要一些命名参数

(django_rest_framework) [root@iZuf62kvdczytcyqlvr2pgZ django_rest_framework]# coreapi action snippets highlight --param id=1
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
  <title>test_1</title>
  <meta http-equiv="content-type" content="text/html; charset=None">
  <style type="text/css">
td.linenos { background-color: #f0f0f0; padding-right: 10px; }
span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }
pre { line-height: 125%; }
...

登录

需要写入的接口需要我们登录才能做,通过以下命令进行登录(test:qazxswedc为用户名和密码)

(django_rest_framework) [root@iZuf62kvdczytcyqlvr2pgZ django_rest_framework]# coreapi credentials add 127.0.0.1 test:qazxswedc --auth basic
Added credentials
127.0.0.1 "Basic dGVzdDpxYXp4c3dlZGM="

重载

(django_rest_framework) [root@iZuf62kvdczytcyqlvr2pgZ django_rest_framework]# coreapi reload
Pastebin API "http://127.0.0.1:80/schema/">
    snippets: {
        create(code, [title], [linenos], [language], [style])
        delete(id)
        highlight(id)
        list()
        partial_update(id, [title], [code], [linenos], [language], [style])
        read(id)
        update(id, code, [title], [linenos], [language], [style])
    }
    users: {
        list()
        read(id)
    }

我们现在可以对数据进行写操作了

coreapi action snippets delete --param id=1

关于

本人是初学Django REST framework,Django REST framework 学习纪要系列文章是我从官网文档学习后的初步消化成果,如有错误,欢迎指正。

学习用代码Github仓库:shelmingsong/django_rest_framework

本文参考的官网文档:Tutorial 7: Schemas & client libraries

博客更新地址

相关文章

网友评论

    本文标题:Django REST framework 学习纪要 Tutor

    本文链接:https://www.haomeiwen.com/subject/lzkfixtx.html