-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
switch back to middleware as function and remove commented code.
- Loading branch information
1 parent
e2b17b4
commit 6d815b2
Showing
2 changed files
with
19 additions
and
47 deletions.
There are no files selected for viewing
This file contains 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 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,50 +1,23 @@ | ||
# from fastapi import Request | ||
# from fastapi.responses import JSONResponse | ||
# | ||
# | ||
# def create_validate_header_middleware(required_header: dict[str, str] | None): | ||
# async def validate_header(request: Request, call_next): | ||
# if required_header: | ||
# header_name, expected_value = next(iter(required_header.items())) | ||
# if ( | ||
# header_name not in request.headers | ||
# or request.headers[header_name] != expected_value | ||
# ): | ||
# return JSONResponse( # attempted to use Exception and although it was caught it did not propagate | ||
# status_code=400, | ||
# content={ | ||
# "error": "Header Validation Error", | ||
# "message": f"Missing or invalid header: {header_name}", | ||
# "code": "HEADER_VALIDATION_ERROR", | ||
# }, | ||
# ) | ||
# return await call_next(request) | ||
# | ||
# return validate_header | ||
|
||
|
||
from fastapi import Request | ||
from fastapi.responses import JSONResponse | ||
from starlette.middleware.base import BaseHTTPMiddleware | ||
from starlette.types import ASGIApp | ||
|
||
|
||
class ValidateHeaderMiddleware(BaseHTTPMiddleware): | ||
def __init__(self, app: ASGIApp, required_header: dict[str, str] | None): | ||
super().__init__(app) | ||
self.required_header = required_header | ||
|
||
async def dispatch(self, request: Request, call_next): | ||
if self.required_header: | ||
header_name, expected_value = next(iter(self.required_header.items())) | ||
if header_name not in request.headers or request.headers[header_name] != expected_value: | ||
return JSONResponse( | ||
def create_validate_header_middleware(required_header: dict[str, str] | None): | ||
async def validate_header(request: Request, call_next): | ||
if required_header: | ||
header_name, expected_value = next(iter(required_header.items())) | ||
if ( | ||
header_name not in request.headers | ||
or request.headers[header_name] != expected_value | ||
): | ||
return JSONResponse( # attempted to use Exception and although it was caught it did not propagate | ||
status_code=400, | ||
content={ | ||
"error": "Header Validation Error", | ||
"message": f"Missing or invalid header: {header_name}", | ||
"code": "HEADER_VALIDATION_ERROR" | ||
} | ||
"code": "HEADER_VALIDATION_ERROR", | ||
}, | ||
) | ||
response = await call_next(request) | ||
return response | ||
return await call_next(request) | ||
|
||
return validate_header |