-
Notifications
You must be signed in to change notification settings - Fork 1
add middleware to convert all requests with yaml content type to json #29
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
Closed
Closed
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8faa966
add middleware to convert all requests with yaml content type to json
Adamkadaban 0c76937
add test to check that yaml middleware works for template route
Adamkadaban 35056aa
clean code up and add missing typing
Adamkadaban 9c520a5
Add mypy developer requirement for python YAML type stubs
alexchristy 4b44f02
remove erroneous yaml get param in tests
Adamkadaban 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,37 @@ | ||
| import json | ||
|
|
||
| import yaml | ||
| from fastapi import HTTPException, Request, Response, status | ||
| from starlette.datastructures import MutableHeaders | ||
| from starlette.middleware.base import RequestResponseEndpoint | ||
|
|
||
| from .api import router | ||
| from .core.config import settings | ||
| from .core.setup import create_application | ||
|
|
||
| app = create_application(router=router, settings=settings) | ||
|
|
||
|
|
||
| @app.middleware("http") | ||
| async def convert_yaml_to_json( | ||
| request: Request, call_next: RequestResponseEndpoint | ||
| ) -> Response: | ||
| """FastAPI middleware that will change all requests with a yaml content type to json.""" | ||
| body = await request.body() | ||
| if request.headers.get("content-type") == "application/yaml": | ||
| try: | ||
| json_body = json.dumps(yaml.safe_load(body.decode("utf-8"))).encode() | ||
| except Exception as e: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, | ||
| detail="Unable to parse provided YAML configuration.", | ||
| ) from e | ||
|
|
||
| request._body = json_body | ||
| updated_headers = MutableHeaders(request._headers) | ||
| updated_headers["content-length"] = str(len(json_body)) | ||
| updated_headers["content-type"] = "application/json" | ||
| request._headers = updated_headers | ||
| request.scope.update(headers=request.headers.raw) | ||
|
|
||
| return await call_next(request) |
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
Oops, something went wrong.
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.