Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit 48cfa18

Browse files
committed
feat(openapi): Add List ProxyNodes with pagination support.
1 parent b12dc4e commit 48cfa18

File tree

5 files changed

+80
-2
lines changed

5 files changed

+80
-2
lines changed

apps/openapi/serializer.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,24 @@
33
from apps.proxy.models import ProxyNode
44

55

6+
def get_proxy_node_exclude_fields():
7+
fields = ProxyNode._meta.fields
8+
field_names_with_ehco = [
9+
field.name for field in fields if field.name.startswith("ehco_")
10+
]
11+
return field_names_with_ehco
12+
13+
614
class ProxyNodeSerializer(serializers.ModelSerializer):
715
class Meta:
816
model = ProxyNode
9-
fields = "__all__"
17+
exclude = get_proxy_node_exclude_fields()
1018

1119
multi_user_port = serializers.SerializerMethodField()
20+
online_info = serializers.SerializerMethodField()
1221

1322
def get_multi_user_port(self, node: ProxyNode):
1423
return node.get_user_port()
24+
25+
def get_online_info(self, node: ProxyNode):
26+
return node.online_info

apps/openapi/views.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ class ProxyNodeViewSet(BaseOpenAPIViewSet):
1515
serializer_class = ProxyNodeSerializer
1616
queryset = ProxyNode.objects.all()
1717

18+
def list(self, request, *args, **kwargs):
19+
nodes = ProxyNode.objects.all()
20+
page = self.paginate_queryset(nodes)
21+
data = self.serializer_class(page, many=True).data
22+
return self.get_paginated_response(data)
23+
1824
@action(detail=False, methods=["get"])
1925
def search(self, request):
2026
ip = request.GET.get("ip")

apps/proxy/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ class ProxyNode(BaseNodeModel, SequenceMixin):
215215
ehco_log_level = models.CharField(
216216
"隧道日志等级", max_length=64, default="info", choices=EHCO_LOG_LEVELS
217217
)
218-
ehco_reload_interval = models.IntegerField("配置重载间隔", max_length=64, default=0)
218+
ehco_reload_interval = models.IntegerField("配置重载间隔", default=0)
219219

220220
upload_bandwidth_bytes = models.BigIntegerField("上传带宽", default=0)
221221
current_used_upload_bandwidth_bytes = models.BigIntegerField("当前使用的上传带宽", default=0)

configs/default/common.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,9 @@
9696

9797
# set debug in env
9898
DEBUG = os.getenv("DEBUG", "True") == "True"
99+
100+
101+
REST_FRAMEWORK = {
102+
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination",
103+
"PAGE_SIZE": 20,
104+
}

spec/openapi.yaml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ components:
1414
in: header
1515
name: x-api-key
1616
schemas:
17+
OnlineInfo:
18+
type: object
19+
properties:
20+
online_user_count:
21+
type: integer
22+
online:
23+
type: boolean
1724
ProxyNode:
1825
type: object
1926
properties:
@@ -52,6 +59,8 @@ components:
5259
type: integer
5360
upload_bandwidth_bytes:
5461
type: integer
62+
online_info:
63+
$ref: "#/components/schemas/OnlineInfo"
5564
required:
5665
- id
5766
- name
@@ -90,6 +99,51 @@ security:
9099
- OpenApiKeyAuth: []
91100

92101
paths:
102+
/proxy_nodes/:
103+
get:
104+
summary: List all ProxyNodes
105+
tags:
106+
- ProxyNode
107+
parameters:
108+
- name: page
109+
in: query
110+
required: false
111+
schema:
112+
type: integer
113+
example: 1
114+
- name: limit
115+
in: query
116+
required: false
117+
schema:
118+
type: integer
119+
example: 10
120+
responses:
121+
200:
122+
description: Successful response
123+
content:
124+
application/json:
125+
schema:
126+
type: object
127+
properties:
128+
count:
129+
type: integer
130+
next:
131+
type: string
132+
description: URL of the next page
133+
previous:
134+
type: string
135+
description: URL of the previous page
136+
results:
137+
type: array
138+
items:
139+
$ref: "#/components/schemas/ProxyNode"
140+
400:
141+
description: Bad request
142+
content:
143+
application/json:
144+
schema:
145+
$ref: "#/components/schemas/CommonErrorResp"
146+
93147
/proxy_nodes/search/:
94148
get:
95149
summary: search node by ip

0 commit comments

Comments
 (0)