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

feat: unify CLI parameter of OpenAPI / GQL spec dump command #1691

Merged
merged 4 commits into from
Nov 8, 2023
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
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 @@ -292,18 +292,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
Loading