Skip to content

Commit

Permalink
feat(openapi): Add List ProxyNodes with pagination support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Dec 12, 2023
1 parent b12dc4e commit 48cfa18
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 2 deletions.
14 changes: 13 additions & 1 deletion apps/openapi/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions apps/openapi/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion apps/proxy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions configs/default/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
54 changes: 54 additions & 0 deletions spec/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -52,6 +59,8 @@ components:
type: integer
upload_bandwidth_bytes:
type: integer
online_info:
$ref: "#/components/schemas/OnlineInfo"
required:
- id
- name
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 48cfa18

Please sign in to comment.