synchronize data between configured servers
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 4s

This commit is contained in:
2026-05-23 20:32:04 +02:00
parent 8d684908e6
commit 0f9c90b883
6 changed files with 264 additions and 133 deletions
+36 -12
View File
@@ -48,13 +48,25 @@ MAPPING: Dict[str, str] = {
"mail_account": "api/admin/mailaccounts",
}
class OptionType(Enum):
"""
OptionType defines the type of param for REST API call.
The type PARAM indicates a query parameter.
The type ID indicates the option is an idntifier as part of the path.
"""
PARAM = auto()
ID = auto()
URL = auto()
class Option:
"""
Option is an utility class to simplify options for the REST API call.
The type defines how to handle the value.
"""
def __init__(self, option_type: OptionType, value: str) -> None:
self.type: Optional[OptionType] = option_type
self.value: Optional[str] = value
@@ -71,8 +83,6 @@ class EndPointNotAvailableException(Exception):
Raised when calling an not existing endpoint.
"""
pass
@dataclass
class Login:
@@ -120,6 +130,9 @@ class Server:
self.token_type = str(token_type)
def request(self, log: Logger, table: str, param: Optional[Option] = None):
"""
Requests data from Kontor-API instance by given table and optional parameters.
"""
if not param:
url: str = f"{self.url}/{MAPPING[table]}"
else:
@@ -133,13 +146,19 @@ class Server:
return data
def update(self, log: Logger, table: str, item_id: UUID, file_info: dict):
"""
Updates data to the Kontor-API instance.
"""
url: str = f"{self.url}/{MAPPING[table]}/{item_id}"
headers: Dict[str, str] = {"Authorization": f"Bearer {self.token}"}
update = requests.put(
url, headers=headers, json=file_info, timeout=self.timeout
)
log.info(f"Status: {update.status_code}")
return update
if update.status_code == 404:
raise EndPointNotAvailableException
data = update.json()
return data
@dataclass
@@ -152,7 +171,9 @@ class ApiConfig:
server: List[Server]
def get_server(self, server_name: str) -> Optional[Server]:
""" """
"""
Return server instance by given name or None.
"""
found_server = None
for server in self.server:
if server.name == server_name:
@@ -189,19 +210,22 @@ def get_logger(level, config: str):
def get_api_config(log: Logger, config: str) -> ApiConfig:
"""
Load configuration from file.
"""
dirs = PlatformDirs(config)
api_config = Path(dirs.user_config_dir, "api.yaml")
with open(api_config, "rt") as f:
with open(api_config, "rt", encoding="utf-8") as f:
api_data = yaml.safe_load(f.read())
servers = [Server(**server) for server in api_data["server"]]
login = Login(**(api_data["login"]))
apiConfig = ApiConfig(server=servers, login=login)
log.debug(apiConfig)
api_config_data = ApiConfig(server=servers, login=login)
log.debug(api_config_data)
if not api_data:
log.fatal("API configuration is missing")
return apiConfig
for server in apiConfig.server:
server.login(apiConfig.login, log)
with open(api_config, "w") as f:
return api_config_data
for server in api_config_data.server:
server.login(api_config_data.login, log)
with open(api_config, "w", encoding="utf-8") as f:
yaml.dump(api_data, f)
return apiConfig
return api_config_data