diff --git a/sapphire/common/__init__.py b/sapphire/common/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/sapphire/common/api/schemas/__init__.py b/sapphire/common/api/schemas/__init__.py new file mode 100644 index 00000000..27d9dada --- /dev/null +++ b/sapphire/common/api/schemas/__init__.py @@ -0,0 +1,10 @@ +from .base import BaseResponse, OKResponse +from .enums import ResponseStatus +from .health import HealthResponse + +__all__ = [ + "ResponseStatus", + "OKResponse", + "BaseResponse", + "HealthResponse", +] diff --git a/sapphire/common/api/schemas/base.py b/sapphire/common/api/schemas/base.py new file mode 100644 index 00000000..3a18a330 --- /dev/null +++ b/sapphire/common/api/schemas/base.py @@ -0,0 +1,11 @@ +from pydantic import BaseModel + +from .enums import ResponseStatus + + +class BaseResponse(BaseModel): + status: ResponseStatus + + +class OKResponse(BaseModel): + status: ResponseStatus = ResponseStatus.OK diff --git a/sapphire/common/api/schemas/enums.py b/sapphire/common/api/schemas/enums.py new file mode 100644 index 00000000..9eb2f33f --- /dev/null +++ b/sapphire/common/api/schemas/enums.py @@ -0,0 +1,6 @@ +from enum import Enum + + +class ResponseStatus(Enum): + OK = "ok" + ERROR = "error" diff --git a/sapphire/common/api/schemas/health.py b/sapphire/common/api/schemas/health.py new file mode 100644 index 00000000..6ee3e1ec --- /dev/null +++ b/sapphire/common/api/schemas/health.py @@ -0,0 +1,6 @@ +from .base import BaseResponse + + +class HealthResponse(BaseResponse): + version: str + name: str diff --git a/sapphire/common/broker/service.py b/sapphire/common/broker/service.py index 0a31f51d..036f6ef9 100644 --- a/sapphire/common/broker/service.py +++ b/sapphire/common/broker/service.py @@ -8,10 +8,10 @@ class BaseBrokerService(ServiceMixin): def __init__( - self, - servers: Iterable[str], - topics: Iterable[str], - handlers: Iterable[BaseBrokerHandler], + self, + servers: Iterable[str], + topics: Iterable[str], + handlers: Iterable[BaseBrokerHandler], ): self._handlers = handlers diff --git a/sapphire/common/database/service.py b/sapphire/common/database/service.py index ccb025d9..70c88c48 100644 --- a/sapphire/common/database/service.py +++ b/sapphire/common/database/service.py @@ -17,7 +17,7 @@ def get_alembic_config_path(self) -> pathlib.Path: def get_alembic_config(self) -> AlembicConfig: migrations_path = self.get_alembic_config_path() - + config = AlembicConfig() config.set_main_option("script_location", str(migrations_path)) config.set_main_option("sqlalchemy.url", self._dsn) @@ -28,4 +28,6 @@ def migrate(self): alembic_command.upgrade(self.get_alembic_config(), "head") def create_migration(self, message: str | None = None): - alembic_command.revision(self.get_alembic_config(), message=message, autogenerate=True) + alembic_command.revision( + self.get_alembic_config(), message=message, autogenerate=True + ) diff --git a/sapphire/common/functions.py b/sapphire/common/functions.py new file mode 100644 index 00000000..f72723da --- /dev/null +++ b/sapphire/common/functions.py @@ -0,0 +1,9 @@ +from functools import reduce + + +def get_nested(storage: dict, *keys): + return reduce( + lambda value, key: value.get(key, {}) if isinstance(value, dict) else None, + keys, + storage, + ) diff --git a/sapphire/users/api/v1beta/health.py b/sapphire/users/api/v1beta/health.py index 18e2fed6..6e6311ff 100644 --- a/sapphire/users/api/v1beta/health.py +++ b/sapphire/users/api/v1beta/health.py @@ -1,2 +1,18 @@ -async def health(): - pass +import os +import pathlib +import tomllib + +from sapphire.common.api.schemas import HealthResponse, ResponseStatus +from sapphire.common.functions import get_nested + + +async def health() -> HealthResponse: + path_to_toml_file = pathlib.Path(os.curdir).absolute() / "pyproject.toml" + with open(path_to_toml_file, "rb") as toml_file: + pyproject_data = tomllib.load(toml_file) + + return HealthResponse( + status=ResponseStatus.OK, + version=get_nested(pyproject_data, "tool", "poetry", "version"), + name="users", + )