Skip to content

Commit

Permalink
fix(api/web/release): fix api test get resource list (#783)
Browse files Browse the repository at this point in the history
  • Loading branch information
Han-Ya-Jun authored Aug 5, 2024
1 parent 86f73e7 commit 7728221
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from apigateway.biz.release import ReleaseHandler
from apigateway.common.fields import CurrentGatewayDefault, TimestampField
from apigateway.common.i18n.field import SerializerTranslatedField
from apigateway.core.constants import (
PublishEventEnum,
PublishEventNameTypeEnum,
Expand Down Expand Up @@ -51,6 +52,26 @@ def validate_resource_version_id(self, value):
return value


class ResourceOutputSLZ(serializers.Serializer):
id = serializers.IntegerField(read_only=True, help_text="资源 ID")
name = serializers.CharField(read_only=True, help_text="资源名称")
description = SerializerTranslatedField(
translated_fields={"en": "description_en"}, allow_blank=True, read_only=True, help_text="资源描述"
)
method = serializers.CharField(read_only=True, help_text="资源前端请求方法")
path = serializers.CharField(read_only=True, help_text="资源前端请求路径")
verified_user_required = serializers.BooleanField(read_only=True, help_text="是否需要认证用户")
verified_app_required = serializers.BooleanField(read_only=True, help_text="是否需要认证应用")
resource_perm_required = serializers.BooleanField(read_only=True, help_text="是否验证应用访问资源的权限")
labels = serializers.SerializerMethodField(help_text="资源标签列表")

class Meta:
ref_name = "apigateway.apis.web.resource.ResourceOutputSLZ"

def get_labels(self, obj):
return self.context["labels"].get(obj.id, [])


class ReleaseResourceSchemaOutputSLZ(serializers.Serializer):
resource_id = serializers.IntegerField(allow_null=False, required=True, help_text="资源id")
body_schema = serializers.JSONField(required=False, help_text="request_body schema")
Expand Down
46 changes: 19 additions & 27 deletions src/dashboard/apigateway/apigateway/apis/web/release/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# to the current version of the project delivered to anyone in the future.
#
import logging
from collections import defaultdict

from django.conf import settings
from django.http import Http404
Expand All @@ -28,8 +27,9 @@
from rest_framework import generics, status

from apigateway.biz.release import ReleaseHandler
from apigateway.biz.released_resource import ReleasedResourceData
from apigateway.biz.released_resource import ReleasedResourceHandler
from apigateway.biz.releaser import ReleaseError, release
from apigateway.biz.resource_label import ResourceLabelHandler
from apigateway.biz.resource_version import ResourceVersionHandler
from apigateway.common.error_codes import error_codes
from apigateway.common.user_credentials import get_user_credentials_from_request
Expand All @@ -45,6 +45,7 @@
ReleaseHistoryQueryInputSLZ,
ReleaseInputSLZ,
ReleaseResourceSchemaOutputSLZ,
ResourceOutputSLZ,
)

logger = logging.getLogger(__name__)
Expand All @@ -53,7 +54,9 @@
@method_decorator(
name="get",
decorator=swagger_auto_schema(
tags=["WebAPI.Release"], operation_description="获取环境下可用的资源列表接口(在线调试)"
tags=["WebAPI.Release"],
operation_description="获取环境下可用的资源列表接口(在线调试)",
responses={status.HTTP_200_OK: ResourceOutputSLZ(many=True)},
),
)
class ReleaseAvailableResourceListApi(generics.ListAPIView):
Expand All @@ -70,31 +73,20 @@ def list(self, request, *args, **kwargs):
instance = self.get_object()
except Http404:
raise error_codes.NOT_FOUND.format(_("当前选择环境未发布版本,请先发布版本到该环境。"))
stage_name = instance.stage.name
data = defaultdict(list)
for resource in instance.resource_version.data:
resource_data = ReleasedResourceData.from_data(resource)
# 禁用环境时,去掉相应资源
if resource_data.is_disabled_in_stage(stage_name):
continue
path_display = resource_data.path_display
data[path_display].append(
{
"id": resource_data.id,
"name": resource_data.name,
"description": resource_data.description,
"description_en": resource_data.description_en,
"method": resource_data.method,
"path": path_display,
"match_subpath": resource_data.match_subpath,
"verified_user_required": resource_data.verified_user_required,
}
)

if data:
return OKJsonResponse(data=data)

raise error_codes.NOT_FOUND.format(_("当前选择环境未发布版本,请先发布版本到该环境。"))
stage_name = instance.stage.name
resources = ReleasedResourceHandler.get_public_released_resource_data_list(
request.gateway.id, stage_name, is_only_public=False
)
resource_ids = [resource.id for resource in resources]
output_slz = ResourceOutputSLZ(
resources,
many=True,
context={
"labels": ResourceLabelHandler.get_labels(resource_ids),
},
)
return OKJsonResponse(data=output_slz.data)


@method_decorator(
Expand Down
10 changes: 7 additions & 3 deletions src/dashboard/apigateway/apigateway/biz/released_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,10 @@ def get_latest_doc_link(resource_ids: List[int]) -> Dict[int, str]:
return doc_links

@staticmethod
def get_public_released_resource_data_list(gateway_id: int, stage_name: str) -> List[ReleasedResourceData]:
"""获取网关环境下,已发布的,可公开的资源数据"""
def get_public_released_resource_data_list(
gateway_id: int, stage_name: str, is_only_public: Optional[bool] = True
) -> List[ReleasedResourceData]:
"""获取网关环境下,已发布的资源数据"""
release = (
Release.objects.filter(gateway_id=gateway_id, stage__name=stage_name)
.prefetch_related("resource_version")
Expand All @@ -188,7 +190,9 @@ def get_public_released_resource_data_list(gateway_id: int, stage_name: str) ->
return []

resources = [ReleasedResourceData.from_data(resource) for resource in release.resource_version.data]
return list(filter(lambda resource: resource.is_public, resources))
if is_only_public:
return list(filter(lambda resource: resource.is_public, resources))
return resources

@staticmethod
def get_released_resource(gateway_id: int, stage_name: str, resource_name: str) -> Optional[ReleasedResource]:
Expand Down

0 comments on commit 7728221

Please sign in to comment.