From 48cfa18fd44d4b38ce3ea834b056b92d2362036e Mon Sep 17 00:00:00 2001 From: Ehco1996 Date: Tue, 12 Dec 2023 09:36:00 +0800 Subject: [PATCH] feat(openapi): Add List ProxyNodes with pagination support. --- apps/openapi/serializer.py | 14 +++++++++- apps/openapi/views.py | 6 +++++ apps/proxy/models.py | 2 +- configs/default/common.py | 6 +++++ spec/openapi.yaml | 54 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 2 deletions(-) diff --git a/apps/openapi/serializer.py b/apps/openapi/serializer.py index 47badc4d69..788c031d1b 100644 --- a/apps/openapi/serializer.py +++ b/apps/openapi/serializer.py @@ -3,12 +3,24 @@ from apps.proxy.models import ProxyNode +def get_proxy_node_exclude_fields(): + fields = ProxyNode._meta.fields + field_names_with_ehco = [ + field.name for field in fields if field.name.startswith("ehco_") + ] + return field_names_with_ehco + + class ProxyNodeSerializer(serializers.ModelSerializer): class Meta: model = ProxyNode - fields = "__all__" + exclude = get_proxy_node_exclude_fields() multi_user_port = serializers.SerializerMethodField() + online_info = serializers.SerializerMethodField() def get_multi_user_port(self, node: ProxyNode): return node.get_user_port() + + def get_online_info(self, node: ProxyNode): + return node.online_info diff --git a/apps/openapi/views.py b/apps/openapi/views.py index c140dc10c3..1f5cc77373 100644 --- a/apps/openapi/views.py +++ b/apps/openapi/views.py @@ -15,6 +15,12 @@ class ProxyNodeViewSet(BaseOpenAPIViewSet): serializer_class = ProxyNodeSerializer queryset = ProxyNode.objects.all() + def list(self, request, *args, **kwargs): + nodes = ProxyNode.objects.all() + page = self.paginate_queryset(nodes) + data = self.serializer_class(page, many=True).data + return self.get_paginated_response(data) + @action(detail=False, methods=["get"]) def search(self, request): ip = request.GET.get("ip") diff --git a/apps/proxy/models.py b/apps/proxy/models.py index ebdd21754f..2d3bcbbead 100644 --- a/apps/proxy/models.py +++ b/apps/proxy/models.py @@ -215,7 +215,7 @@ class ProxyNode(BaseNodeModel, SequenceMixin): ehco_log_level = models.CharField( "隧道日志等级", max_length=64, default="info", choices=EHCO_LOG_LEVELS ) - ehco_reload_interval = models.IntegerField("配置重载间隔", max_length=64, default=0) + ehco_reload_interval = models.IntegerField("配置重载间隔", default=0) upload_bandwidth_bytes = models.BigIntegerField("上传带宽", default=0) current_used_upload_bandwidth_bytes = models.BigIntegerField("当前使用的上传带宽", default=0) diff --git a/configs/default/common.py b/configs/default/common.py index 1fc2618330..f20dffde05 100644 --- a/configs/default/common.py +++ b/configs/default/common.py @@ -96,3 +96,9 @@ # set debug in env DEBUG = os.getenv("DEBUG", "True") == "True" + + +REST_FRAMEWORK = { + "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination", + "PAGE_SIZE": 20, +} diff --git a/spec/openapi.yaml b/spec/openapi.yaml index 0b4e676e16..d96c77393b 100644 --- a/spec/openapi.yaml +++ b/spec/openapi.yaml @@ -14,6 +14,13 @@ components: in: header name: x-api-key schemas: + OnlineInfo: + type: object + properties: + online_user_count: + type: integer + online: + type: boolean ProxyNode: type: object properties: @@ -52,6 +59,8 @@ components: type: integer upload_bandwidth_bytes: type: integer + online_info: + $ref: "#/components/schemas/OnlineInfo" required: - id - name @@ -90,6 +99,51 @@ security: - OpenApiKeyAuth: [] paths: + /proxy_nodes/: + get: + summary: List all ProxyNodes + tags: + - ProxyNode + parameters: + - name: page + in: query + required: false + schema: + type: integer + example: 1 + - name: limit + in: query + required: false + schema: + type: integer + example: 10 + responses: + 200: + description: Successful response + content: + application/json: + schema: + type: object + properties: + count: + type: integer + next: + type: string + description: URL of the next page + previous: + type: string + description: URL of the previous page + results: + type: array + items: + $ref: "#/components/schemas/ProxyNode" + 400: + description: Bad request + content: + application/json: + schema: + $ref: "#/components/schemas/CommonErrorResp" + /proxy_nodes/search/: get: summary: search node by ip