Skip to content

Commit

Permalink
feat(runner): add ability to export YAML OpenAPI specs (#62)
Browse files Browse the repository at this point in the history
This commit gives developers the ability to also export YAML based
OpenAPI specs. These can be useful when wanting to create automatic API
documentation using documentation software like https://mintlify.com/.
  • Loading branch information
rickstaa committed Apr 19, 2024
1 parent a255fc0 commit 7e85929
Show file tree
Hide file tree
Showing 2 changed files with 380 additions and 12 deletions.
58 changes: 46 additions & 12 deletions runner/gen_openapi.py
Expand Up @@ -2,28 +2,62 @@
from app.main import app, use_route_names_as_operation_ids
from app.routes import health, text_to_image, image_to_image, image_to_video
import json
import yaml
import argparse


def write_openapi_json(fname):
def write_openapi(fname):
"""Write OpenAPI schema to file.
Args:
fname (str): The file name to write to. The file extension determines the file
type. Either 'json' or 'yaml'.
"""
app.include_router(health.router)
app.include_router(text_to_image.router)
app.include_router(image_to_image.router)
app.include_router(image_to_video.router)

use_route_names_as_operation_ids(app)

# Write OpenAPI schema to file.
with open(fname, "w") as f:
json.dump(
get_openapi(
title="Livepeer AI Runner",
version="0.1.0",
openapi_version=app.openapi_version,
description="An application to run AI pipelines",
routes=app.routes,
),
f,
)
print(f"Writing OpenAPI schema to '{fname}'...")
if fname.endswith(".yaml"):
yaml.dump(
get_openapi(
title="Livepeer AI Runner",
version="0.1.0",
openapi_version=app.openapi_version,
description="An application to run AI pipelines",
routes=app.routes,
),
f,
sort_keys=False,
)
else:
json.dump(
get_openapi(
title="Livepeer AI Runner",
version="0.1.0",
openapi_version=app.openapi_version,
description="An application to run AI pipelines",
routes=app.routes,
),
f,
)
print("OpenAPI schema generated and saved.")


if __name__ == "__main__":
write_openapi_json("openapi.json")
parser = argparse.ArgumentParser()
parser.add_argument(
"--type",
type=str,
choices=["json", "yaml"],
default="json",
help="File type to write to, either 'json' or 'yaml'. Default is 'json'",
)
args = parser.parse_args()

write_openapi(f"openapi.{args.type.lower()}")

0 comments on commit 7e85929

Please sign in to comment.