fastapi-swagger 0.1.6__tar.gz
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.
- fastapi_swagger-0.1.6/LICENSE +21 -0
- fastapi_swagger-0.1.6/PKG-INFO +100 -0
- fastapi_swagger-0.1.6/README.md +78 -0
- fastapi_swagger-0.1.6/fastapi_swagger/__init__.py +3 -0
- fastapi_swagger-0.1.6/fastapi_swagger/main.py +88 -0
- fastapi_swagger-0.1.6/fastapi_swagger/resources/__init__.py +0 -0
- fastapi_swagger-0.1.6/fastapi_swagger/resources/favicon-32x32.png +0 -0
- fastapi_swagger-0.1.6/fastapi_swagger/resources/swagger-ui-bundle.js +2 -0
- fastapi_swagger-0.1.6/fastapi_swagger/resources/swagger-ui.css +3 -0
- fastapi_swagger-0.1.6/pyproject.toml +34 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Ruslan Bel'kov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: fastapi-swagger
|
|
3
|
+
Version: 0.1.6
|
|
4
|
+
Summary:
|
|
5
|
+
Home-page: https://github.com/dantetemplar/fastapi-swagger
|
|
6
|
+
License: MIT
|
|
7
|
+
Author: Ruslan Bel'kov
|
|
8
|
+
Author-email: ruslan.belckov@yandex.ru
|
|
9
|
+
Requires-Python: >=3.10,<4.0
|
|
10
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Requires-Dist: fastapi (==0.100)
|
|
19
|
+
Project-URL: Repository, https://github.com/dantetemplar/fastapi-swagger
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# FastAPI Swagger Plugin
|
|
23
|
+
|
|
24
|
+
This plugin updates the FastAPI app to include a latest Swagger UI distribution. Also, it works locally and does not
|
|
25
|
+
depend on the @tiangolo domains and cdns.
|
|
26
|
+
|
|
27
|
+
## Why?
|
|
28
|
+
|
|
29
|
+
The FastAPI already includes a Swagger UI. However, author updates swagger not so often. Moreover he uses his own
|
|
30
|
+
hosts for the resources, and they are not always available (especially in Russia). This plugin allows to use the latest
|
|
31
|
+
Swagger UI distribution and host it inside your app.
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
### Installation
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install fastapi_swagger
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Basic Usage
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from fastapi import FastAPI
|
|
45
|
+
from fastapi_swagger import patch_fastapi
|
|
46
|
+
|
|
47
|
+
app = FastAPI(docs_url=None, swagger_ui_oauth2_redirect_url=None) # docs url will be at /docs
|
|
48
|
+
|
|
49
|
+
patch_fastapi(app)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## How it works?
|
|
53
|
+
|
|
54
|
+
How it was before:
|
|
55
|
+
FastAPI uses the `get_swagger_ui_html` function to render the Swagger UI under the hood.
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
def get_swagger_ui_html(
|
|
59
|
+
*,
|
|
60
|
+
openapi_url: str,
|
|
61
|
+
title: str,
|
|
62
|
+
swagger_js_url: str = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui-bundle.js",
|
|
63
|
+
swagger_css_url: str = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui.css",
|
|
64
|
+
swagger_favicon_url: str = "https://fastapi.tiangolo.com/img/favicon.png",
|
|
65
|
+
oauth2_redirect_url: Optional[str] = None,
|
|
66
|
+
init_oauth: Optional[Dict[str, Any]] = None,
|
|
67
|
+
swagger_ui_parameters: Optional[Dict[str, Any]] = None,
|
|
68
|
+
) -> HTMLResponse:
|
|
69
|
+
...
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
How it is now:
|
|
73
|
+
|
|
74
|
+
Actually, we just copy the Swagger UI distribution from
|
|
75
|
+
the [GitHub releases](https://github.com/swagger-api/swagger-ui/releases) to the `fastapi_swagger` package, and serve it
|
|
76
|
+
from your app.
|
|
77
|
+
Patch creates several additional routes with the Swagger UI resources and one route for docs page (btw, using same
|
|
78
|
+
`get_swagger_ui_html` function).
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
def patch_fastapi(
|
|
82
|
+
app: FastAPI,
|
|
83
|
+
docs_url: str = "/docs",
|
|
84
|
+
*,
|
|
85
|
+
title: Optional[str],
|
|
86
|
+
swagger_js_url: str = "/swagger/swagger-ui-bundle.js", # relative path from app root
|
|
87
|
+
swagger_css_url: str = "/swagger/swagger-ui.css", # relative path from app root
|
|
88
|
+
swagger_favicon_url: str = "/swagger/favicon-32x32.png", # relative path from app root
|
|
89
|
+
oauth2_redirect_url: Optional[str] = None,
|
|
90
|
+
init_oauth: Optional[Dict[str, Any]] = None,
|
|
91
|
+
swagger_ui_parameters: Optional[Dict[str, Any]] = None,
|
|
92
|
+
):
|
|
93
|
+
...
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
patch_fastapi(app)
|
|
97
|
+
# Now there are additional routes /swagger/swagger-ui-bundle.js, /swagger/swagger-ui.css, /swagger/favicon-32x32.png and /docs
|
|
98
|
+
# They all are not dependent on the external resources.
|
|
99
|
+
```
|
|
100
|
+
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# FastAPI Swagger Plugin
|
|
2
|
+
|
|
3
|
+
This plugin updates the FastAPI app to include a latest Swagger UI distribution. Also, it works locally and does not
|
|
4
|
+
depend on the @tiangolo domains and cdns.
|
|
5
|
+
|
|
6
|
+
## Why?
|
|
7
|
+
|
|
8
|
+
The FastAPI already includes a Swagger UI. However, author updates swagger not so often. Moreover he uses his own
|
|
9
|
+
hosts for the resources, and they are not always available (especially in Russia). This plugin allows to use the latest
|
|
10
|
+
Swagger UI distribution and host it inside your app.
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
### Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pip install fastapi_swagger
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### Basic Usage
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
from fastapi import FastAPI
|
|
24
|
+
from fastapi_swagger import patch_fastapi
|
|
25
|
+
|
|
26
|
+
app = FastAPI(docs_url=None, swagger_ui_oauth2_redirect_url=None) # docs url will be at /docs
|
|
27
|
+
|
|
28
|
+
patch_fastapi(app)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## How it works?
|
|
32
|
+
|
|
33
|
+
How it was before:
|
|
34
|
+
FastAPI uses the `get_swagger_ui_html` function to render the Swagger UI under the hood.
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
def get_swagger_ui_html(
|
|
38
|
+
*,
|
|
39
|
+
openapi_url: str,
|
|
40
|
+
title: str,
|
|
41
|
+
swagger_js_url: str = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui-bundle.js",
|
|
42
|
+
swagger_css_url: str = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui.css",
|
|
43
|
+
swagger_favicon_url: str = "https://fastapi.tiangolo.com/img/favicon.png",
|
|
44
|
+
oauth2_redirect_url: Optional[str] = None,
|
|
45
|
+
init_oauth: Optional[Dict[str, Any]] = None,
|
|
46
|
+
swagger_ui_parameters: Optional[Dict[str, Any]] = None,
|
|
47
|
+
) -> HTMLResponse:
|
|
48
|
+
...
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
How it is now:
|
|
52
|
+
|
|
53
|
+
Actually, we just copy the Swagger UI distribution from
|
|
54
|
+
the [GitHub releases](https://github.com/swagger-api/swagger-ui/releases) to the `fastapi_swagger` package, and serve it
|
|
55
|
+
from your app.
|
|
56
|
+
Patch creates several additional routes with the Swagger UI resources and one route for docs page (btw, using same
|
|
57
|
+
`get_swagger_ui_html` function).
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
def patch_fastapi(
|
|
61
|
+
app: FastAPI,
|
|
62
|
+
docs_url: str = "/docs",
|
|
63
|
+
*,
|
|
64
|
+
title: Optional[str],
|
|
65
|
+
swagger_js_url: str = "/swagger/swagger-ui-bundle.js", # relative path from app root
|
|
66
|
+
swagger_css_url: str = "/swagger/swagger-ui.css", # relative path from app root
|
|
67
|
+
swagger_favicon_url: str = "/swagger/favicon-32x32.png", # relative path from app root
|
|
68
|
+
oauth2_redirect_url: Optional[str] = None,
|
|
69
|
+
init_oauth: Optional[Dict[str, Any]] = None,
|
|
70
|
+
swagger_ui_parameters: Optional[Dict[str, Any]] = None,
|
|
71
|
+
):
|
|
72
|
+
...
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
patch_fastapi(app)
|
|
76
|
+
# Now there are additional routes /swagger/swagger-ui-bundle.js, /swagger/swagger-ui.css, /swagger/favicon-32x32.png and /docs
|
|
77
|
+
# They all are not dependent on the external resources.
|
|
78
|
+
```
|
|
@@ -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
|
|
Binary file
|