Skip to content
Snippets Groups Projects
Commit d1d8ceafcf23 authored by Arnaud Vergnet's avatar Arnaud Vergnet :sun_with_face:
Browse files

fix: write openapi.yaml to cubicweb config dir

parent b974ab06d0c1
No related branches found
No related tags found
1 merge request!16fix: write openapi.yaml to cubicweb config dir
......@@ -17,6 +17,7 @@
from cubicweb_api.constants import API_ROUTE_NAME_PREFIX
from cubicweb_api.httperrors import get_http_error, get_http_500_error
from cubicweb_api.util import get_cw_repo
log = logging.getLogger(__name__)
......@@ -28,7 +29,7 @@
production = "openapi.yaml"
def get_file_in_folder(filename: str) -> str:
return path.join(path.dirname(__file__), filename)
def get_template_file_path() -> str:
return path.join(path.dirname(__file__), OpenApiFiles.template)
......@@ -33,7 +34,11 @@
def generate_openapi_file(path_prefix: str):
spec_dict: Dict = read_yaml_file(get_file_in_folder(OpenApiFiles.template))
def get_production_file_path(config: Configurator) -> str:
return path.join(get_cw_repo(config).config.apphome, OpenApiFiles.production)
def generate_openapi_file(config: Configurator, path_prefix: str):
spec_dict: Dict = read_yaml_file(get_template_file_path())
paths_dict: Optional[Dict[str, Dict]] = spec_dict.get("paths")
edited_path_dict = {}
if paths_dict is None:
......@@ -44,9 +49,9 @@
] = f"{API_ROUTE_NAME_PREFIX}{path_item[OPENAPI_PYRAMID_KEY]}"
edited_path_dict[f"{path_prefix}{path_str}"] = path_item
spec_dict["paths"] = edited_path_dict
with open(get_file_in_folder(OpenApiFiles.production), "w") as file:
with open(get_production_file_path(config), "w") as file:
dump(spec_dict, file)
def register_openapi_routes(config: Configurator, path_prefix: str):
config.include("pyramid_openapi3")
......@@ -48,8 +53,8 @@
dump(spec_dict, file)
def register_openapi_routes(config: Configurator, path_prefix: str):
config.include("pyramid_openapi3")
generate_openapi_file(path_prefix)
generate_openapi_file(config, path_prefix)
# TODO block access if anonymous access is disabled and user is not connected
config.pyramid_openapi3_spec(
......@@ -54,6 +59,6 @@
# TODO block access if anonymous access is disabled and user is not connected
config.pyramid_openapi3_spec(
f"{path.dirname(__file__)}/openapi.yaml",
get_production_file_path(config),
route=f"{path_prefix}/openapi.yaml",
)
config.pyramid_openapi3_add_explorer(route=f"{path_prefix}/openapi")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment