Skip to content
This repository has been archived by the owner on Apr 14, 2024. It is now read-only.

Commit

Permalink
task/29: Add basic schemas & add health handler in user-app (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegYurchik authored Sep 30, 2023
2 parents 077b854 + 8080b6a commit d2ffadb
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 8 deletions.
Empty file added sapphire/common/__init__.py
Empty file.
10 changes: 10 additions & 0 deletions sapphire/common/api/schemas/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .base import BaseResponse, OKResponse
from .enums import ResponseStatus
from .health import HealthResponse

__all__ = [
"ResponseStatus",
"OKResponse",
"BaseResponse",
"HealthResponse",
]
11 changes: 11 additions & 0 deletions sapphire/common/api/schemas/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from pydantic import BaseModel

from .enums import ResponseStatus


class BaseResponse(BaseModel):
status: ResponseStatus


class OKResponse(BaseModel):
status: ResponseStatus = ResponseStatus.OK
6 changes: 6 additions & 0 deletions sapphire/common/api/schemas/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from enum import Enum


class ResponseStatus(Enum):
OK = "ok"
ERROR = "error"
6 changes: 6 additions & 0 deletions sapphire/common/api/schemas/health.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .base import BaseResponse


class HealthResponse(BaseResponse):
version: str
name: str
8 changes: 4 additions & 4 deletions sapphire/common/broker/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions sapphire/common/database/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
)
9 changes: 9 additions & 0 deletions sapphire/common/functions.py
Original file line number Diff line number Diff line change
@@ -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,
)
20 changes: 18 additions & 2 deletions sapphire/users/api/v1beta/health.py
Original file line number Diff line number Diff line change
@@ -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",
)

0 comments on commit d2ffadb

Please sign in to comment.