How to use coreapi client with django rest framework?

别来无恙 提交于 2021-02-08 23:40:24

问题


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

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

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

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.

Both of them are dead simple.

# 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

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?


回答1:


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 (if you haven't installed it yet)
  2. coreapi get http://localhost:8000/openapi?format=openapi-json --format=openapi

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.



来源:https://stackoverflow.com/questions/57061803/how-to-use-coreapi-client-with-django-rest-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!