Swagger API文档

时间:2014-01-30 11:22:25

标签: python django rest flask swagger

我看到了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 Document in 一点都不好。我的主要问题是如何在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')

serializers.py

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

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

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字符串的字段,如this 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'
            ]
        },

1 个答案:

答案 0 :(得分:0)

这会有效吗?

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