fastapi-swagger 0.1.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of fastapi-swagger might be problematic. Click here for more details.

@@ -0,0 +1,3 @@
1
+ __all__ = ["patch_fastapi"]
2
+
3
+ from .main import patch_fastapi
@@ -0,0 +1,88 @@
1
+ from importlib import resources
2
+ from typing import Optional, Dict, Any
3
+
4
+ from fastapi import FastAPI, Response, Request
5
+ from fastapi.openapi.docs import get_swagger_ui_html
6
+ from starlette.responses import RedirectResponse
7
+
8
+
9
+ def patch_fastapi(
10
+ app: FastAPI,
11
+ docs_url: str = "/docs",
12
+ redirect_from_root_to_docs: bool = True,
13
+ *,
14
+ title: Optional[str],
15
+ swagger_js_url: str = "/swagger/swagger-ui-bundle.js", # relative path from app root
16
+ swagger_css_url: str = "/swagger/swagger-ui.css", # relative path from app root
17
+ swagger_favicon_url: str = "/swagger/favicon-32x32.png", # relative path from app root
18
+ oauth2_redirect_url: Optional[str] = None,
19
+ init_oauth: Optional[Dict[str, Any]] = None,
20
+ swagger_ui_parameters: Optional[Dict[str, Any]] = None,
21
+ ):
22
+ """
23
+ Patch FastAPI app to serve Swagger UI using fastapi_swagger plugin
24
+
25
+ :param app: FastAPI app
26
+ :param docs_url: URL to serve Swagger UI
27
+ :param redirect_from_root_to_docs: Redirect from root to Swagger UI (default: True)
28
+ :param title: Title of Swagger UI page (default: f"{app.title} - Swagger UI") - html title
29
+ :param swagger_js_url: Where to serve Swagger UI JS bundle
30
+ :param swagger_css_url: Where to serve Swagger UI CSS
31
+ :param swagger_favicon_url: Where to serve Swagger UI favicon image - html icon
32
+ :param swagger_ui_parameters: Parameters for Swagger UI
33
+ (default: {"tryItOutEnabled": True, "persistAuthorization": True, "filter": True})
34
+ :param init_oauth: OAuth2 configuration
35
+ :param oauth2_redirect_url: OAuth2 redirect URL
36
+ """
37
+ if redirect_from_root_to_docs:
38
+
39
+ @app.get("/", include_in_schema=False)
40
+ async def redirect_to_docs(request: Request):
41
+ return RedirectResponse(url=request.url_for("swagger_ui_html"))
42
+
43
+ @app.get(docs_url, tags=["Swagger"], include_in_schema=False)
44
+ async def swagger_ui_html(request: Request):
45
+ nonlocal swagger_ui_parameters, title, oauth2_redirect_url
46
+ root_path = request.scope.get("root_path", "").rstrip("/")
47
+ openapi_url = root_path + app.openapi_url
48
+ _swagger_js_url = request.url_for("swagger_ui_bundle_js")
49
+ _swagger_css_url = request.url_for("swagger_ui_css")
50
+ _swagger_favicon_url = request.url_for("swagger_favicon_png")
51
+ if swagger_ui_parameters is None:
52
+ swagger_ui_parameters = {
53
+ "tryItOutEnabled": True,
54
+ "persistAuthorization": True,
55
+ "filter": True,
56
+ }
57
+ return get_swagger_ui_html(
58
+ openapi_url=openapi_url,
59
+ title=title or f"{app.title} - Swagger UI",
60
+ swagger_js_url=str(_swagger_js_url),
61
+ swagger_css_url=str(_swagger_css_url),
62
+ swagger_favicon_url=str(_swagger_favicon_url),
63
+ oauth2_redirect_url=oauth2_redirect_url,
64
+ init_oauth=init_oauth,
65
+ swagger_ui_parameters=swagger_ui_parameters,
66
+ )
67
+
68
+ @app.get(swagger_js_url, tags=["Swagger"], include_in_schema=False)
69
+ async def swagger_ui_bundle_js() -> Response:
70
+ with resources.open_binary(
71
+ "fastapi_swagger.resources", "swagger-ui-bundle.js"
72
+ ) as f:
73
+ _ = f.read()
74
+ return Response(content=_, media_type="application/javascript")
75
+
76
+ @app.get(swagger_css_url, tags=["Swagger"], include_in_schema=False)
77
+ async def swagger_ui_css() -> Response:
78
+ with resources.open_binary("fastapi_swagger.resources", "swagger-ui.css") as f:
79
+ _ = f.read()
80
+ return Response(content=_, media_type="text/css")
81
+
82
+ @app.get(swagger_favicon_url, tags=["Swagger"], include_in_schema=False)
83
+ async def swagger_favicon_png() -> Response:
84
+ with resources.open_binary(
85
+ "fastapi_swagger.resources", "favicon-32x32.png"
86
+ ) as f:
87
+ _ = f.read()
88
+ return Response(content=_, media_type="image/png")
File without changes