Commit 034c4174 authored by Arnaud Vergnet's avatar Arnaud Vergnet
Browse files

feat: allow configuring openapi server url

parent 74d5bf3fe4f5
Pipeline #136079 passed with stages
in 53 seconds
import logging
from os import path
from typing import Union, Dict, Optional
from typing import Union, Dict, Optional, Sequence
from cubicweb import ConfigurationError
from openapi_spec_validator.schemas import read_yaml_file
......@@ -16,7 +16,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
from cubicweb_api.util import get_cw_all_in_one_config, get_api_server_base
log = logging.getLogger(__name__)
......@@ -28,7 +28,7 @@
def get_production_file_path(config: Configurator) -> str:
return path.join(get_cw_repo(config).config.apphome, "openapi.yaml")
return path.join(get_cw_all_in_one_config(config).apphome, "openapi.yaml")
def generate_openapi_file(config: Configurator, path_prefix: str):
......@@ -43,6 +43,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
servers_array: Optional[Sequence[dict]] = spec_dict.get("servers")
for server_item in servers_array:
server_item["url"] = get_api_server_base(config)
with open(get_production_file_path(config), "w") as file:
dump(spec_dict, file)
......
......@@ -9,6 +9,8 @@
email: contact@logilab.fr
title: CubicWeb API
version: 0.1.0
servers:
- url: http://localhost/
paths:
/schema:
x-pyramid-route-name: schema
......
......@@ -28,4 +28,15 @@
"level": 2,
},
),
(
"api-server-name",
{
"type": "string",
"default": None,
"help": "The base url to use for openapi validation. "
"Set this to the CubicWeb base url if openapi cannot find the server.",
"group": "api",
"level": 2,
},
),
)
......@@ -18,6 +18,7 @@
from typing import Union
from cubicweb import ConfigurationError
from cubicweb.pyramid.config import AllInOneConfiguration
from cubicweb.pyramid.core import CubicWebPyramidRequest
from cubicweb.server.repository import Repository
from pyramid.config import Configurator
......@@ -34,6 +35,32 @@
return req_or_conf.registry["cubicweb.repository"]
def get_cw_all_in_one_config(config: Configurator) -> AllInOneConfiguration:
return get_cw_repo(config).config
def get_api_server_base(config: Configurator):
repo = get_cw_repo(config)
server_name: str = (
repo.get_option_value("api-server-name")
or get_cw_all_in_one_config(config).default_base_url()
)
match = re.match("^[a-zA-Z0-9-_@.&+!*(),/:]{1,60}$", server_name)
if match:
return server_name
else:
if len(server_name) > 60:
raise ConfigurationError(
f"api-server-name '{server_name}' is too long. "
f"Max size allowed is 60 characters. "
f"Current size is {len(server_name)}."
)
raise ConfigurationError(
f"api-server-name '{server_name}' contains invalid characters. "
"Allowed characters are: a-zA-Z0-9-_@.&+!*(),/:"
)
def get_api_path_prefix(config: Configurator) -> str:
repo = get_cw_repo(config)
path_prefix: str = (
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment