Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Swagger documentation to Gateway #1205

Merged
merged 18 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gateway/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ def get_serializer_class(self):
return v1_serializers.RuntimeJobSerializer

def get_queryset(self):
# Allow unauthenticated users to read the swagger documentation
if self.request.user is None or not self.request.user.is_authenticated:
return RuntimeJob.objects.none()
return RuntimeJob.objects.all().filter(job__author=self.request.user)


Expand Down
9 changes: 8 additions & 1 deletion gateway/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

Version views inherit from the different views.
"""

import glob
import json
import logging
Expand Down Expand Up @@ -132,6 +133,9 @@ def get_serializer_class(self):
return self.serializer_class

def get_queryset(self):
# Allow unauthenticated users to read the swagger documentation
if self.request.user is None or not self.request.user.is_authenticated:
return Program.objects.none()
return (
Program.objects.all().filter(author=self.request.user).order_by("-created")
)
Expand Down Expand Up @@ -322,7 +326,10 @@ def get_serializer_class(self):
return self.serializer_class

def get_queryset(self):
return Job.objects.all().filter(author=self.request.user).order_by("-created")
# Allow unauthenticated users to read the swagger documentation
if self.request.user is None or not self.request.user.is_authenticated:
return Job.objects.none()
return (Job.objects.all()).filter(author=self.request.user).order_by("-created")

def perform_create(self, serializer):
serializer.save(author=self.request.user)
Expand Down
18 changes: 18 additions & 0 deletions gateway/main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.1/ref/settings/
"""

import os
import os.path
import sys
Expand Down Expand Up @@ -59,6 +60,7 @@
"allauth",
"api",
"psycopg2",
"drf_yasg",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -242,6 +244,17 @@
# 'JWT_AUTH_REFRESH_COOKIE': 'gateway-refresh-token',
}

SWAGGER_SETTINGS = {
"SECURITY_DEFINITIONS": {
"Bearer Token": {
"type": "apiKey",
"name": "Authorization",
"in": "header",
},
},
"USE_SESSION_AUTH": False,
}

SITE_ID = 1
SITE_HOST = os.environ.get("SITE_HOST", "http://localhost:8000")

Expand Down Expand Up @@ -346,3 +359,8 @@
CSP_SCRIPT_SRC = "'none'"
CSP_FRAME_ANCESTORS = "'self'"
CSP_OBJECT_SRC = "'self'"
CSP_IMG_SRC = ("'self'", "data:", "https://cdn.redoc.ly")
CSP_STYLE_SRC_ELEM = ("'self'", "'unsafe-inline'")
CSP_SCRIPT_SRC_ELEM = "'self'"
CSP_CONNECT_SRC = "'self'"
CSP_WORKER_SRC = ("'self'", "blob:")
33 changes: 31 additions & 2 deletions gateway/main/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,31 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""

from django.conf import settings
from django.conf.urls.static import static
from django.urls import path, include, re_path
from django.views.generic import TemplateView
from rest_framework import routers

from rest_framework import routers, permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
import probes.views

schema = get_schema_view( # pylint: disable=invalid-name
openapi.Info(
title="Gateway API",
default_version="v1",
description="List of available API endpoint for gateway.",
),
public=True,
permission_classes=[permissions.AllowAny],
# Patterns to be included in the Swagger documentation
patterns=[
re_path(r"^api/v1/", include(("api.v1.urls", "api"), namespace="v1")),
# Add other included patterns if necessary
],
)

router = routers.DefaultRouter()


Expand All @@ -30,6 +47,18 @@
path("liveness/", probes.views.liveness, name="liveness"),
path("", include("django_prometheus.urls")),
re_path(r"^api/v1/", include(("api.v1.urls", "api"), namespace="v1")),
# docs
re_path(
r"^swagger(?P<format>\.json|\.yaml)$",
schema.without_ui(cache_timeout=0),
name="schema-json",
),
re_path(
r"^swagger/$",
schema.with_ui("swagger", cache_timeout=0),
name="schema-swagger-ui",
),
re_path(r"^redoc/$", schema.with_ui("redoc", cache_timeout=0), name="schema-redoc"),
path(
"DomainVerification.html",
TemplateView.as_view(template_name="DomainVerification.html"),
Expand Down
1 change: 1 addition & 0 deletions gateway/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ kubernetes>=26.1.0
opentelemetry-distro>=0.40b0
opentelemetry-exporter-otlp>=1.19.0
django-concurrency>=2.4
drf-yasg>=1.21.7
Loading