Skip to content

Commit

Permalink
feat: Unify the output format of openapi and gql specifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaminyam committed Nov 6, 2023
1 parent c2af528 commit 81e288d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
25 changes: 21 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,23 @@ 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="-",
help="Output file path (default: stdout)",
)
def dump_gql_schema(cli_ctx: CLIContext, output: Path) -> None:
asyncio.run(generate_gql_schema(output))
19 changes: 13 additions & 6 deletions src/ai/backend/manager/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,18 +292,25 @@ 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="-",
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 81e288d

Please sign in to comment.