Skip to content

Commit

Permalink
🚚 Change patch_doc argument name from docs_cdn_host to cdn_host
Browse files Browse the repository at this point in the history
  • Loading branch information
waketzheng committed Sep 21, 2024
1 parent 63ec81d commit 9a8c3f9
Show file tree
Hide file tree
Showing 6 changed files with 280 additions and 241 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ archive.zip

# Backups
*.bak
.dmypy.json
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ app = FastAPI()
fastapi_cdn_host.patch_docs(app)
```
See more at:
- examples/
- tests/
- [examples/](https://github.com/waketzheng/fastapi-cdn-host/tree/main/examples)
- [tests/](https://github.com/waketzheng/fastapi-cdn-host/tree/main/tests)

## Detail
1. Let's say that the default docs CDN host https://cdn.jsdelivr.net is too slow in your network, while unpkg.com is much faster.
Expand All @@ -54,11 +54,14 @@ https://fastapi.tiangolo.com/how-to/custom-docs-ui-assets/?h=static#self-hosting
```py
from fastapi_cdn_host import AssetUrl

fastapi_cdn_host.patch_docs(app, AssetUrl(
js='http://my-cdn.com/swagger-ui.js',
css='http://my-cdn.com/swagger-ui.css',
redoc='http://my-cdn.com/redoc.standalone.js',
favicon='http://my-cdn.com/favicon.ico')
fastapi_cdn_host.patch_docs(
app,
cdn_host=AssetUrl(
js='http://my-cdn.com/swagger-ui.js',
css='http://my-cdn.com/swagger-ui.css',
redoc='http://my-cdn.com/redoc.standalone.js',
favicon='http://my-cdn.com/favicon.ico',
)
)
```

Expand Down
10 changes: 8 additions & 2 deletions fastapi_cdn_host/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from .client import AssetUrl, CdnHostEnum, CdnHostItem, monkey_patch_for_docs_ui
from .client import (
AssetUrl,
CdnHostEnum,
CdnHostItem,
monkey_patch_for_docs_ui,
patch_docs,
)
from .utils import today_lock, weekday_lock

patch_docs = monkey_patch = monkey_patch_for_docs_ui
monkey_patch = monkey_patch_for_docs_ui
__version__ = "0.8.0"
__all__ = (
"__version__",
Expand Down
13 changes: 10 additions & 3 deletions fastapi_cdn_host/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,19 +560,21 @@ def detect_local_file(
return self._maybe(static_root, app=app, favicon=favicon_url)


def monkey_patch_for_docs_ui(
def patch_docs(
app: FastAPI,
docs_cdn_host: Union[
cdn_host: Union[
CdnHostEnum, List[CdnHostInfoType], CdnHostInfoType, Path, AssetUrl, None
] = None,
favicon_url: Union[str, None] = None,
lock: Union[Callable[[Request], Any], None] = None,
cache: bool = True,
*,
docs_cdn_host=None, # For backward compatibility
) -> None:
"""Use local static files or the faster CDN host for docs asset(swagger-ui)
:param app: the FastAPI object
:param docs_cdn_host: static root path or CDN host info
:param cdn_host: static root path or CDN host info
:param favicon_url: docs page logo
:param lock: function that receive a request argument to verify it
:param cache: whether cache race result in disk
Expand All @@ -582,6 +584,8 @@ def monkey_patch_for_docs_ui(
if not openapi_url or (not docs_url and not redoc_url):
logger.info("API docs not activated, skip monkey patch.")
return
if docs_cdn_host is None and cdn_host is not None:
docs_cdn_host = cdn_host
if isinstance(docs_cdn_host, AssetUrl):
if favicon_url is not None and favicon_url != docs_cdn_host.favicon:
docs_cdn_host.favicon = favicon_url
Expand All @@ -595,3 +599,6 @@ def monkey_patch_for_docs_ui(
DocsBuilder(index).update_docs_entrypoint(urls, app, docs_url, lock=lock)
if redoc_url and (index := route_index.get(redoc_url)) is not None:
DocsBuilder(index).update_redoc_entrypoint(urls, app, redoc_url, lock=lock)


monkey_patch_for_docs_ui = patch_docs # For backward compatibility
Loading

0 comments on commit 9a8c3f9

Please sign in to comment.