initial commit
This commit is contained in:
0
domain/__init__.py
Normal file
0
domain/__init__.py
Normal file
59
domain/entities.py
Normal file
59
domain/entities.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from typing import Any
|
||||
|
||||
|
||||
class RouteStatus(Enum):
|
||||
OPEN = "RTOPEN"
|
||||
CLOSE = "RTCLOSE"
|
||||
|
||||
|
||||
class RouteMode(Enum):
|
||||
NORMAL = "normal"
|
||||
RECONFIGURE = "reconfigure"
|
||||
|
||||
|
||||
@dataclass
|
||||
class Route:
|
||||
name: str
|
||||
mode: RouteMode = RouteMode.NORMAL
|
||||
status: str = "close"
|
||||
|
||||
@property
|
||||
def is_open(self) -> bool:
|
||||
return self.status == "open"
|
||||
|
||||
@property
|
||||
def is_reconfigurable(self) -> bool:
|
||||
return self.mode == RouteMode.RECONFIGURE
|
||||
|
||||
|
||||
@dataclass
|
||||
class ApiResponse:
|
||||
ok: bool
|
||||
data: dict[str, Any]
|
||||
http_code: int
|
||||
headers: dict[str, str] = field(default_factory=dict)
|
||||
|
||||
@property
|
||||
def is_rate_limited(self) -> bool:
|
||||
return self.http_code == 429
|
||||
|
||||
@property
|
||||
def is_server_error(self) -> bool:
|
||||
return self.http_code == 503
|
||||
|
||||
@property
|
||||
def retry_after(self) -> int:
|
||||
return self.data.get("retry_after", 5)
|
||||
|
||||
@property
|
||||
def message(self) -> str:
|
||||
return self.data.get("message", "")
|
||||
|
||||
@property
|
||||
def flag(self) -> str | None:
|
||||
msg = self.message
|
||||
if msg and "{FLG:" in msg:
|
||||
return msg
|
||||
return None
|
||||
24
domain/ports.py
Normal file
24
domain/ports.py
Normal file
@@ -0,0 +1,24 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any
|
||||
|
||||
from domain.entities import ApiResponse
|
||||
|
||||
|
||||
class RailwayApiPort(ABC):
|
||||
"""Port (interfejs) do komunikacji z API kolejowym."""
|
||||
|
||||
@abstractmethod
|
||||
def send_action(self, action: str, **params: Any) -> ApiResponse:
|
||||
...
|
||||
|
||||
|
||||
class EventBusPort(ABC):
|
||||
"""Port do architektury opartej o zdarzenia (event-driven)."""
|
||||
|
||||
@abstractmethod
|
||||
def emit(self, event: str, data: dict[str, Any]) -> None:
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
def on(self, event: str, callback: Any) -> None:
|
||||
...
|
||||
Reference in New Issue
Block a user