Skip to content

Commit

Permalink
use pydantic settings (#657)
Browse files Browse the repository at this point in the history
* use pydantic settings

* rename stac_fastapi_id to stac_fastapi_landing_id
  • Loading branch information
vincentsarago authored Apr 11, 2024
1 parent e579311 commit 82816fa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
18 changes: 14 additions & 4 deletions stac_fastapi/api/stac_fastapi/api/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Fastapi app creation."""
import os

from typing import Any, Dict, List, Optional, Tuple, Type, Union

import attr
Expand Down Expand Up @@ -84,11 +84,21 @@ class StacApi:
converter=update_openapi,
)
router: APIRouter = attr.ib(default=attr.Factory(APIRouter))
title: str = attr.ib(default=os.getenv("STAC_FASTAPI_TITLE", "stac-fastapi"))
api_version: str = attr.ib(default=os.getenv("STAC_FASTAPI_VERSION", "0.1"))
title: str = attr.ib(
default=attr.Factory(
lambda self: self.settings.stac_fastapi_title, takes_self=True
)
)
api_version: str = attr.ib(
default=attr.Factory(
lambda self: self.settings.stac_fastapi_version, takes_self=True
)
)
stac_version: str = attr.ib(default=STAC_VERSION)
description: str = attr.ib(
default=os.getenv("STAC_FASTAPI_DESCRIPTION", "stac-fastapi")
default=attr.Factory(
lambda self: self.settings.stac_fastapi_description, takes_self=True
)
)
search_get_request_model: Type[BaseSearchGetRequest] = attr.ib(
default=BaseSearchGetRequest
Expand Down
5 changes: 5 additions & 0 deletions stac_fastapi/types/stac_fastapi/types/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class ApiSettings(BaseSettings):
# `pydantic.BaseSettings` instead
default_includes: Optional[Set[str]] = None

stac_fastapi_title: str = "stac-fastapi"
stac_fastapi_description: str = "stac-fastapi"
stac_fastapi_version: str = "0.1"
stac_fastapi_landing_id: str = "stac-fastapi"

app_host: str = "0.0.0.0"
app_port: int = 8000
reload: bool = True
Expand Down
12 changes: 6 additions & 6 deletions stac_fastapi/types/stac_fastapi/types/core.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Base clients."""

import abc
import os
from typing import Any, Dict, List, Optional, Union
from urllib.parse import urljoin

Expand All @@ -13,6 +12,7 @@
from starlette.responses import Response

from stac_fastapi.types import stac as stac_types
from stac_fastapi.types.config import ApiSettings
from stac_fastapi.types.conformance import BASE_CONFORMANCE_CLASSES
from stac_fastapi.types.extension import ApiExtension
from stac_fastapi.types.requests import get_base_url
Expand All @@ -23,6 +23,8 @@
NumType = Union[float, int]
StacType = Dict[str, Any]

api_settings = ApiSettings()


@attr.s # type:ignore
class BaseTransactionsClient(abc.ABC):
Expand Down Expand Up @@ -256,11 +258,9 @@ class LandingPageMixin(abc.ABC):
"""Create a STAC landing page (GET /)."""

stac_version: str = attr.ib(default=STAC_VERSION)
landing_page_id: str = attr.ib(default=os.getenv("STAC_FASTAPI_ID", "0.1"))
title: str = attr.ib(default=os.getenv("STAC_FASTAPI_TITLE", "stac-fastapi"))
description: str = attr.ib(
default=os.getenv("STAC_FASTAPI_DESCRIPTION", "stac-fastapi")
)
landing_page_id: str = attr.ib(default=api_settings.stac_fastapi_landing_id)
title: str = attr.ib(default=api_settings.stac_fastapi_title)
description: str = attr.ib(default=api_settings.stac_fastapi_description)

def _landing_page(
self,
Expand Down

0 comments on commit 82816fa

Please sign in to comment.