Refactor OpenAPI client script and backend server logic.

Simplify and streamline OpenAPI client generation and backend startup logic. Improved error handling, environment configuration, and self-signed SSL certificate management. Added support for generating OpenAPI schema via command-line argument.
This commit is contained in:
GotthardG
2024-12-17 14:50:31 +01:00
parent 176aaa2867
commit 33e3a2d4df
2 changed files with 56 additions and 86 deletions

View File

@ -3,65 +3,55 @@
# Extract values from pyproject.toml
PYPROJECT_FILE="$(dirname "$0")/pyproject.toml"
# Extract name directly and ignore newlines
NAME=$(awk -F'=' '/^name/ { gsub(/"/, "", $2); print $2 }' "$PYPROJECT_FILE" | xargs)
VERSION=$(awk -F'=' '/^version/ { gsub(/"/, "", $2); print $2 }' "$PYPROJECT_FILE" | xargs)
NAME=$(awk -F'= ' '/^name/ { print $2 }' "$PYPROJECT_FILE" | tr -d '"')
VERSION=$(awk -F'= ' '/^version/ { print $2 }' "$PYPROJECT_FILE" | tr -d '"')
if [[ -z "$VERSION" || -z "$NAME" ]]; then
echo "Error: Could not determine version or name from pyproject.toml"
exit 1
fi
# Ensure the extracted name is valid (No spaces or unexpected characters)
if ! [[ "$NAME" =~ ^[a-zA-Z0-9_-]+$ ]]; then
echo "Error: Invalid project name detected: '$NAME'"
if [[ -z "$NAME" || -z "$VERSION" ]]; then
echo "Error: Unable to extract name or version from pyproject.toml."
exit 1
fi
echo "Using project name: $NAME"
echo "Using version: $VERSION"
# Navigate to project root
# Navigate to backend directory
cd "$(dirname "$0")/backend" || exit
# Generate OpenAPI JSON file
python3 main.py generate-openapi
echo "Generating OpenAPI JSON..."
python3 -m main generate-openapi
if [[ ! -f openapi.json ]]; then
echo "Error: openapi.json file not generated!"
echo "Error: Failed to generate openapi.json!"
exit 1
fi
# Download OpenAPI generator CLI if not present
OPENAPI_VERSION=7.8.0
# Download OpenAPI Generator CLI
OPENAPI_VERSION="7.8.0"
if [[ ! -f openapi-generator-cli.jar ]]; then
wget "https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/${OPENAPI_VERSION}/openapi-generator-cli-${OPENAPI_VERSION}.jar" -O openapi-generator-cli.jar
fi
# Run OpenAPI generator with synced name and version
# Generate client
java -jar openapi-generator-cli.jar generate \
-i "$(pwd)/openapi.json" \
-i openapi.json \
-o python-client/ \
-g python \
--additional-properties=packageName="${NAME}client",projectName="${NAME}",packageVersion="${VERSION}" \
--additional-properties=authorName="Guillaume Gotthard",authorEmail="guillaume.gotthard@psi.ch"
--additional-properties=packageName="${NAME}_client",projectName="${NAME}",packageVersion="${VERSION}"
# Check if the generator succeeded
if [[ ! -d python-client ]]; then
echo "OpenAPI generator failed. Exiting."
echo "Error: Failed to generate Python client."
exit 1
fi
# Build Python package
echo "Building Python package..."
# Build the package
cd python-client || exit
python3 -m venv .venv
source .venv/bin/activate
pip install -U pip build
python -m build
python3 -m build
# Verify build output
if [[ ! -d dist || -z "$(ls -A dist)" ]]; then
echo "Error: No files generated in 'dist/'. Package build failed."
echo "Error: Failed to build Python package."
exit 1
fi