Api: init response envelop

This commit is contained in:
Johannes Kirschbauer
2024-05-26 15:57:10 +02:00
parent f54c518fd7
commit ed171f0264
2 changed files with 34 additions and 2 deletions

View File

@@ -1,9 +1,27 @@
from collections.abc import Callable
from typing import Any, TypeVar
from dataclasses import dataclass
from typing import Any, Generic, Literal, TypeVar
T = TypeVar("T")
ResponseDataType = TypeVar("ResponseDataType")
@dataclass
class ApiError:
message: str
description: str | None
location: list[str] | None
@dataclass
class ApiResponse(Generic[ResponseDataType]):
status: Literal["success", "error"]
errors: list[ApiError] | None
data: ResponseDataType | None
class _MethodRegistry:
def __init__(self) -> None:
self._registry: dict[str, Callable] = {}

View File

@@ -1,13 +1,27 @@
import argparse
import logging
from dataclasses import dataclass
from pathlib import Path
from clan_cli.api import API
from clan_cli.config.machine import set_config_for_machine
log = logging.getLogger(__name__)
@dataclass
class MachineCreateRequest:
name: str
config: dict
@API.register
def create_machine(flake_dir: str | Path, machine: MachineCreateRequest) -> None:
set_config_for_machine(Path(flake_dir), machine.name, machine.config)
def create_command(args: argparse.Namespace) -> None:
set_config_for_machine(args.flake, args.machine, dict())
create_machine(args.flake, MachineCreateRequest(args.machine, dict()))
def register_create_parser(parser: argparse.ArgumentParser) -> None: