-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from softwareone-platform/MPT-6085_add_cli_for…
…_utils MPT-6085 add cli for future utils; move openapi generation script to …
- Loading branch information
Showing
12 changed files
with
147 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,3 +172,7 @@ bandit.json | |
|
||
# Postgres data | ||
pg_data/ | ||
|
||
|
||
# OpenAPI default spec file | ||
ffc_operations_openapi_spec.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from app.cli import app | ||
|
||
if __name__ == "__main__": | ||
app() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import json | ||
from enum import Enum | ||
from pathlib import Path | ||
from typing import Annotated | ||
|
||
import typer | ||
import yaml | ||
from fastapi.openapi.utils import get_openapi | ||
|
||
|
||
class OutputFormat(str, Enum): | ||
json = "json" | ||
yaml = "yaml" | ||
|
||
|
||
app = typer.Typer( | ||
add_completion=False, | ||
rich_markup_mode="rich", | ||
) | ||
|
||
|
||
@app.command() | ||
def openapi( | ||
output: Annotated[ | ||
Path | None, | ||
typer.Option( | ||
"--output", | ||
"-o", | ||
help="Output file", | ||
), | ||
] = Path("ffc_operations_openapi_spec.yml"), | ||
output_format: Annotated[ | ||
OutputFormat, | ||
typer.Option( | ||
"--output-format", | ||
"-f", | ||
help="Output file format", | ||
), | ||
] = OutputFormat.yaml, | ||
): | ||
""" | ||
Generates the OpenAPI spec file. | ||
""" | ||
from app import main | ||
|
||
dump_fn = json.dump if output_format == OutputFormat.json else yaml.dump | ||
spec = get_openapi( | ||
title=main.app.title, | ||
version=main.app.version, | ||
openapi_version=main.app.openapi_version, | ||
description=main.app.description, | ||
routes=main.app.routes, | ||
) | ||
with open(output, "w") as f: # type: ignore | ||
dump_fn(spec, f, indent=2) | ||
|
||
|
||
@app.callback() | ||
def main( | ||
ctx: typer.Context, | ||
): | ||
from app import settings | ||
|
||
ctx.obj = settings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import json | ||
import shlex | ||
from pathlib import Path | ||
|
||
import yaml | ||
from pytest_mock import MockerFixture | ||
from typer.testing import CliRunner | ||
|
||
from app.cli import app | ||
|
||
|
||
def test_openapi(mocker: MockerFixture): | ||
spec = {"test": "openapi"} | ||
mocker.patch("app.cli.get_openapi", return_value=spec) | ||
mocked_open = mocker.mock_open() | ||
mocker.patch("app.cli.open", mocked_open) | ||
|
||
runner = CliRunner() | ||
result = runner.invoke(app, "openapi") | ||
|
||
assert result.exit_code == 0 | ||
mocked_open.assert_called_once_with(Path("ffc_operations_openapi_spec.yml"), "w") | ||
written_data = "".join(call.args[0] for call in mocked_open().write.call_args_list) | ||
assert written_data == yaml.dump(spec, indent=2) | ||
|
||
|
||
def test_openapi_custom_output(mocker: MockerFixture): | ||
spec = {"test": "openapi"} | ||
mocker.patch("app.cli.get_openapi", return_value=spec) | ||
mocked_open = mocker.mock_open() | ||
mocker.patch("app.cli.open", mocked_open) | ||
|
||
runner = CliRunner() | ||
result = runner.invoke(app, shlex.split("openapi -o openapi.json -f json")) | ||
|
||
assert result.exit_code == 0 | ||
mocked_open.assert_called_once_with(Path("openapi.json"), "w") | ||
written_data = "".join(call.args[0] for call in mocked_open().write.call_args_list) | ||
assert written_data == json.dumps(spec, indent=2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.