|
| 1 | +"""TiPG openapi tools.""" |
| 2 | + |
| 3 | +from fastapi import FastAPI |
| 4 | + |
| 5 | +from starlette.requests import Request |
| 6 | +from starlette.responses import Response |
| 7 | +from starlette.routing import Route, request_response |
| 8 | + |
| 9 | + |
| 10 | +def _update_openapi(app: FastAPI) -> FastAPI: |
| 11 | + """Update OpenAPI response content-type. |
| 12 | +
|
| 13 | + This function modifies the openapi route to comply with the STAC API spec's required |
| 14 | + content-type response header. |
| 15 | +
|
| 16 | + Copied from https://github.com/stac-utils/stac-fastapi/blob/main/stac_fastapi/api/stac_fastapi/api/openapi.py |
| 17 | +
|
| 18 | + MIT License |
| 19 | +
|
| 20 | + Copyright (c) 2020 Arturo AI |
| 21 | +
|
| 22 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 23 | + of this software and associated documentation files (the "Software"), to deal |
| 24 | + in the Software without restriction, including without limitation the rights |
| 25 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 26 | + copies of the Software, and to permit persons to whom the Software is |
| 27 | + furnished to do so, subject to the following conditions: |
| 28 | +
|
| 29 | + The above copyright notice and this permission notice shall be included in all |
| 30 | + copies or substantial portions of the Software. |
| 31 | +
|
| 32 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 33 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 34 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 35 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 36 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 37 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 38 | + SOFTWARE. |
| 39 | + """ |
| 40 | + # Find the route for the openapi_url in the app |
| 41 | + openapi_route: Route = next( |
| 42 | + route for route in app.router.routes if route.path == app.openapi_url |
| 43 | + ) |
| 44 | + # Store the old endpoint function so we can call it from the patched function |
| 45 | + old_endpoint = openapi_route.endpoint |
| 46 | + |
| 47 | + # Create a patched endpoint function that modifies the content type of the response |
| 48 | + async def patched_openapi_endpoint(req: Request) -> Response: |
| 49 | + # Get the response from the old endpoint function |
| 50 | + response = await old_endpoint(req) |
| 51 | + # Update the content type header in place |
| 52 | + response.headers["content-type"] = ( |
| 53 | + "application/vnd.oai.openapi+json;version=3.0" |
| 54 | + ) |
| 55 | + # Return the updated response |
| 56 | + return response |
| 57 | + |
| 58 | + # When a Route is accessed the `handle` function calls `self.app`. Which is |
| 59 | + # the endpoint function wrapped with `request_response`. So we need to wrap |
| 60 | + # our patched function and replace the existing app with it. |
| 61 | + openapi_route.app = request_response(patched_openapi_endpoint) |
| 62 | + |
| 63 | + # return the patched app |
| 64 | + return app |
0 commit comments