Rest框架中使用coreapi客户端

Rest框架中使用coreapi客户端

本文介绍了如何在Django Rest框架中使用coreapi客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've integrated django rest framework version 3.10 into an existing django 2.2 project, placing api root at /api.

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

Now I'm trying to use coreapi cli client to upload some documents to the server.

$ 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.

/openapi 是一个端点,可根据请求生成架构并返回

/openapi is an endpoint that generates schema upon request and returns

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

没有什么复杂的问题,发票的路径也不存在(即使它应该是/api/invoices ).

Nothing complicated and path for invoices exists (even though it should be /api/invoices).

我已经成功地使coreapi可以与外部api一起使用,所以这似乎与我配置网址和/或视图有关.

I've successfully got coreapi to work with external apis, so this seems to be problem with how I configure my urls and/or views.

他们两个都很简单.

# urls.py
from rest_framework import routers

from . import views


router = routers.DefaultRouter()
router.register(r'invoices', views.InvoiceViewSet)

urlpatterns = [
    path('api/', include(router.urls)),
]


# views.py
# ... imports ...

class InvoiceSerializer(serializers.HyperlinkedModelSerializer):
    """Defines API representation of invoices"""

    class Meta:  # pylint:disable=too-few-public-methods, missing-docstring
        model = Invoice
        fields = (
            'file_name',
            'original_file_name',
            'upload_date',
            'data',
            'confidence',
            'user_verified',
        )


class InvoiceViewSet(viewsets.ModelViewSet):
    """Defines api views for invoices"""
    # default permissions are set in settings.py
    parser_classes = (JSONParser, XMLParser, FormParser, MultiPartParser)
    queryset = Invoice.objects.all()
    serializer_class = InvoiceSerializer

    @action(methods=['post'], detail=True)
    def upload_with_ground_truth_file(self, request, pk):
        pass

似乎我缺少了显而易见的东西.我需要配置什么才能使用coreapi客户端使用我的api?

It seems that I'm missing something glaringly obvious. What do I need to configure so I can use coreapi client to consume my api?

推荐答案

我遇到了相同的错误-python 3.8.5,Django 3.1,DRF 3.12.1.对我来说,相似之处在于 coreapi 返回的是 DownloadedFile 而不是 Document ,这似乎是在 coreapi 收到时发生的一种意想不到的内容类型.

I hit the same error -- python 3.8.5, Django 3.1, DRF 3.12.1. The similarity for me was that coreapi returns a DownloadedFile instead of a Document, which seems to happen when coreapi receives a content-type that it doesn't expect.

对我来说,解决方案是:

For me, solution was:

  1. pip install openapi-codec (如果尚未安装)
  2. coreapi获取http://localhost:8000/openapi?format = openapi-json --format = openapi
  1. pip install openapi-codec (if you haven't installed it yet)
  2. coreapi get http://localhost:8000/openapi?format=openapi-json --format=openapi

您会知道它成功了,因为您将看不到 DownloadedFile 的任何提法,而是所有可用标签/操作的摘要.

You'll know it succeeded because you won't see any mention of DownloadedFile, instead a summary of the available tags/actions.

不确定为什么要强制使用格式.

Not sure why forcing the format should be necessary.

这篇关于如何在Django Rest框架中使用coreapi客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:32