简体   繁体   English

Swagger API 文档

[英]Swagger API documentation

I saw swagger documentation of Flask and Django .我看到了FlaskDjango的招摇文档。 In Flask I can design and document my API hand-written.(Include which fields are required, optional etc. under parameters sections).在 Flask 中,我可以手写设计和记录我的 API。(在参数部分下包括哪些字段是必需的、可选的等)。

Here's how we do in Flask这是我们在 Flask 中的做法

class Todo(Resource):
    "Describing elephants"
    @swagger.operation(
        notes='some really good notes',
        responseClass=ModelClass.__name__,
        nickname='upload',
        parameters=[
            {
              "name": "body",
              "description": "blueprint object that needs to be added. YAML.",
              "required": True,
              "allowMultiple": False,
              "dataType": ModelClass2.__name__,
              "paramType": "body"
            }
          ],
        responseMessages=[
            {
              "code": 201,
              "message": "Created. The URL of the created blueprint should be in the Location header"
            },
            {
              "code": 405,
              "message": "Invalid input"
            }
          ]
        )

I can chose which parameters to include, and which not.我可以选择包含哪些参数,哪些不包含。 But how do I implement the same in Django?但是我如何在 Django 中实现相同的功能呢? Django-Swagger Document in not good at all. Django-Swagger 文档一点也不好。 My main issue is how do I write my raw-json in Django.我的主要问题是如何在 Django 中编写我的 raw-json。

In Django it automates it which does not allows me to customize my json.在 Django 中,它会自动执行它,这不允许我自定义我的 json。 How do I implement the same kind of thing on Django?如何在 Django 上实现相同的功能?

Here is models.py file这是models.py文件

class Controller(models.Model):
    id = models.IntegerField(primary_key = True)
    name = models.CharField(max_length = 255, unique = True)
    ip = models.CharField(max_length = 255, unique = True)
    installation_id = models.ForeignKey('Installation')

serializers.py序列化程序.py

class ActionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Controller
        fields = ('installation',)

urls.py网址.py

from django.conf.urls import patterns, url
from rest_framework.urlpatterns import format_suffix_patterns
from modules.actions import views as views

urlpatterns = patterns('',
    url(r'(?P<installation>[0-9]+)', views.ApiActions.as_view()),
)

views.py视图.py

class ApiActions(APIView):

    """
    Returns controllers List
    """

    model = Controller
    serializer_class = ActionSerializer 

    def get(self, request, installation,format=None):

        controllers = Controller.objects.get(installation_id = installation)
        serializer = ActionSerializer(controllers)
        return Response(serializer.data)

My questions are我的问题是

1) If I need to add a field say xyz , which is not in my models how do I add it? 1)如果我需要添加一个字段说xyz ,这不在我的模型中,我该如何添加它?

2) Quiet similar to 1st , If i need to add a field which accepts values b/w 3 provided values,ie a dropdown. 2)安静类似于1st ,如果我需要添加一个接受值 b/w 3 提供的值的字段,即下拉列表。 how do I add it?我该如何添加它?

3) How I add an optional field? 3)如何添加可选字段? (since in case of PUT request, I might only update 1 field and rest leave it blank, which means optional field). (因为在PUT请求的情况下,我可能只更新 1 个字段并将其留空,这意味着optional字段)。

4) Also how do I add a field that accepts the json string, as this api does? 4)我如何添加一个接受json字符串的字段,就像这个api一样?

Thanks谢谢

I can do all of these things in Flask by hardcoding my api.我可以通过硬编码我的 api 在 Flask 中完成所有这些事情。 But in Django, it automates from my models, which does not(as I believe) gives me the access to customize my api.但是在 Django 中,它会从我的模型中自动执行,这并没有(我相信)让我可以访问自定义我的 api。 In Flask, I just need to write my API with hands and then integrate with the Swagger.在 Flask 中,我只需要手动编写我的 API,然后与 Swagger 集成。 Does this same thing exist in Django? Django中是否存在同样的事情?

Like I just need to add the following json in my Flask code and it will answer all my questions.就像我只需要在我的 Flask 代码中添加以下json ,它就会回答我所有的问题。

# Swagger json:
    "models": {
        "TodoItemWithArgs": {
            "description": "A description...",
            "id": "TodoItem",
            "properties": {
                "arg1": { # I can add any number of arguments I want as per my requirements.
                    "type": "string"
                },
                "arg2": {
                    "type": "string"
                },
                "arg3": {
                    "default": "123",
                    "type": "string"
                }
            },
            "required": [
                "arg1",
                "arg2" # arg3 is not mentioned and hence 'opional'
            ]
        },

Would this work:这会起作用吗:

class TriggerView(APIView):
    """
    This text is the description for this API
        mykey -- My Key parameter
    """

    authentication_classes = (BasicAuthentication,)
    permission_classes = (IsAuthenticated,)

    def post(self, request, format=None):
        print request.DATA
        return Response(status=status.HTTP_202_ACCEPTED)

the POST request: POST 请求:

Authorization:Basic YWRtaW46cGFzcw==
Content-Type:application/json

{"mykey": "myvalue"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM