[英]Swagger API documentation
我看到了Flask和Django的招搖文檔。 在 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.