feat: add openapi client generator script

refs #142
This commit is contained in:
fusion44 2022-08-07 07:38:02 +02:00
parent c2f83358db
commit 1fdfd19c6f
No known key found for this signature in database
GPG key ID: 645FA807E935D9D5
5 changed files with 4313 additions and 1 deletions

View file

@ -8,7 +8,8 @@ help:
@echo "To test the project type 'make test'"
@echo "To assess test coverage type 'make coverage'"
@echo "To generate the requirements.txt file for pip type 'make requirements'"
@echo "To sync current changes to a blitz for testing, type 'make sync-to-blitz'. Adjust connection values in scripts/push_to_blitz.sh"
@echo "To sync current changes to a blitz for testing, type 'make sync-to-blitz'.\n Adjust connection values in scripts/push_to_blitz.sh"
@echo "To generate the client libraries type 'make generate-client-libs'"
@echo "------------------------------------"
clean:
@ -40,3 +41,6 @@ sync-to-blitz:
pre-commit:
pre-commit run --all-files
generate-client-libs:
python gen_client_libs.py

View file

@ -112,6 +112,26 @@ poetry export -f requirements.txt --output requirements.txt
> :information_source: This will skip all dev dependencies by default.\
> This step is required to avoid having to install poetry for final deployment.
### Generating the client libraries
> The client library live in an extra repository:
https://github.com/fusion44/blitz_api_client_libraries
Install [OpenAPI Generator](https://openapi-generator.tech) and Java:
```sh
npm install @openapitools/openapi-generator-cli -g
sudo apt install default-jre
```
Clone https://github.com/fusion44/blitz_api_client_libraries next to the blitz_api folder.
```sh
make generate-client-libs
```
> ⚠️ The first run requires `sudo` as it must download a Java .jar file to the system npm package folder.
### Testing
Make sure to include tests for important pieces of submitted code.

82
gen_client_libs.py Normal file
View file

@ -0,0 +1,82 @@
import importlib
import json
import os
import shutil
from subprocess import PIPE, Popen
from fastapi.openapi.utils import get_openapi
# npm install @openapitools/openapi-generator-cli -g
# sudo apt install default-jre
# For now assume that the clients repository is a sibling to the current working directory.
out_path = os.path.abspath(os.path.join("../", f"blitz_api_client_libraries"))
mod = importlib.import_module(f"app.main")
app = getattr(mod, "app")
version = "v1"
if version:
for route in app.router.routes:
if version in route.path:
app = route.app
break
def _generate(generator: str):
p = os.path.abspath(os.path.join(out_path, f"clients/{generator}"))
print(f"Removing {p}")
if os.path.exists(p):
shutil.rmtree(p)
print(f"Recreating directory {p}")
os.makedirs(p, exist_ok=True)
print(f"Generating client files {generator}")
process = Popen(
[
"openapi-generator-cli",
"generate",
"-i",
"openapi.json",
"-g",
generator,
"-o",
p,
],
stdout=PIPE,
stderr=PIPE,
)
_, stderr = process.communicate()
if stderr:
print(stderr)
def main():
# Gets the OpenAPI specs.
specs = get_openapi(
title=app.title if app.title else None,
version=app.version if app.version else None,
openapi_version=app.openapi_version if app.openapi_version else None,
description=app.description if app.description else None,
routes=app.routes if app.routes else None,
)
with open(f"openapi.json", "w") as f:
json.dump(specs, f, indent=2)
_generate("dart-dio")
_generate("dart")
_generate("go")
_generate("javascript")
_generate("kotlin")
_generate("python")
_generate("typescript-axios")
_generate("typescript-fetch")
if __name__ == "__main__":
main()

4199
openapi.json Normal file

File diff suppressed because one or more lines are too long

7
openapitools.json Normal file
View file

@ -0,0 +1,7 @@
{
"$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
"spaces": 2,
"generator-cli": {
"version": "6.0.1"
}
}