Skip to content

Commit 83b40ba

Browse files
redjaxredjax
and
redjax
authored
Topic/improvements (#229)
* feat(improve-fastapi): Add fix_api_docs (#227) This function fixes errors loading /docs when a root_path is set Co-authored-by: redjax <[email protected]> * fix(diskcache): Rename check_cache_key_exists() -> search_cache() (#228) Co-authored-by: redjax <[email protected]> * chore(format): Check & format code --------- Co-authored-by: redjax <[email protected]>
1 parent 9db18fd commit 83b40ba

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

red_utils/diskcache_utils/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
)
1212
from .operations import (
1313
check_cache,
14-
check_cache_key_exists,
1514
clear_cache,
1615
convert_to_seconds,
1716
delete_val,
1817
get_cache_size,
1918
get_val,
2019
manage_cache_tag_index,
2120
new_cache,
21+
search_cache,
2222
set_expire,
2323
set_val,
2424
)

red_utils/diskcache_utils/operations.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def clear_cache(cache: Cache = None) -> bool:
116116
)
117117

118118

119-
def check_cache_key_exists(key: str = None, cache: diskcache.core.Cache = None) -> bool:
119+
def search_cache(key: str = None, cache: diskcache.core.Cache = None) -> bool:
120120
"""Check if a key exists in a cache."""
121121
## Key validation
122122
validate_key(key=key)
@@ -198,7 +198,7 @@ def set_expire(
198198
validate_cache(cache)
199199
validate_expire(expire)
200200

201-
if not check_cache_key_exists(key=key, cache=cache):
201+
if not search_cache(key=key, cache=cache):
202202
return {
203203
"warning": f"Cache item with key [{key}] does not exist in cache at {cache.directory}/"
204204
}
@@ -218,7 +218,7 @@ def get_val(key: valid_key_types = None, cache: Cache = None, tags: list[str] =
218218
validate_cache(cache)
219219
validate_tags(tags)
220220

221-
if check_cache_key_exists(key=key, cache=cache):
221+
if search_cache(key=key, cache=cache):
222222
try:
223223
with cache as ref:
224224
_val = ref.get(key=key)

red_utils/fastapi_utils/operations.py

+17
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@
2323
from fastapi import APIRouter, FastAPI
2424
from fastapi.middleware.cors import CORSMiddleware
2525

26+
def fix_api_docs(app: FastAPI):
27+
"""Fix Not Found /api/v1/openapi.json error when a root_path is set.
28+
29+
NOTE: This is *only* necessary if you set a root_path in the FastAPI app,
30+
i.e. app = FastAPI(root_path="/some/path").
31+
32+
If a FastAPI app is initialized with root_path=$some_path, the /docs
33+
route breaks. Pass the FastAPI app to through this function to fix,
34+
i.e.:
35+
fix_api_docs(app)
36+
"""
37+
38+
@app.get(app.root_path + "/openapi.json")
39+
def custom_swagger_ui_html():
40+
return app.openapi()
41+
42+
2643
def update_tags_metadata(
2744
tags_metadata: list = tags_metadata,
2845
update_metadata: Union[list[dict[str, str]], dict[str, str]] = None,

0 commit comments

Comments
 (0)