Merge pull request 'drop dependency on fastapi in the cli' (#551) from Mic92-cli into main
All checks were successful
assets1 / test (push) Successful in 22s
checks / test (push) Successful in 46s
checks-impure / test (push) Successful in 1m11s
checks / test (pull_request) Successful in 45s
checks-impure / test (pull_request) Successful in 1m10s

This commit is contained in:
clan-bot 2023-11-21 14:13:13 +00:00
commit 6072159020
6 changed files with 34 additions and 31 deletions

View File

@ -6,14 +6,8 @@ from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import Optional
from fastapi import HTTPException
from clan_cli.dirs import (
machine_settings_file,
nixpkgs_source,
specific_machine_dir,
)
from clan_cli.errors import ClanError
from clan_cli.dirs import machine_settings_file, nixpkgs_source, specific_machine_dir
from clan_cli.errors import ClanError, ClanHttpError
from clan_cli.git import commit_file
from clan_cli.nix import nix_eval
@ -83,9 +77,9 @@ def verify_machine_config(
def config_for_machine(flake_dir: Path, machine_name: str) -> dict:
# read the config from a json file located at {flake}/machines/{machine_name}/settings.json
if not specific_machine_dir(flake_dir, machine_name).exists():
raise HTTPException(
raise ClanHttpError(
msg=f"Machine {machine_name} not found. Create the machine first`",
status_code=404,
detail=f"Machine {machine_name} not found. Create the machine first`",
)
settings_path = machine_settings_file(flake_dir, machine_name)
if not settings_path.exists():
@ -100,9 +94,9 @@ def set_config_for_machine(flake_dir: Path, machine_name: str, config: dict) ->
raise ClanError("Machine name must be a valid hostname")
if "networking" in config and "hostName" in config["networking"]:
if machine_name != config["networking"]["hostName"]:
raise HTTPException(
raise ClanHttpError(
msg="Machine name does not match the 'networking.hostName' setting in the config",
status_code=400,
detail="Machine name does not match the 'networking.hostName' setting in the config",
)
config["networking"]["hostName"] = machine_name
# create machine folder if it doesn't exist

View File

@ -6,12 +6,8 @@ from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import Optional
from fastapi import HTTPException
from clan_cli.dirs import (
nixpkgs_source,
)
from clan_cli.errors import ClanError
from clan_cli.dirs import nixpkgs_source
from clan_cli.errors import ClanError, ClanHttpError
from clan_cli.nix import nix_eval
@ -59,17 +55,14 @@ def machine_schema(
)
if proc.returncode != 0:
print(proc.stderr, file=sys.stderr)
raise ClanError(
f"Failed to check clanImports for existence:\n{proc.stderr}"
raise ClanHttpError(
status_code=400,
msg=f"Failed to check clanImports for existence:\n{proc.stderr}",
)
modules_not_found = json.loads(proc.stdout)
if len(modules_not_found) > 0:
raise HTTPException(
status_code=400,
detail={
"msg": "Some requested clan modules could not be found",
"modules_not_found": modules_not_found,
},
raise ClanHttpError(
msg="Some requested clan modules could not be found", status_code=400
)
# get the schema

View File

@ -2,3 +2,13 @@ class ClanError(Exception):
"""Base class for exceptions in this module."""
pass
class ClanHttpError(ClanError):
status_code: int
msg: str
def __init__(self, status_code: int, msg: str):
self.status_code = status_code
self.msg = msg
super().__init__(msg)

View File

@ -4,7 +4,7 @@ from fastapi import Request, status
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
from ..errors import ClanError
from ..errors import ClanError, ClanHttpError
from .settings import settings
log = logging.getLogger(__name__)
@ -17,7 +17,13 @@ def clan_error_handler(request: Request, exc: Exception) -> JSONResponse:
headers["Access-Control-Allow-Origin"] = "*"
headers["Access-Control-Allow-Methods"] = "*"
if isinstance(exc, ClanError):
if isinstance(exc, ClanHttpError):
return JSONResponse(
status_code=exc.status_code,
content=jsonable_encoder(dict(detail={"msg": exc.msg})),
headers=headers,
)
elif isinstance(exc, ClanError):
log.error(f"ClanError: {exc}")
detail = [
{

View File

@ -43,11 +43,12 @@ let
dependencies = [
argcomplete # optional dependency: if not enabled, shell completion will not work
fastapi
uvicorn # optional dependencies: if not enabled, webui subcommand will not work
];
pytestDependencies = runtimeDependencies ++ dependencies ++ [
fastapi # optional dependencies: if not enabled, webui subcommand will not work
uvicorn # optional dependencies: if not enabled, webui subcommand will not work
#schemathesis # optional for http fuzzing
pytest
pytest-cov

View File

@ -48,7 +48,6 @@ def test_schema_invalid_clan_imports(
"Some requested clan modules could not be found"
in response.json()["detail"]["msg"]
)
assert "non-existing-clan-module" in response.json()["detail"]["modules_not_found"]
def test_create_machine_invalid_hostname(