【问题标题】:How to use coreapi client with django rest framework?如何将 coreapi 客户端与 django rest 框架一起使用?
【发布时间】:2019-11-25 11:00:03
【问题描述】:

我已将 django rest 框架 3.10 版集成到现有 django 2.2 项目中,将 api root 放置在 /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.

/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

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

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

它们都非常简单。

# 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?

【问题讨论】:

  • 这一定是因为您的coreapi get http://localhost:8000/openapi 并未实际加载架构(“当前文档”),因此您的操作与之前加载的架构背道而驰。我只是想知道您是如何解决的,因为我遇到了完全相同的问题。
  • 查看没有之前加载的架构时会发生什么:stackoverflow.com/questions/57982787/…

标签: python-3.x django-rest-framework


【解决方案1】:

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

对我来说,解决方案是:

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

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

不确定为什么需要强制格式化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 2011-12-19
    • 2016-12-25
    • 2016-06-10
    相关资源
    最近更新 更多