fastapi-standalone-docs 0.0.1__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.
@@ -0,0 +1,2 @@
1
+ # flake8: noqa
2
+ from fastapi_standalone_docs.standalone_docs import StandaloneDocs
@@ -0,0 +1,59 @@
1
+ from pathlib import Path
2
+ from typing import List
3
+ from urllib.request import urlretrieve
4
+
5
+ import fastapi_standalone_docs
6
+
7
+ FASTAPI_FILES = [
8
+ "https://fastapi.tiangolo.com/img/favicon.png",
9
+ "https://raw.githubusercontent.com/tiangolo/fastapi/master/LICENSE",
10
+ ]
11
+
12
+ SWAGGER_FILES = [
13
+ "https://cdn.jsdelivr.net/npm/swagger-ui-dist@4/LICENSE",
14
+ "https://cdn.jsdelivr.net/npm/swagger-ui-dist@4/swagger-ui.css",
15
+ "https://cdn.jsdelivr.net/npm/swagger-ui-dist@4/swagger-ui-bundle.js",
16
+ ]
17
+
18
+ REDOC_FILES = [
19
+ "https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js.LICENSE.txt",
20
+ "https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js",
21
+ "https://cdn.redoc.ly/redoc/logo-mini.svg",
22
+ ]
23
+
24
+
25
+ def update_docs():
26
+ static_path = Path(fastapi_standalone_docs.__path__[0]) / "static"
27
+ fastapi_path = static_path / "fastapi"
28
+ swagger_path = static_path / "swagger"
29
+ redoc_path = static_path / "redoc"
30
+
31
+ fastapi_path.mkdir(parents=True, exist_ok=True)
32
+ swagger_path.mkdir(parents=True, exist_ok=True)
33
+ redoc_path.mkdir(parents=True, exist_ok=True)
34
+
35
+ download_files(fastapi_path, FASTAPI_FILES)
36
+ download_files(swagger_path, SWAGGER_FILES)
37
+ download_files(redoc_path, REDOC_FILES)
38
+
39
+ patch_redoc_logo(target=static_path / "redoc")
40
+
41
+
42
+ def download_files(destination: Path, urls: List[str]) -> None:
43
+ for url in urls:
44
+ filename = url.split("/")[-1]
45
+ urlretrieve(url, destination / filename)
46
+
47
+
48
+ def patch_redoc_logo(target: Path):
49
+ file = target / "redoc.standalone.js"
50
+ content = file.read_text(encoding="utf-8")
51
+ content = content.replace(
52
+ '"https://cdn.redoc.ly/redoc/logo-mini.svg"',
53
+ 'window.location.pathname+"/logo-mini.svg"',
54
+ )
55
+ file.write_text(content, encoding="utf-8")
56
+
57
+
58
+ if __name__ == "__main__":
59
+ update_docs()
@@ -0,0 +1,74 @@
1
+ from pathlib import Path
2
+ from typing import Optional
3
+
4
+ from fastapi import FastAPI, applications
5
+ from fastapi.openapi.docs import get_redoc_html, get_swagger_ui_html
6
+ from fastapi.staticfiles import StaticFiles
7
+
8
+ import fastapi_standalone_docs
9
+
10
+
11
+ class StandaloneDocs:
12
+ def __init__(
13
+ self,
14
+ app: FastAPI,
15
+ with_google_fonts: bool = False,
16
+ swagger_favicon_url: Optional[str] = None,
17
+ redoc_favicon_url: Optional[str] = None,
18
+ ):
19
+ self.app = app
20
+ self.with_google_fonts = with_google_fonts
21
+ self.static_path = Path(fastapi_standalone_docs.__path__[0]) / "static"
22
+
23
+ self.patch_swagger(swagger_favicon_url)
24
+ self.patch_redoc(redoc_favicon_url)
25
+
26
+ def patch_swagger(self, swagger_favicon_url: Optional[str]):
27
+ if self.app.docs_url:
28
+ if not swagger_favicon_url:
29
+ swagger_favicon_url = self.app.docs_url + "/fastapi/favicon.png"
30
+ self.app.mount(
31
+ self.app.docs_url + "/fastapi/",
32
+ StaticFiles(directory=self.static_path / "fastapi"),
33
+ )
34
+
35
+ self.app.mount(
36
+ self.app.docs_url + "/",
37
+ StaticFiles(directory=self.static_path / "swagger"),
38
+ )
39
+
40
+ def patched_get_swagger_ui_html(*args, **kwargs):
41
+ return get_swagger_ui_html(
42
+ *args,
43
+ **kwargs,
44
+ swagger_favicon_url=swagger_favicon_url,
45
+ swagger_css_url=self.app.docs_url + "/swagger-ui.css",
46
+ swagger_js_url=self.app.docs_url + "/swagger-ui-bundle.js",
47
+ )
48
+
49
+ applications.get_swagger_ui_html = patched_get_swagger_ui_html
50
+
51
+ def patch_redoc(self, redoc_favicon_url: Optional[str]):
52
+ if self.app.redoc_url:
53
+ if not redoc_favicon_url:
54
+ redoc_favicon_url = self.app.redoc_url + "/fastapi/favicon.png"
55
+ self.app.mount(
56
+ self.app.redoc_url + "/fastapi/",
57
+ StaticFiles(directory=self.static_path / "fastapi"),
58
+ )
59
+
60
+ self.app.mount(
61
+ self.app.redoc_url + "/",
62
+ StaticFiles(directory=self.static_path / "redoc"),
63
+ )
64
+
65
+ def patched_get_redoc_html(*args, **kwargs):
66
+ return get_redoc_html(
67
+ *args,
68
+ **kwargs,
69
+ redoc_js_url=self.app.redoc_url + "/redoc.standalone.js",
70
+ redoc_favicon_url=redoc_favicon_url,
71
+ with_google_fonts=self.with_google_fonts,
72
+ )
73
+
74
+ applications.get_redoc_html = patched_get_redoc_html
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Sebastián Ramírez
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1 @@
1
+ <svg width="300" height="300" xmlns="http://www.w3.org/2000/svg"><path fill="#0044d4" d="M249.488 231.49a123.68 123.68 0 0 0-22.86-12.66 111.55 111.55 0 0 0-42.12-214.75h-171.8a8.18 8.18 0 0 0-5.78 14 7.48 7.48 0 0 0 5.78 2.48 95 95 0 1 1 0 190c-.83 0-1.38.27-2.21.27a8 8 0 0 0-6.33 7.71v1.11a7.69 7.69 0 0 0 7.71 7.7h5.52a92.93 92.93 0 0 1 50.66 17.62 95.33 95.33 0 0 1 34.14 45.43 8.27 8.27 0 0 0 7.7 5.52h171.8a7.76 7.76 0 0 0 6.61-3.58 8 8 0 0 0 1.09-7.42 109.06 109.06 0 0 0-39.91-53.43zm-158-37.16a111.62 111.62 0 0 0 32.76-78.75 110 110 0 0 0-32.76-78.74 105.91 105.91 0 0 0-20.37-16.24h113.39a95 95 0 1 1 0 190 3.55 3.55 0 0 0-1.65.27H70.798a109.06 109.06 0 0 0 20.65-16.54h.04zm-13.77 37.16c-1.92-1.37-4.13-2.75-6.33-4.13h117.8a94.79 94.79 0 0 1 80.12 52h-153.63a112 112 0 0 0-38-47.87h.04z" class="cls-1"/><path fill="#0044d4" d="M158.398 115.58a8.11 8.11 0 0 0 8.26 8.26h82.6a8.26 8.26 0 0 0 0-16.52h-82.6a8.29 8.29 0 0 0-8.26 8.26zM152.298 156.92h82.59a8.26 8.26 0 0 0 0-16.52h-82.59a8.11 8.11 0 0 0-8.26 8.26 8.28 8.28 0 0 0 8.26 8.26zM152.298 90.8h82.59a8.26 8.26 0 0 0 0-16.52h-82.59a8.11 8.11 0 0 0-8.26 8.26 8.46 8.46 0 0 0 8.26 8.26z" class="cls-1"/></svg>