Skip to content

Commit 70c5980

Browse files
committed
api: add drf-spectacular
1 parent b4505f2 commit 70c5980

6 files changed

+56
-5
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ db-sources
44
hhub/migrations
55
__pycache__
66
*.sqlite3
7+
# The schema is autogenerated
8+
schema.yml

docker-entrypoint.sh

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ until postgres_ready; do
2323

2424
case "$1" in
2525
"dev_web_start")
26+
27+
echo "==> Generating OpenAPI schema..."
28+
python manage.py spectacular --color --file schema.yml
29+
2630
echo "==> Creating migrations..."
2731
python manage.py makemigrations hhub
2832

hhub/settings.py

+14
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,22 @@
5151
"corsheaders",
5252
# Used to run separate scripts that interact with the models (sync_db)
5353
"django_extensions",
54+
"drf_spectacular",
5455
]
5556

57+
# REST Framework
58+
# https://www.django-rest-framework.org/api-guide/settings/
59+
60+
REST_FRAMEWORK = {
61+
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
62+
}
63+
64+
SPECTACULAR_SETTINGS = {
65+
"TITLE": "Homebrew Hub",
66+
"DESCRIPTION": " A digital repository of of homebrew games, patches, hackroms for old consoles.",
67+
"VERSION": "1.0.0",
68+
}
69+
5670
MIDDLEWARE = [
5771
"django.middleware.security.SecurityMiddleware",
5872
"django.contrib.sessions.middleware.SessionMiddleware",

hhub/urls.py

+12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
"""
1616

1717
from django.urls import include, path
18+
from drf_spectacular.views import (
19+
SpectacularAPIView,
20+
SpectacularSwaggerView,
21+
)
1822

1923
from hhub import views
2024

@@ -24,6 +28,14 @@
2428
r"api/",
2529
include(
2630
[
31+
# Serve the OpenAPI schema
32+
path("schema/", SpectacularAPIView.as_view(), name="schema"),
33+
# Serve the Swagger UI to browse the OpenAPI schema
34+
path(
35+
"schema/swagger-ui/",
36+
SpectacularSwaggerView.as_view(url_name="schema"),
37+
name="swagger-ui",
38+
),
2739
path("entry/<slug:pk>.json", views.entry_manifest),
2840
path("all", views.entries_all),
2941
path("stats", views.stats),

hhub/views.py

+11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
from hhub.models import Entry
99
from hhub.serializers import EntrySerializer
1010

11+
from drf_spectacular.utils import extend_schema
12+
from drf_spectacular.types import OpenApiTypes
13+
14+
from rest_framework.decorators import api_view
15+
1116

1217
def entry_manifest(request, pk):
1318
"""
@@ -190,6 +195,12 @@ def search_entries(request):
190195
)
191196

192197

198+
@extend_schema(
199+
summary="Get statistics about the entries in the database",
200+
description="Retrieve aggregated counts of entries by type, tags, and platforms.",
201+
responses={200: OpenApiTypes.OBJECT},
202+
)
203+
@api_view(["GET"])
193204
def stats(request):
194205
entries = Entry.objects
195206
data = {

requirements.txt

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
asgiref==3.8.1
2+
attrs==24.2.0
23
black==24.10.0
34
cfgv==3.4.0
45
click==8.1.7
56
distlib==0.3.9
6-
dj_database_url==2.2.0
7-
Django==4.2.16
7+
dj-database-url==2.2.0
8+
django==4.2.16
89
django-cors-headers==4.5.0
910
django-extensions==3.2.3
1011
djangorestframework==3.15.2
12+
drf-spectacular==0.28.0
1113
filelock==3.16.1
1214
identify==2.6.1
15+
inflection==0.5.1
1316
isort==5.13.2
17+
jsonschema==4.23.0
18+
jsonschema-specifications==2024.10.1
1419
mypy-extensions==1.0.0
1520
nodeenv==1.9.1
1621
pathspec==0.12.1
@@ -19,10 +24,13 @@ pre-commit==4.0.1
1924
psycopg2==2.9.10
2025
python-dateutil==2.9.0.post0
2126
pytz==2024.2
22-
PyYAML==6.0.2
27+
pyyaml==6.0.2
28+
referencing==0.35.1
29+
rpds-py==0.22.3
30+
six==1.17.0
2331
sqlparse==0.5.1
2432
toml==0.10.2
2533
tomli==2.0.2
26-
typing_extensions==4.12.2
34+
typing-extensions==4.12.2
35+
uritemplate==4.1.1
2736
virtualenv==20.27.0
28-
six==1.17.0

0 commit comments

Comments
 (0)