views.py 886 B

1234567891011121314151617181920212223242526272829
  1. from django.http import HttpResponse
  2. from rest_framework.response import Response
  3. from rest_framework.views import APIView
  4. from apps.api_tokens.schema import APITokenSchema
  5. from apps.users.serializers import UserSerializer
  6. class APIRootView(APIView):
  7. """/api/0/ gives information about the server and current user"""
  8. def get(self, request, *args, **kwargs):
  9. user_data = None
  10. auth_data = None
  11. if request.user.is_authenticated:
  12. user_data = UserSerializer(instance=request.user).data
  13. if api_token := request.auth:
  14. auth_data = APITokenSchema(**api_token.__dict__).dict()
  15. return Response(
  16. {
  17. "version": "0",
  18. "user": user_data,
  19. "auth": auth_data,
  20. }
  21. )
  22. async def health(request):
  23. return HttpResponse("ok", content_type="text/plain")