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

Generate OpenAPI Schema #166

Merged
merged 5 commits into from
Jul 12, 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
17 changes: 8 additions & 9 deletions .github/disabled_workflows/generate-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@ name: Generate and save documentation of API

on:
pull_request:
types:
- closed
types: [closed]

jobs:
generar-y-guardar-documentacion:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest

steps:
- name: Checkout del repositorio
- name: Checkout repository
uses: actions/checkout@v2
with:
persist-credentials: false
token: ${{ secrets.PAT }}

- name: Configurar Python
- name: Configure Python
uses: actions/setup-python@v2
with:
python-version: 3.11
Expand All @@ -30,11 +29,11 @@ jobs:
pip install -r requirements.txt
python generate-docs.py
env:
MONGO_URI : ${{ secrets.MONGO_URI }}
SECRET_KEY_SIGN : ${{ secrets.SECRET_KEY_SIGN }}
SERVERLESS_FUNCTION_URL : ${{ secrets.SERVERLESS_FUNCTION_URL }}
ARCH : ${{ secrets.ARCH }}
ENV_VALUE : ${{ secrets.ENV_VALUE }}
MONGO_URI : none
SECRET_KEY_SIGN : none
SERVERLESS_FUNCTION_URL : none
ARCH : BLOB
ENV_VALUE : PROD

- name: Add and commit changes
working-directory: Backend/
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/generate-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ permissions:
contents: write
jobs:
deploy:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
51 changes: 51 additions & 0 deletions .github/workflows/generate-openapi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Generate OpenAPI Schema

on:
pull_request:
types:
- closed

jobs:
generar-y-guardar-documentacion:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest

steps:
- name: Checkout del repositorio
uses: actions/checkout@v2
with:
persist-credentials: false
token: ${{ secrets.PAT }}

- name: Configure Python
uses: actions/setup-python@v2
with:
python-version: 3.11

- name: Install dependencies
working-directory: Backend/
run: |
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
python -m app.tools.generate_openapi
env:
MONGO_URI : none
SECRET_KEY_SIGN : none
SERVERLESS_FUNCTION_URL : none
ARCH : BLOB
ENV_VALUE : PROD

- name: Add and commit changes
working-directory: Electron/src/swagger/
run: |
git config --local user.email "actions@github.com"
git config --local user.name "GitHub Actions"
git add openapi.json
git commit -m "docs: Update OpenAPI Schema"

- name: Pushing to the protected branch 'protected'
uses: CasperWA/push-protected@v2
with:
token: ${{ secrets.PAT }}
branch: ${{ github.base_ref }}
4 changes: 2 additions & 2 deletions Backend/app/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ async def lifespan_handler(app: FastAPI):

app = FastAPI(
title="SpotifyElectronAPI",
description="API created with FastAPI Python to serve \
as backend for Spotify Electron music streaming Desktop App",
description="API created with FastAPI Python to serve\
as backend for Spotify Electron music streaming Desktop App",
version="1.0.0",
lifespan=lifespan_handler,
)
Expand Down
53 changes: 53 additions & 0 deletions Backend/app/tools/generate_openapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Generate OpenAPI Schema from app

Steps:
1. Go to Backend/
2. Run `python -m app.tools.generate_openapi`
3. OpenAPI Schema will be located at OPENAPI_SCHEMA_OUTPUT_FILE path
"""

import asyncio
import json
import os

from ..__main__ import app, lifespan_handler

OPENAPI_SCHEMA_OUTPUT_FOLDER = "../Electron/src/swagger/"
OPENAPI_SCHEMA_OUTPUT_FILE = f"{OPENAPI_SCHEMA_OUTPUT_FOLDER}openapi.json"


def check_openapi_folder_exists() -> bool:
"""Checks if folder that has to store OpenAPI file exists

Returns:
-------
bool: if the OpenAPI folder exists
"""
cwd = os.path.abspath(os.getcwd())
print(f"> Current Working Directory: {cwd}")
openapi_expected_path_folder = os.path.join(
os.path.abspath(os.getcwd()), OPENAPI_SCHEMA_OUTPUT_FOLDER
)
print(f"> Checking if {openapi_expected_path_folder} folder exists")
if not os.path.exists(openapi_expected_path_folder):
print(f"> Folder {openapi_expected_path_folder} doesnt exists")
return False
print(f"> Folder {openapi_expected_path_folder} exists")
return True


async def generate_openapi() -> None:
"""Generates OpenAPI schema and writes it into a file"""
if not check_openapi_folder_exists():
return
async with lifespan_handler(app):
print("> Generating OpenAPI Schema")
openapi = app.openapi()
openapi_json = json.dumps(openapi, indent=2)
with open(OPENAPI_SCHEMA_OUTPUT_FILE, "w") as file:
print(f"> Writing OpenAPI Schema on {OPENAPI_SCHEMA_OUTPUT_FILE}")
file.write(openapi_json)


if __name__ == "__main__":
asyncio.run(generate_openapi())
1 change: 1 addition & 0 deletions Electron/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/src/swagger/**
4 changes: 2 additions & 2 deletions Electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"build:renderer": "cross-env NODE_ENV=production TS_NODE_TRANSPILE_ONLY=true webpack --config ./.erb/configs/webpack.config.renderer.prod.ts",
"postinstall": "ts-node .erb/scripts/check-native-dep.js && electron-builder install-app-deps && cross-env NODE_ENV=development TS_NODE_TRANSPILE_ONLY=true webpack --config ./.erb/configs/webpack.config.renderer.dev.dll.ts",
"lint": "cross-env NODE_ENV=development eslint src/ --ext .js,.jsx,.ts,.tsx --fix",
"format:write": "prettier src/ --write",
"format:check": "prettier src/ --check",
"format:write": "prettier src/ --write --ignore-path .prettierignore",
"format:check": "prettier src/ --check --ignore-path .prettierignore",
"package": "ts-node ./.erb/scripts/clean.js dist && npm run build && electron-builder build --publish never",
"rebuild": "electron-rebuild --parallel --types prod,dev,optional --module-dir release/app",
"start": "ts-node ./.erb/scripts/check-port-in-use.js && npm run start:renderer",
Expand Down
Loading