-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert server status endpoint to ninja (#234)
- Loading branch information
1 parent
11c0a31
commit bc0d549
Showing
4 changed files
with
24 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,35 @@ | ||
import datetime | ||
import socket | ||
from dataclasses import dataclass | ||
|
||
from rest_framework import permissions, serializers | ||
from rest_framework.response import Response | ||
from rest_framework.schemas.openapi import AutoSchema | ||
from rest_framework.views import APIView | ||
|
||
SERVER_INSTANCE_EPOCH = datetime.datetime.now() | ||
|
||
|
||
@dataclass | ||
class ServerStatus: | ||
uptime: datetime.timedelta | ||
hostname: str | ||
ip: str | ||
from ninja import Field, Router, Schema | ||
|
||
from django.http import HttpRequest | ||
|
||
class UptimeSerializer(serializers.Serializer): | ||
iso8601 = serializers.DurationField(source='*') | ||
days = serializers.IntegerField() | ||
seconds = serializers.IntegerField() | ||
useconds = serializers.IntegerField(source='microseconds') | ||
router = Router() | ||
|
||
SERVER_INSTANCE_EPOCH = datetime.datetime.now() | ||
|
||
class ServerStatusSerializer(serializers.Serializer): | ||
uptime = UptimeSerializer(required=True) | ||
hostname = serializers.CharField(required=True) | ||
ip = serializers.IPAddressField(required=True) | ||
|
||
def create(self, validated_data): | ||
instance = ServerStatus( | ||
uptime=validated_data['uptime']['iso8601'], | ||
hostname=validated_data['hostname'], | ||
ip=validated_data['iso8601'], | ||
) | ||
# we'd save it here if this were a Django model | ||
# instance.save() | ||
return instance | ||
|
||
def update(self, instance, validated_data): | ||
instance.uptime = validated_data.get('uptime', instance.uptime) | ||
instance.hostname = validated_data.get('hostname', instance.hostname) | ||
instance.ip = validated_data.get('ip', instance.ip) | ||
# we'd save it here if this were a Django model | ||
# instance.save() | ||
return instance | ||
|
||
class UptimeSchema(Schema): | ||
iso8601: str | ||
days: int | ||
seconds: int | ||
useconds: int = Field(..., alias='microseconds') | ||
|
||
class ServerStatusSchema(AutoSchema): | ||
def get_operation_id(self, *args): | ||
return 'getStatus' | ||
@staticmethod | ||
def resolve_iso8601(obj: datetime.timedelta): | ||
return str(obj) | ||
|
||
def get_serializer(self, *args): | ||
return ServerStatusSerializer() | ||
|
||
class ServerStatusSchema(Schema): | ||
uptime: UptimeSchema | ||
hostname: str | ||
ip: str | ||
|
||
class RetrieveServerStatus(APIView): | ||
permission_classes = [permissions.AllowAny] | ||
schema = ServerStatusSchema() | ||
action = 'retrieve' | ||
|
||
def get(self, request): | ||
uptime = datetime.datetime.now() - SERVER_INSTANCE_EPOCH | ||
hostname = socket.gethostname() | ||
ip = socket.gethostbyname(hostname) | ||
server_status = ServerStatus(uptime, hostname, ip) | ||
serializer = ServerStatusSerializer(server_status) | ||
return Response(serializer.data) | ||
@router.get('/', response=ServerStatusSchema) | ||
def get_status(request: HttpRequest): | ||
uptime = datetime.datetime.now() - SERVER_INSTANCE_EPOCH | ||
hostname = socket.gethostname() | ||
ip = socket.gethostbyname(hostname) | ||
return ServerStatusSchema(uptime=uptime, hostname=hostname, ip=ip) |