diff --git a/api/common/data_handlers/__init__.py b/api/common/data_handlers/__init__.py index 3ffdcb0..ae88d8c 100644 --- a/api/common/data_handlers/__init__.py +++ b/api/common/data_handlers/__init__.py @@ -8,5 +8,5 @@ if ROLE == "all" or ROLE == "worker": import siibra - siibra.warm_cache() + siibra.warm_cache(siibra.WarmupLevel.INSTANCE) diff --git a/api/server/api.py b/api/server/api.py index d239e0a..85408de 100644 --- a/api/server/api.py +++ b/api/server/api.py @@ -41,11 +41,11 @@ siibra_api.include_router(vocabolaries_router, prefix="/vocabularies") add_pagination(siibra_api) +add_sample_code(siibra_api) # Versioning for api endpoints siibra_api = VersionedFastAPI(siibra_api) -add_sample_code(siibra_api) templates = Jinja2Templates(directory="templates/") siibra_api.mount("/static", StaticFiles(directory="static"), name="static") diff --git a/api/server/code_snippet.py b/api/server/code_snippet.py index 2564cab..8b38612 100644 --- a/api/server/code_snippet.py +++ b/api/server/code_snippet.py @@ -69,45 +69,40 @@ def get_sourcecode(request: Request, lang: str="python") -> str: raise NotFound def add_sample_code(rootapp: FastAPI): + app = rootapp - for route in rootapp.routes: - if (isinstance(route, Mount) - and route.path == "/v3_0" - and isinstance(route.app, FastAPI) - ): - app = route.app - def custom_api(): - if app.openapi_schema: - return app.openapi_schema - - openapi_schema = get_openapi( - title="siibra-api", - version="v3", - summary="siibra-api openapi specification", - description="siibra-api is a http wrapper around siibra-python", - routes=app.routes, - ) - - id_to_route = { - route.operation_id or route.unique_id: route - for route in app.routes - if isinstance(route, APIRoute) - } + def custom_api(): + if app.openapi_schema: + return app.openapi_schema + + openapi_schema = get_openapi( + title="siibra-api", + version="v3", + summary="siibra-api openapi specification", + description="siibra-api is a http wrapper around siibra-python", + routes=app.routes, + ) + + id_to_route = { + route.operation_id or route.unique_id: route + for route in app.routes + if isinstance(route, APIRoute) + } - for path_value in openapi_schema.get("paths").values(): - for method_value in path_value.values(): - op_id = method_value.get("operationId") - if op_id not in id_to_route: - continue - route = id_to_route[op_id] - if route.name not in name_to_fns_map: - continue - fn0, fn1 = name_to_fns_map[route.name] - method_value["x-codeSamples"] = [{ - "lang": lang, - "source": code, - } for lang, code in yield_codeblock(fn0.__doc__)] + for path_value in openapi_schema.get("paths").values(): + for method_value in path_value.values(): + op_id = method_value.get("operationId") + if op_id not in id_to_route: + continue + route = id_to_route[op_id] + if route.name not in name_to_fns_map: + continue + fn0, fn1 = name_to_fns_map[route.name] + method_value["x-codeSamples"] = [{ + "lang": lang, + "source": code, + } for lang, code in yield_codeblock(fn0.__doc__)] - app.openapi_schema = openapi_schema - return app.openapi_schema - app.openapi = custom_api + app.openapi_schema = openapi_schema + return app.openapi_schema + app.openapi = custom_api