Skip to content

Commit 1054d07

Browse files
author
Xinkai Yi
committed
feat: 新增@extend_schema装饰器声明接口以支持自动接入蓝鲸API网关
1 parent a78f982 commit 1054d07

5 files changed

Lines changed: 39 additions & 22 deletions

File tree

bk-plugin-framework/bk_plugin_framework/serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
def enveloper(serializer_class, many: bool = False):
6-
"""统一响应包装器(参考您的模式)"""
6+
"""统一响应包装器"""
77
component_name = "Enveloped{}{}".format(
88
serializer_class.__name__.replace("Serializer", ""),
99
"List" if many else "",

bk-plugin-framework/bk_plugin_framework/services/bpf_service/api/callback.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,36 @@
1515

1616
from apigw_manager.apigw.decorators import apigw_require
1717
from bk_plugin_framework.runtime.callback.api import callback, parse_callback_token
18+
from bk_plugin_framework.serializers import enveloper
1819
from blueapps.account.decorators import login_exempt
1920
from django.utils.decorators import method_decorator
20-
from drf_yasg.utils import swagger_auto_schema
21+
from drf_spectacular.utils import extend_schema
22+
from rest_framework import serializers
2123
from rest_framework.decorators import action
2224
from rest_framework.response import Response
2325
from rest_framework.views import APIView
2426

2527
logger = logging.getLogger("bk_plugin")
2628

2729

30+
class PluginCallbackParamsSerializer(serializers.Serializer):
31+
token = serializers.CharField(help_text="插件回调token", required=True)
32+
33+
34+
class PluginCallbackResponseSerializer(serializers.Serializer):
35+
result = serializers.BooleanField(help_text="回调结果,True表示成功,False表示失败")
36+
message = serializers.CharField(help_text="回调结果信息", required=False)
37+
38+
2839
@method_decorator(login_exempt, name="dispatch")
2940
@method_decorator(apigw_require, name="dispatch")
3041
class PluginCallback(APIView):
3142
authentication_classes = [] # csrf exempt
3243

33-
@swagger_auto_schema(
34-
method="POST",
35-
operation_summary="plugin callback",
44+
@extend_schema(
45+
summary="插件回调",
46+
request=PluginCallbackParamsSerializer,
47+
responses={200: enveloper(PluginCallbackResponseSerializer)},
3648
)
3749
@action(methods=["POST"], detail=True)
3850
def post(self, request, token):

bk-plugin-framework/bk_plugin_framework/services/bpf_service/api/invoke.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from apigw_manager.apigw.decorators import apigw_require
1515
from bk_plugin_framework.hub import VersionHub
1616
from bk_plugin_framework.runtime.executor import BKPluginExecutor
17+
from bk_plugin_framework.serializers import enveloper
1718
from bk_plugin_framework.services.bpf_service.api.permissions import (
1819
ScopeAllowPermission,
1920
)
@@ -22,7 +23,7 @@
2223
)
2324
from blueapps.account.decorators import login_exempt
2425
from django.utils.decorators import method_decorator
25-
from drf_yasg.utils import swagger_auto_schema
26+
from drf_spectacular.utils import extend_schema
2627
from rest_framework import serializers, status
2728
from rest_framework.decorators import action
2829
from rest_framework.exceptions import ValidationError
@@ -55,11 +56,10 @@ class Invoke(APIView):
5556
authentication_classes = [] # csrf exempt
5657
permission_classes = [ScopeAllowPermission]
5758

58-
@swagger_auto_schema(
59-
method="POST",
60-
operation_summary="Invoke specific version plugin",
61-
request_body=InvokeParamsSerializer,
62-
responses={200: InvokeResponseSerializer},
59+
@extend_schema(
60+
summary="插件调用",
61+
request=InvokeParamsSerializer,
62+
responses={200: enveloper(InvokeResponseSerializer)},
6363
)
6464
@action(methods=["POST"], detail=True)
6565
def post(self, request, version):

bk-plugin-framework/bk_plugin_framework/services/bpf_service/api/plugin_api_dispatch.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from urllib.parse import urlsplit
1616

1717
from apigw_manager.apigw.decorators import apigw_require
18+
from bk_plugin_framework.serializers import enveloper
1819
from bk_plugin_framework.services.bpf_service.api.permissions import (
1920
ScopeAllowPermission,
2021
)
@@ -25,7 +26,7 @@
2526
from django.test import RequestFactory
2627
from django.urls import Resolver404, resolve
2728
from django.utils.decorators import method_decorator
28-
from drf_yasg.utils import swagger_auto_schema
29+
from drf_spectacular.utils import extend_schema
2930
from rest_framework import serializers, status
3031
from rest_framework.decorators import action
3132
from rest_framework.exceptions import ValidationError
@@ -76,11 +77,10 @@ class PluginAPIDispatch(APIView):
7677
authentication_classes = [] # csrf exempt
7778
permission_classes = [ScopeAllowPermission]
7879

79-
@swagger_auto_schema(
80-
method="POST",
81-
operation_summary="Plugin API dispatch",
82-
request_body=PluginAPIDispatchParamsSerializer,
83-
responses={200: PluginAPIDispatchResponseSerializer},
80+
@extend_schema(
81+
summary="插件API分发",
82+
request=PluginAPIDispatchParamsSerializer,
83+
responses={200: enveloper(PluginAPIDispatchResponseSerializer)},
8484
)
8585
@action(methods=["POST"], detail=True)
8686
def post(self, request):

bk-plugin-framework/bk_plugin_framework/services/bpf_service/api/schedule.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
import logging
1414

1515
from bk_plugin_framework.runtime.schedule.models import Schedule as ScheduleModel
16+
from bk_plugin_framework.serializers import enveloper
1617
from bk_plugin_framework.services.bpf_service.api.serializers import (
1718
StandardResponseSerializer,
1819
)
1920
from blueapps.account.decorators import login_exempt
2021
from django.utils.decorators import method_decorator
21-
from drf_yasg.utils import swagger_auto_schema
22+
from drf_spectacular.utils import extend_schema
2223
from rest_framework import permissions, serializers
2324
from rest_framework.decorators import action
2425
from rest_framework.response import Response
@@ -27,6 +28,10 @@
2728
logger = logging.getLogger("root")
2829

2930

31+
class ScheduleParamsSerializer(serializers.Serializer):
32+
trace_id = serializers.CharField(help_text="插件调用 trace id")
33+
34+
3035
class ScheduleResponseSerializer(StandardResponseSerializer):
3136
class ScheduleDataSerializer(serializers.Serializer):
3237
class Meta:
@@ -48,10 +53,10 @@ class Schedule(APIView):
4853

4954
permission_classes = [permissions.AllowAny]
5055

51-
@swagger_auto_schema(
52-
method="GET",
53-
operation_summary="Get plugin schedule detail with trace_id",
54-
responses={200: ScheduleResponseSerializer},
56+
@extend_schema(
57+
summary="获取插件调度详情",
58+
request=ScheduleParamsSerializer,
59+
responses={200: enveloper(ScheduleResponseSerializer)},
5560
)
5661
@action(methods=["GET"], detail=True)
5762
def get(self, request, trace_id):

0 commit comments

Comments
 (0)