-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Maintenance/new settings: settings-library and cleanup (#2736)
- Loading branch information
Showing
78 changed files
with
341 additions
and
157 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
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
6 changes: 6 additions & 0 deletions
6
packages/service-library/src/servicelib/aiohttp/web_exceptions_extension.py
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,6 @@ | ||
from aiohttp.web_exceptions import HTTPClientError | ||
|
||
|
||
class HTTPLocked(HTTPClientError): | ||
# pylint: disable=too-many-ancestors | ||
status_code = 423 |
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,6 @@ | ||
# Units | ||
|
||
GB = 1024 ** 3 | ||
|
||
# Formatting | ||
HEADER_STR = "{:-^50}" |
11 changes: 10 additions & 1 deletion
11
packages/settings-library/src/settings_library/basic_types.py
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 was deleted.
Oops, something went wrong.
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,18 @@ | ||
from pydantic.fields import Field | ||
from pydantic.types import SecretStr | ||
|
||
from .base import BaseCustomSettings | ||
from .basic_types import PortInt | ||
|
||
|
||
class SMTPSettings(BaseCustomSettings): | ||
"""Simple Mail Transfer Protocol""" | ||
|
||
SMTP_SENDER: str = "@".join(["O2SPARC support <support", "osparc.io>"]) | ||
|
||
SMTP_HOST: str | ||
SMTP_PORT: PortInt | ||
|
||
SMTP_TLS_ENABLED: bool = Field(description="Enables Secure Mode") | ||
SMTP_USERNAME: str | ||
SMTP_PASSWORD: SecretStr |
22 changes: 22 additions & 0 deletions
22
packages/settings-library/src/settings_library/prometheus.py
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,22 @@ | ||
from functools import cached_property | ||
|
||
from pydantic.networks import HttpUrl | ||
from settings_library.base import BaseCustomSettings | ||
from settings_library.utils_service import MixinServiceSettings | ||
|
||
from .basic_types import PortInt, VersionTag | ||
|
||
|
||
class PrometheusSettings(BaseCustomSettings, MixinServiceSettings): | ||
PROMETHEUS_HOST: str = "prometheus" | ||
PROMETHEUS_PORT: PortInt = 9090 | ||
PROMETHEUS_VTAG: VersionTag = "v1" | ||
|
||
@cached_property | ||
def base_url(self) -> str: | ||
return HttpUrl.build( | ||
scheme="http", | ||
host=self.PROMETHEUS_HOST, | ||
port=f"{self.PROMETHEUS_PORT}", | ||
path=f"/api/{self.PROMETHEUS_VTAG}/query", | ||
) |
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
File renamed without changes.
72 changes: 72 additions & 0 deletions
72
packages/settings-library/src/settings_library/utils_service.py
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,72 @@ | ||
""" Helpers to build settings for services with http API | ||
""" | ||
|
||
|
||
from pydantic.networks import AnyUrl | ||
|
||
from .basic_types import PortInt | ||
|
||
DEFAULT_AIOHTTP_PORT: PortInt = 8000 | ||
DEFAULT_FASTAPI_PORT: PortInt = 8080 | ||
|
||
|
||
class MixinServiceSettings: | ||
"""Mixin with common helpers based on validated fields with canonical name | ||
Example: | ||
- Subclass should define host, port and vtag fields as | ||
class MyServiceSettings(BaseCustomSettings, MixinServiceSettings): | ||
{prefix}_HOST: str | ||
{prefix}_PORT: PortInt | ||
{prefix}_VTAG: VersionTag [Optional] | ||
# optional | ||
{prefix}_SCHEME: str (urls default to http) | ||
{prefix}_USER: str | ||
{prefix}_PASSWORD: SecretStr | ||
""" | ||
|
||
# | ||
# URL conventions (based on https://yarl.readthedocs.io/en/latest/api.html) | ||
# | ||
# http://user:[email protected]:8042/v0/resource?name=ferret#nose | ||
# \__/ \__/ \__/ \_________/ \__/\__________/ \_________/ \__/ | ||
# | | | | | | | | | ||
# scheme user password host port path query fragment | ||
# | ||
# origin -> http://example.com | ||
# api_base -> http://example.com:8042/v0 | ||
# | ||
|
||
def _build_api_base_url(self, *, prefix: str) -> str: | ||
assert prefix # nosec | ||
prefix = prefix.upper() | ||
password = getattr(self, f"{prefix}_PASSWORD") | ||
vtag = getattr(self, f"{prefix}_VTAG", None) | ||
return AnyUrl.build( | ||
scheme=getattr(self, f"{prefix}_SCHEME", "http"), | ||
user=getattr(self, f"{prefix}_USER", None), | ||
password=password.get_secret_value() if password is not None else None, | ||
host=getattr(self, f"{prefix}_HOST"), | ||
port=f"{getattr(self, f'{prefix}_PORT')}", | ||
path=f"/{vtag}" if vtag is not None else None, | ||
query=None, | ||
fragment=None, | ||
) | ||
|
||
def _build_origin_url(self, *, prefix: str) -> str: | ||
assert prefix # nosec | ||
prefix = prefix.upper() | ||
return AnyUrl.build( | ||
scheme=getattr(self, f"{prefix}_SCHEME", "http"), | ||
user=None, | ||
password=None, | ||
host=getattr(self, f"{prefix}_HOST"), | ||
port=None, | ||
path=None, | ||
query=None, | ||
fragment=None, | ||
) |
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,50 @@ | ||
# pylint: disable=redefined-outer-name | ||
# pylint: disable=unused-argument | ||
# pylint: disable=unused-variable | ||
|
||
from functools import cached_property | ||
from typing import Optional | ||
|
||
from pydantic.types import SecretStr | ||
from settings_library.base import BaseCustomSettings | ||
from settings_library.basic_types import PortInt, VersionTag | ||
from settings_library.utils_service import DEFAULT_AIOHTTP_PORT, MixinServiceSettings | ||
|
||
|
||
def test_mixing_service_settings_usage(monkeypatch): | ||
# this test provides an example of usage | ||
class MySettings(BaseCustomSettings, MixinServiceSettings): | ||
MY_HOST: str = "example.com" | ||
MY_PORT: PortInt = DEFAULT_AIOHTTP_PORT | ||
MY_VTAG: Optional[VersionTag] = None | ||
|
||
# optional | ||
MY_USER: Optional[str] | ||
MY_PASSWORD: Optional[SecretStr] | ||
|
||
@cached_property | ||
def api_base_url(self) -> str: | ||
return self._build_api_base_url(prefix="MY") | ||
|
||
@cached_property | ||
def origin_url(self) -> str: | ||
return self._build_origin_url(prefix="MY") | ||
|
||
settings = MySettings.create_from_envs() | ||
assert settings.api_base_url == "http://example.com:8000" | ||
assert settings.origin_url == "http://example.com" | ||
|
||
# ----------- | ||
monkeypatch.setenv("MY_VTAG", "v9") | ||
|
||
settings = MySettings.create_from_envs() | ||
assert settings.api_base_url == "http://example.com:8000/v9" | ||
assert settings.origin_url == "http://example.com" | ||
|
||
# ----------- | ||
monkeypatch.setenv("MY_USER", "me") | ||
monkeypatch.setenv("MY_PASSWORD", "secret") | ||
|
||
settings = MySettings.create_from_envs() | ||
assert settings.api_base_url == "http://me:[email protected]:8000/v9" | ||
assert settings.origin_url == "http://example.com" |
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
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
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
Oops, something went wrong.