From 054e693de514e27b442f6eeb19fccf284389e8a3 Mon Sep 17 00:00:00 2001 From: WellingtonLFaria Date: Mon, 23 Sep 2024 18:26:24 -0300 Subject: [PATCH 1/8] func: Adiciona drf-spectacular ao arquivo requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 8aa8876..c49bda8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ python-dotenv==1.0.1 sqlparse==0.5.1 typing_extensions==4.12.2 tzdata==2024.1 +drf-spectacular \ No newline at end of file From d6ff4463377b1bbd5e2d81173482f8ce1b826409 Mon Sep 17 00:00:00 2001 From: WellingtonLFaria Date: Mon, 23 Sep 2024 18:29:15 -0300 Subject: [PATCH 2/8] func: Adiciona o drf_spectacular no INSTALLED_APPS no arquivo settings.py --- src/tupan/tupan/settings.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/tupan/tupan/settings.py b/src/tupan/tupan/settings.py index 25a14d8..db267a0 100644 --- a/src/tupan/tupan/settings.py +++ b/src/tupan/tupan/settings.py @@ -45,6 +45,7 @@ 'rest_framework', 'alertas', 'estacoes' + 'drf_spectacular', ] MIDDLEWARE = [ From f55394f5062f0111c0a4fe63993e14e10b46aa49 Mon Sep 17 00:00:00 2001 From: WellingtonLFaria Date: Mon, 23 Sep 2024 18:31:02 -0300 Subject: [PATCH 3/8] func: Configura o drf_spectacular como o schema class padrao do rest_framework --- src/tupan/tupan/settings.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/tupan/tupan/settings.py b/src/tupan/tupan/settings.py index db267a0..2029560 100644 --- a/src/tupan/tupan/settings.py +++ b/src/tupan/tupan/settings.py @@ -48,6 +48,10 @@ 'drf_spectacular', ] +REST_FRAMEWORK = { + 'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema', +} + MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', From 79134a92807257d99d1175ee381bbdefcdbb6634 Mon Sep 17 00:00:00 2001 From: WellingtonLFaria Date: Mon, 23 Sep 2024 18:33:12 -0300 Subject: [PATCH 4/8] func: Adiciona alguns metadados para o spectacular --- src/tupan/tupan/settings.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/tupan/tupan/settings.py b/src/tupan/tupan/settings.py index 2029560..6dcf951 100644 --- a/src/tupan/tupan/settings.py +++ b/src/tupan/tupan/settings.py @@ -52,6 +52,13 @@ 'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema', } +SPECTACULAR_SETTINGS = { + 'TITLE': 'Tupã API', + 'DESCRIPTION': '', + 'VERSION': '1.0.0', + 'SERVE_INCLUDE_SCHEMA': False, +} + MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', From 34c3fb71e153cb0372567c9970143fbbc4b9f447 Mon Sep 17 00:00:00 2001 From: WellingtonLFaria Date: Mon, 23 Sep 2024 18:37:44 -0300 Subject: [PATCH 5/8] docs: Documentando a geracao do schema usado pelo swagger --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a96a801..8a1a985 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ # Base de conhecimento TUPAN-BACK - ## Ambiente ### Criar, ativar e instalar as dependencias do ambiente virtual @@ -21,6 +20,11 @@ cp ./src/.env_sample ./src/.env ``` Logo em seguida altere as variáveis para os valores desejados. +### Gerando o schema para o swagger +Execute o seguinte comando no terminal para gerar o .yml que será utilizado pelo swagger: +``` +python ./src/tupan/manage.py spectacular --color --file schema.yml +``` ## Testes From b511b3c991848ef13b1df754448541609a9c98cc Mon Sep 17 00:00:00 2001 From: WellingtonLFaria Date: Mon, 23 Sep 2024 18:40:18 -0300 Subject: [PATCH 6/8] func: Adicionando os endpoints para o swagger, redoc e para o download do schema das rotas da aplicacao --- src/tupan/tupan/urls.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/tupan/tupan/urls.py b/src/tupan/tupan/urls.py index 903bfe4..2fd62b6 100644 --- a/src/tupan/tupan/urls.py +++ b/src/tupan/tupan/urls.py @@ -16,8 +16,13 @@ """ from django.contrib import admin from django.urls import include, path +from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView urlpatterns = [ path('admin/', admin.site.urls), - path('', include('estacoes.urls')) + path('', include('estacoes.urls')), + path('admin/', admin.site.urls), + path('api/schema/', SpectacularAPIView.as_view(), name='schema'), + path('api/schema/swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'), + path('api/schema/redoc/', SpectacularRedocView.as_view(url_name='schema'), name='redoc'), ] From 9306ea1000ee9f0513fdb54af4eb9463b7c01d81 Mon Sep 17 00:00:00 2001 From: WellingtonLFaria Date: Mon, 23 Sep 2024 18:46:27 -0300 Subject: [PATCH 7/8] docs: Documentando como acessar o swagger-ui --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 8a1a985..c02d361 100644 --- a/README.md +++ b/README.md @@ -20,12 +20,18 @@ cp ./src/.env_sample ./src/.env ``` Logo em seguida altere as variáveis para os valores desejados. +## Swagger + ### Gerando o schema para o swagger Execute o seguinte comando no terminal para gerar o .yml que será utilizado pelo swagger: ``` python ./src/tupan/manage.py spectacular --color --file schema.yml ``` +### Como acessar o swagger +Rode a aplicação e acesse o endpoint: +> /api/schema/swagger-ui + ## Testes ### Para rodar testes: From fe3c490d547449969646e779d97966dd76405b17 Mon Sep 17 00:00:00 2001 From: WellingtonLFaria Date: Mon, 23 Sep 2024 18:51:02 -0300 Subject: [PATCH 8/8] func: Adicionando schema.yml ao .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index d443543..1af7677 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ __pycache__/ /node_modules .env .pytest_cache -__pycache__ \ No newline at end of file +__pycache__ +schema.yml \ No newline at end of file