-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FR: custom cdn options #423
Conversation
- added tests
Codecov ReportAttention:
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## main #423 +/- ##
==========================================
- Coverage 97.70% 97.69% -0.01%
==========================================
Files 67 67
Lines 6277 6300 +23
==========================================
+ Hits 6133 6155 +22
- Misses 144 145 +1 ☔ View full report in Codecov by Sentry. |
- Do not assume that URLs are for CDNs - Remove the connection between OpenAPIHandler and class with files URLs
Hi @joshua-auchincloss With the current code API I implemented, all it takes to achieve what you want is to define a subclass of the provided classes and including the desired HTML, like in this example: from dataclasses import dataclass
from pathlib import Path
from blacksheep import Application
from blacksheep.server.openapi.v3 import OpenAPIHandler
from blacksheep.server.openapi.ui import SwaggerUIProvider, UIOptions
from openapidocs.v3 import Info
app = Application()
class CustomUIProvider(SwaggerUIProvider):
def get_openapi_ui_html(self, options: UIOptions) -> str:
_template = Path("example.html").read_text()
return _template.replace("{options.spec_url}", options.spec_url)
docs = OpenAPIHandler(info=Info(title="Example API", version="0.0.1"))
# Set the UI provider as desired:
docs.ui_providers = [CustomUIProvider()]
docs.bind_app(app)
@dataclass
class Foo:
foo: str
@app.route("/foo")
async def get_foo() -> Foo:
return Foo("Hello!") example.html: <!DOCTYPE html>
<html>
<head>
<title>My desired title</title>
<link rel="icon" href="/favicon.png"/>
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/5.10.0/swagger-ui.css">
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/5.10.0/swagger-ui-bundle.min.js"></script>
<script>
const ui = SwaggerUIBundle({
url: '{options.spec_url}',
oauth2RedirectUrl: window.location.origin + '/docs/oauth2-redirect',
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIBundle.SwaggerUIStandalonePreset
],
layout: "BaseLayout",
deepLinking: true,
showExtensions: true,
showCommonExtensions: true
})
</script>
</body>
</html> It is just 6 lines of Python, and a simple HTML file: +from blacksheep.server.openapi.ui import SwaggerUIProvider, UIOptions
from openapidocs.v3 import Info
app = Application()
+class CustomUIProvider(SwaggerUIProvider):
+ def get_openapi_ui_html(self, options: UIOptions) -> str:
+ _template = Path("example.html").read_text()
+ return _template.replace("{options.spec_url}", options.spec_url)
docs = OpenAPIHandler(info=Info(title="Example API", version="0.0.1"))
# Set the UI provider as desired:
+docs.ui_providers = [CustomUIProvider()]
docs.bind_app(app) With the additional benefit that you can control everything of the HTML (favicon, metatags, etc.), not only the URL sources for the static files. |
Hey thanks for the reply - I appreciate the review, although our opinions differ on this I guess. From an end user perspective, why would I make a custom html handler and custom HTML source files when it's already bundled with the server in package resources? I don't think it's (unreasonable) to have support for these use cases without an extra ~2 files in my source code |
@joshua-auchincloss fair enough. I agree it's generally good to offer one more option to users. I recreated a PR from your branch. docs = OpenAPIHandler(info=Info(title="Cats API", version="0.0.1"))
docs.ui_providers[0].ui_files = UIFilesOptions(
js_url=get_test_files_url("swag-js"),
css_url=get_test_files_url("swag-css"),
)
docs.ui_providers.append(
ReDocUIProvider(
ui_files=UIFilesOptions(
js_url=get_test_files_url("redoc-js"),
fonts_url=get_test_files_url("redoc-fonts"),
)
)
) |
* Add support for custom Swagger UI files URL sources * refactor: expose cdn opts - added tests * Fix test assertion * Apply corrections before merge - Do not assume that URLs are for CDNs - Remove the connection between OpenAPIHandler and class with files URLs * Restore contribution for PR #423 * Update CHANGELOG.md * Update CHANGELOG.md * Update __init__.py --------- Co-authored-by: benefactarch <[email protected]>
Hello,
Hoping to add the following features, rationale to enable support for e.g. private resource CDNs:
OpenAPIHandler
initialization