Skip to content

Commit

Permalink
feat: unify CLI parameter of OpenAPI / GQL spec dump command (#1691)
Browse files Browse the repository at this point in the history
Backported-from: main
Backported-to: 23.09
  • Loading branch information
Yaminyam authored and achimnol committed Dec 20, 2023
1 parent 863210b commit 6f7c176
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
1 change: 1 addition & 0 deletions changes/1691.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add the --output option to the function that outputs gql and openapi and unify the output format.
26 changes: 22 additions & 4 deletions src/ai/backend/manager/cli/api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from __future__ import annotations

import asyncio
import logging
from pathlib import Path
from typing import TYPE_CHECKING

import aiofiles
import click
import graphene

Expand All @@ -19,9 +22,24 @@ def cli(args) -> None:
pass


async def generate_gql_schema(output_path: Path) -> None:
schema = graphene.Schema(query=Queries, mutation=Mutations, auto_camelcase=False)
if output_path == "-":
log.info("======== GraphQL API Schema ========")
print(str(schema))
else:
async with aiofiles.open(output_path, "w") as fw:
await fw.write(str(schema))


@cli.command()
@click.pass_obj
def dump_gql_schema(cli_ctx: CLIContext) -> None:
schema = graphene.Schema(query=Queries, mutation=Mutations, auto_camelcase=False)
log.info("======== GraphQL API Schema ========")
print(str(schema))
@click.option(
"--output",
"-o",
default="-",
type=click.Path(dir_okay=False, writable=True),
help="Output file path (default: stdout)",
)
def dump_gql_schema(cli_ctx: CLIContext, output: Path) -> None:
asyncio.run(generate_gql_schema(output))
20 changes: 14 additions & 6 deletions src/ai/backend/manager/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,18 +290,26 @@ async def generate_openapi(output_path: Path) -> None:
route_def["parameters"] = parameters
route_def["description"] = "\n".join(description)
openapi["paths"][path][method.lower()] = route_def

async with aiofiles.open(output_path, mode="w") as fw:
await fw.write(json.dumps(openapi, ensure_ascii=False, indent=2))
if output_path == "-" or output_path is None:
print(json.dumps(openapi, ensure_ascii=False, indent=2))
else:
async with aiofiles.open(output_path, mode="w") as fw:
await fw.write(json.dumps(openapi, ensure_ascii=False, indent=2))


@click.command()
@click.argument("OUTPUT_PATH")
def main(output_path: Path) -> None:
@click.option(
"--output",
"-o",
default="-",
type=click.Path(dir_okay=False, writable=True),
help="Output file path (default: stdout)",
)
def main(output: Path) -> None:
"""
Generates OpenAPI specification of Backend.AI API.
"""
asyncio.run(generate_openapi(output_path))
asyncio.run(generate_openapi(output))


if __name__ == "__main__":
Expand Down

0 comments on commit 6f7c176

Please sign in to comment.