繁体   English   中英

Swagger API 文档

[英]Swagger API documentation

我看到了FlaskDjango的招摇文档。 在 Flask 中,我可以手写设计和记录我的 API。(在参数部分下包括哪些字段是必需的、可选的等)。

这是我们在 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"
            }
          ]
        )

我可以选择包含哪些参数,哪些不包含。 但是我如何在 Django 中实现相同的功能呢? Django-Swagger 文档一点也不好。 我的主要问题是如何在 Django 中编写我的 raw-json。

在 Django 中,它会自动执行它,这不允许我自定义我的 json。 如何在 Django 上实现相同的功能?

这是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')

序列化程序.py

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

网址.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()),
)

视图.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)

我的问题是

1)如果我需要添加一个字段说xyz ,这不在我的模型中,我该如何添加它?

2)安静类似于1st ,如果我需要添加一个接受值 b/w 3 提供的值的字段,即下拉列表。 我该如何添加它?

3)如何添加可选字段? (因为在PUT请求的情况下,我可能只更新 1 个字段并将其留空,这意味着optional字段)。

4)我如何添加一个接受json字符串的字段,就像这个api一样?

谢谢

我可以通过硬编码我的 api 在 Flask 中完成所有这些事情。 但是在 Django 中,它会从我的模型中自动执行,这并没有(我相信)让我可以访问自定义我的 api。 在 Flask 中,我只需要手动编写我的 API,然后与 Swagger 集成。 Django中是否存在同样的事情?

就像我只需要在我的 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'
            ]
        },

这会起作用吗:

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)

POST 请求:

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

{"mykey": "myvalue"}

暂无
暂无

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

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