Skip to content

Commit

Permalink
refact: use bento base pydantic config
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlougheed committed Dec 22, 2023
1 parent 19990be commit d3bfe30
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 51 deletions.
40 changes: 3 additions & 37 deletions bento_drop_box_service/config.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import json

from bento_lib.logging import LogLevelLiteral
from bento_lib.config.pydantic import BentoBaseConfig
from fastapi import Depends
from functools import lru_cache
from pydantic.fields import FieldInfo
from pydantic_settings import BaseSettings, EnvSettingsSource, PydanticBaseSettingsSource, SettingsConfigDict
from typing import Annotated, Any, Literal
from typing import Annotated, Literal

from .constants import SERVICE_TYPE

Expand All @@ -16,48 +12,18 @@
]


class CorsOriginsParsingSource(EnvSettingsSource):
def prepare_field_value(self, field_name: str, field: FieldInfo, value: Any, value_is_complex: bool) -> Any:
if field_name == "cors_origins":
return tuple(x.strip() for x in value.split(";")) if value is not None else ()
return json.loads(value) if value_is_complex else value


class Config(BaseSettings):
bento_debug: bool = False
bento_container_local: bool = False

class Config(BentoBaseConfig):
service_id: str = str(":".join(list(SERVICE_TYPE.values())[:2]))
service_name: str = "Bento Drop Box Service"
service_description: str = "Drop box service for a Bento platform node."
service_contact_url: str = "mailto:[email protected]"
service_url: str = "http://127.0.0.1:5000" # base URL to construct object URIs from

service_data: str = "data/"
service_data_source: Literal["local"] = "local"
traversal_limit: int = 16

bento_authz_service_url: str # Bento authorization service base URL
authz_enabled: bool = True

cors_origins: tuple[str, ...]

log_level: LogLevelLiteral = "debug"

# Make Config instances hashable + immutable
model_config = SettingsConfigDict(frozen=True)

@classmethod
def settings_customise_sources(
cls,
settings_cls: type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]:
return (CorsOriginsParsingSource(settings_cls),)


@lru_cache()
def get_config() -> Config:
Expand Down
2 changes: 1 addition & 1 deletion bento_drop_box_service/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from bento_lib.service_info import GA4GHServiceType
from bento_lib.service_info.types import GA4GHServiceType
from bento_drop_box_service import __version__

__all__ = [
Expand Down
23 changes: 10 additions & 13 deletions bento_drop_box_service/routes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from bento_lib.auth.permissions import P_VIEW_DROP_BOX, P_INGEST_DROP_BOX, P_DELETE_DROP_BOX
from bento_lib.auth.resources import RESOURCE_EVERYTHING
from bento_lib.service_info.constants import SERVICE_ORGANIZATION_C3G
from bento_lib.service_info.helpers import build_service_info
from bento_lib.service_info.helpers import build_service_info_from_pydantic_config
from fastapi import APIRouter, Form, Request, status
from fastapi.exceptions import HTTPException
from fastapi.responses import JSONResponse
Expand Down Expand Up @@ -75,15 +74,13 @@ async def drop_box_delete(path: str, backend: BackendDependency):
@drop_box_router.get("/service-info", dependencies=[authz_middleware.dep_public_endpoint()])
async def service_info(config: ConfigDependency, logger: LoggerDependency) -> Response:
# Spec: https://github.com/ga4gh-discovery/ga4gh-service-info
return JSONResponse(await build_service_info({
"id": config.service_id,
"name": config.service_name,
"type": SERVICE_TYPE,
"description": config.service_description,
"organization": SERVICE_ORGANIZATION_C3G,
"contactUrl": config.service_contact_url,
"version": __version__,
"bento": {
"serviceKind": BENTO_SERVICE_KIND
return JSONResponse(await build_service_info_from_pydantic_config(
config,
logger,
{
"serviceKind": BENTO_SERVICE_KIND,
"gitRepository": "https://github.com/bento-platform/bento_drop_box_service",
},
}, debug=config.bento_debug, local=config.bento_container_local, logger=logger))
SERVICE_TYPE,
__version__,
))

0 comments on commit d3bfe30

Please sign in to comment.