-
Notifications
You must be signed in to change notification settings - Fork 116
Rework update_openapi function. #523
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
Merged
gadomski
merged 6 commits into
stac-utils:master
from
eseglem:feature/rework-update-openapi
Feb 7, 2023
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
adf0a8e
Rework update_openapi function.
eseglem f78584d
Update changelog.
eseglem 3eaf010
Merge branch 'master' into feature/rework-update-openapi
gadomski a318353
Additional documentation and add removed code back as deprecated.
eseglem 05671af
chore: fixup changelog
gadomski ffbb1bf
refactor: move VndOaiResponse back to old location
gadomski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,68 +1,36 @@ | ||
| """openapi.""" | ||
| from fastapi import FastAPI | ||
| from fastapi.openapi.utils import get_openapi | ||
| from starlette.requests import Request | ||
| from starlette.responses import JSONResponse | ||
|
|
||
| from stac_fastapi.api.config import ApiExtensions | ||
| from stac_fastapi.types.config import ApiSettings | ||
|
|
||
|
|
||
| class VndOaiResponse(JSONResponse): | ||
| """JSON with custom, vendor content-type.""" | ||
|
|
||
| media_type = "application/vnd.oai.openapi+json;version=3.0" | ||
| from starlette.responses import JSONResponse, Response | ||
| from starlette.routing import Route, request_response | ||
|
|
||
|
|
||
| def update_openapi(app: FastAPI) -> FastAPI: | ||
| """Update OpenAPI response content-type. | ||
| This function modifies the openapi route to comply with the STAC API spec's | ||
| required content-type response header | ||
| required content-type response header. | ||
| """ | ||
| urls = (server_data.get("url") for server_data in app.servers) | ||
| server_urls = {url for url in urls if url} | ||
|
|
||
| async def openapi(req: Request) -> JSONResponse: | ||
| root_path = req.scope.get("root_path", "").rstrip("/") | ||
| if root_path not in server_urls: | ||
| if root_path and app.root_path_in_servers: | ||
| app.servers.insert(0, {"url": root_path}) | ||
| server_urls.add(root_path) | ||
| return VndOaiResponse(app.openapi()) | ||
|
|
||
| # Remove the default openapi route | ||
| app.router.routes = list( | ||
| filter(lambda r: r.path != app.openapi_url, app.router.routes) | ||
| # Find the route for the openapi_url in the app. | ||
| openapi_route: Route = next( | ||
| route for route in app.router.routes if route.path == app.openapi_url | ||
| ) | ||
| # Add the updated openapi route | ||
| app.add_route(app.openapi_url, openapi, include_in_schema=False) | ||
| return app | ||
|
|
||
|
|
||
| # TODO: Remove or fix, this is currently unused | ||
| # and calls a missing method on ApiSettings | ||
| def config_openapi(app: FastAPI, settings: ApiSettings): | ||
gadomski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Config openapi.""" | ||
|
|
||
| def custom_openapi(): | ||
| """Config openapi.""" | ||
| if app.openapi_schema: | ||
| return app.openapi_schema | ||
|
|
||
| openapi_schema = get_openapi( | ||
| title="Arturo STAC API", version="0.1", routes=app.routes | ||
| ) | ||
|
|
||
| if settings.api_extension_is_enabled(ApiExtensions.fields): | ||
| openapi_schema["paths"]["/search"]["get"]["responses"]["200"]["content"][ | ||
| "application/json" | ||
| ]["schema"] = {"$ref": "#/components/schemas/ItemCollection"} | ||
| openapi_schema["paths"]["/search"]["post"]["responses"]["200"]["content"][ | ||
| "application/json" | ||
| ]["schema"] = {"$ref": "#/components/schemas/ItemCollection"} | ||
|
|
||
| app.openapi_schema = openapi_schema | ||
| return app.openapi_schema | ||
|
|
||
| app.openapi = custom_openapi | ||
| # Create a patched endpoint that modifies the content type of the response | ||
| async def patched_openapi_endpoint(req: Request) -> Response: | ||
| # Get the response from the original endpoint | ||
| response: JSONResponse = await openapi_route.endpoint(req) | ||
| # Update the content type header in place | ||
| response.headers[ | ||
| "content-type" | ||
| ] = "application/vnd.oai.openapi+json;version=3.0" | ||
| # Return the updated response | ||
| return response | ||
|
|
||
| # When a route is accessed the `handle` function will call `self.app`. So we can | ||
| # leave the `endpoint` the original function for use in the patched function and | ||
| # just update the `app` to use the patched function. | ||
gadomski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| openapi_route.app = request_response(patched_openapi_endpoint) | ||
|
|
||
| # return the patched app | ||
| return app | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.