如何将 coreapi 客户端与 django Rest 框架一起使用?

jul*_*lka 5 python-3.x django-rest-framework

我已将 django Rest Framework 版本 3.10 集成到现有的 django 2.2 项目中,将 api 根目录放置在/api.

现在我尝试使用 coreapi cli 客户端将一些文档上传到服务器。

$ coreapi get http://localhost:8000/openapi
<DownloadedFile '/root/.coreapi/downloads/openapi (4)', open 'rb'>
$ coreapi get http://localhost:8000/api
{
    "invoices": "http://localhost:8000/api/invoices/"
}
$ coreapi action invoices list
Index ['invoices']['list'] did not reference a link. Key 'invoices' was not found.
Run Code Online (Sandbox Code Playgroud)

/openapi是根据请求生成模式并返回的端点

openapi: 3.0.2
info:
  title: Orka
  version: TODO
  description: API for orka project
paths:
  /invoices/:
    get:
      operationId: ListInvoices
      parameters: []
      responses:
        '200':
          content:
            application/json:
              schema:
                required:
                - file_name
                - original_file_name
                properties:
                  file_name:
                    type: string
                  original_file_name:
                    type: string
                    maxLength: 80
                  upload_date:
                    type: string
                    format: date-time
                    readOnly: true
                  data:
                    type: object
                    nullable: true
                  confidence:
                    type: number
                  user_verified:
                    type: boolean
  /invoices/{id}/:
    get:
      operationId: retrieveInvoice
      parameters:
      - name: id
        in: path
        required: true
        description: A unique integer value identifying this Invoice.
        schema:
          type: string
      responses:
        '200':
          content:
            application/json:
              schema:
                required:
                - file_name
                - original_file_name
                properties:
                  file_name:
                    type: string
                  original_file_name:
                    type: string
                    maxLength: 80
                  upload_date:
                    type: string
                    format: date-time
                    readOnly: true
                  data:
                    type: object
                    nullable: true
                  confidence:
                    type: number
                  user_verified:
                    type: boolean
Run Code Online (Sandbox Code Playgroud)

不存在任何复杂的发票路径(即使它应该是/api/invoices)。

我已经成功让 coreapi 与外部 api 一起使用,所以这似乎是我如何配置 url 和/或视图的问题。

它们都非常简单。

$ coreapi get http://localhost:8000/openapi
<DownloadedFile '/root/.coreapi/downloads/openapi (4)', open 'rb'>
$ coreapi get http://localhost:8000/api
{
    "invoices": "http://localhost:8000/api/invoices/"
}
$ coreapi action invoices list
Index ['invoices']['list'] did not reference a link. Key 'invoices' was not found.
Run Code Online (Sandbox Code Playgroud)

看来我错过了一些明显的东西。我需要配置什么才能使用 coreapi 客户端来使用我的 api?

小智 4

我遇到了同样的错误 - python 3.8.5、Django 3.1、DRF 3.12.1。对我来说,相似之处在于coreapi返回 aDownloadedFile而不是 a Document,这似乎是在coreapi收到不期望的内容类型时发生的。

对我来说,解决方案是:

  1. pip install openapi-codec(如果您还没有安装)
  2. coreapi get http://localhost:8000/openapi?format=openapi-json --format=openapi

您会知道它成功了,因为您不会看到任何提及 的内容DownloadedFile,而是看到可用标签/操作的摘要。

不知道为什么必须强制采用这种格式。