-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add openapi module for pygeoapi override
- Loading branch information
1 parent
0257cff
commit fc38616
Showing
5 changed files
with
68 additions
and
53 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
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
"""pygeoapi package.""" |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"""Override vanilla openapi module.""" | ||
from typing import Dict | ||
from typing import List | ||
|
||
from app.config.app import configuration as cfg | ||
from app.config.logging import create_logger | ||
from openapi_pydantic.v3.v3_0_3 import OpenAPI | ||
from openapi_pydantic.v3.v3_0_3 import SecurityScheme | ||
from pydantic_core import ValidationError | ||
|
||
|
||
logger = create_logger("app.pygeoapi.openapi") | ||
|
||
|
||
def augment_security(doc: Dict, security_schemes: List[SecurityScheme]) -> OpenAPI: | ||
"""Augment openapi document with security sections.""" | ||
try: | ||
openapi = OpenAPI.model_validate_json(doc) | ||
except ValidationError as e: | ||
logger.error(e) | ||
raise | ||
security_scheme_types = [ | ||
security_scheme.type for security_scheme in security_schemes | ||
] | ||
_security_schemes = {"securitySchemes": {}} # type: dict[str, dict] | ||
if all( | ||
item in ["http", "apiKey", "oauth2", "openIdConnect"] | ||
for item in security_scheme_types | ||
): | ||
dumped_schemes = {} | ||
for scheme in security_schemes: | ||
dumped_schemes.update( | ||
{ | ||
f"pygeoapi {cfg.PYGEOAPI_SECURITY_SCHEME}": scheme.model_dump( # noqa B950 | ||
by_alias=True, exclude_none=True | ||
) | ||
} | ||
) | ||
_security_schemes["securitySchemes"] = dumped_schemes | ||
content = openapi.model_dump(by_alias=True, exclude_none=True) | ||
components = content.get("components") | ||
if components: | ||
components.update(_security_schemes) | ||
content["components"] = components | ||
paths = openapi.paths | ||
secured_paths = {} | ||
if paths: | ||
for key, value in paths.items(): | ||
if value.get: | ||
value.get.security = [{f"pygeoapi {cfg.PYGEOAPI_SECURITY_SCHEME}": []}] | ||
if value.post: | ||
value.post.security = [{f"pygeoapi {cfg.PYGEOAPI_SECURITY_SCHEME}": []}] | ||
secured_paths.update({key: value}) | ||
if secured_paths: | ||
content["paths"] = secured_paths | ||
return OpenAPI(**content) |
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