Skip to content

Commit

Permalink
No-verify commit to get the test endpoints in place
Browse files Browse the repository at this point in the history
(these will likely need some help before they're good)
  • Loading branch information
jkachel committed Feb 28, 2024
1 parent 77ec839 commit c7ba87b
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 4 deletions.
27 changes: 25 additions & 2 deletions system_meta/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,34 @@
from django.urls import include, re_path
from rest_framework import routers

from system_meta.views import IntegratedSystemViewSet, ProductViewSet
from system_meta.views import (
IntegratedSystemViewSet,
ProductViewSet,
apisix_test_request,
authed_traefik_test_request,
traefik_test_request,
)

router = routers.DefaultRouter()

router.register(r"integrated_system", IntegratedSystemViewSet)
router.register(r"product", ProductViewSet)

urlpatterns = [re_path("^", include(router.urls))]
urlpatterns = [
re_path("^", include(router.urls)),
re_path(
r"^apisix_test_request/$",
apisix_test_request,
name="apisix_test_request",
),
re_path(
r"^traefik_test_request/$",
traefik_test_request,
name="traefik_test_request",
),
re_path(
r"^authed_traefik_test_request/$",
authed_traefik_test_request,
name="authed_traefik_test_request",
),
]
82 changes: 80 additions & 2 deletions system_meta/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
"""Views for the REST API for system metadata."""

from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated
import base64
import json
import logging

from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import status
from rest_framework.decorators import (
api_view,
authentication_classes,
permission_classes,
)
from rest_framework.permissions import (
IsAuthenticated,
)
from rest_framework.response import Response

from system_meta.models import IntegratedSystem, Product
from system_meta.serializers import IntegratedSystemSerializer, ProductSerializer


class IntegratedSystemViewSet(viewsets.ModelViewSet):
from unified_ecommerce.authentication import (
ApiGatewayAuthentication,
)
from unified_ecommerce.permissions import (
IsAdminUserOrReadOnly,
)
from unified_ecommerce.viewsets import AuthVariegatedModelViewSet

log = logging.getLogger(__name__)

"""Viewset for IntegratedSystem model."""

queryset = IntegratedSystem.objects.all()
Expand All @@ -21,3 +44,58 @@ class ProductViewSet(viewsets.ModelViewSet):
queryset = Product.objects.all()
serializer_class = ProductSerializer
permission_classes = (IsAuthenticated,)
@api_view(["GET"])
@authentication_classes(
[
ApiGatewayAuthentication,
]
)
@permission_classes([])
def apisix_test_request(request):
"""Test API request so we can see how the APISIX integration works."""

def decode_x_header(header):
x_userinfo = request.META.get(header, False)

if not x_userinfo:
return f"No {header} header"

decoded_x_userinfo = base64.b64decode(x_userinfo)
return json.loads(decoded_x_userinfo)

response = {
"name": "Response ok!",
"user": request.user.username if request.user is not None else None,
"x_userinfo": decode_x_header("HTTP_X_USERINFO"),
"x_id_token": decode_x_header("HTTP_X_ID_TOKEN"),
}

return Response(response, status=status.HTTP_200_OK)


@api_view(["GET"])
@permission_classes([])
def traefik_test_request(request):
"""Test API request so we can see how the Traefik integration works."""

response = {
"name": "Response ok!",
"user": request.user.username if request.user is not None else None,
"metas": [f"{key}: {value}" for key, value in request.META.items()],
}

return Response(response, status=status.HTTP_200_OK)


@api_view(["GET"])
@permission_classes([IsAuthenticated])
def authed_traefik_test_request(request):
"""Test API request so we can see how the Traefik integration works."""

response = {
"name": "Response ok!",
"user": request.user.username if request.user is not None else None,
"metas": [f"{key}: {value}" for key, value in request.META.items()],
}

return Response(response, status=status.HTTP_200_OK)

0 comments on commit c7ba87b

Please sign in to comment.