✨ Champ port optionnel (Radarr + qBittorrent) et normalisation des adresses locales
This commit is contained in:
1 parent
f0b5f2b21e
commit
76dbd7e425
6 files changed
+127
-35
No files matched your search
@@ -2,6 +2,7 @@ import json
|
||||
import os
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
@@ -10,15 +11,35 @@ from app.config import settings
|
||||
|
||||
class ApiServiceConfig(BaseModel):
|
||||
url: str | None = None
|
||||
port: int | None = None
|
||||
api_key: str | None = None
|
||||
|
||||
|
||||
class QbittorrentConfig(BaseModel):
|
||||
url: str | None = None
|
||||
port: int | None = None
|
||||
username: str | None = None
|
||||
password: str | None = None
|
||||
|
||||
|
||||
def build_base_url(url: str | None, port: int | None = None) -> str | None:
|
||||
"""Normalise une adresse de service en URL de base.
|
||||
|
||||
Accepte une adresse locale sans schema ("http://" est deduit) et un port
|
||||
optionnel separe, applique seulement si l'URL n'en porte pas deja un.
|
||||
"""
|
||||
if not url:
|
||||
return None
|
||||
base = str(url).strip().rstrip("/")
|
||||
if "://" not in base:
|
||||
base = f"http://{base}"
|
||||
if port:
|
||||
parsed = urlparse(base)
|
||||
if parsed.port is None:
|
||||
base = f"{parsed.scheme}://{parsed.netloc}:{port}"
|
||||
return base
|
||||
|
||||
|
||||
class AppConfig(BaseModel):
|
||||
radarr: ApiServiceConfig = Field(default_factory=ApiServiceConfig)
|
||||
sonarr: ApiServiceConfig = Field(default_factory=ApiServiceConfig)
|
||||
|
||||
+34
-17
@@ -14,7 +14,7 @@ from sqlalchemy import inspect, select, text, update
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app import auth
|
||||
from app.conf_store import read_config, write_config
|
||||
from app.conf_store import ApiServiceConfig, build_base_url, read_config, write_config
|
||||
from app.converter import cancel_conversion, conversion_paths, finalize_validation
|
||||
from app.database import Base, engine, get_db
|
||||
from app.models import IsoMovie
|
||||
@@ -82,18 +82,20 @@ app.add_middleware(
|
||||
DbSession = Annotated[Session, Depends(get_db)]
|
||||
|
||||
|
||||
def _api_service_read(url: str | None, api_key: str | None) -> ApiServiceSettingsRead:
|
||||
return ApiServiceSettingsRead(url=url, api_key_configured=api_key is not None)
|
||||
def _api_service_read(service: ApiServiceConfig) -> ApiServiceSettingsRead:
|
||||
return ApiServiceSettingsRead(
|
||||
url=service.url, port=service.port, api_key_configured=service.api_key is not None
|
||||
)
|
||||
|
||||
|
||||
def _settings_read() -> SettingsRead:
|
||||
cfg = read_config()
|
||||
return SettingsRead(
|
||||
radarr=_api_service_read(cfg.radarr.url, cfg.radarr.api_key),
|
||||
sonarr=_api_service_read(cfg.sonarr.url, cfg.sonarr.api_key),
|
||||
prowlarr=_api_service_read(cfg.prowlarr.url, cfg.prowlarr.api_key),
|
||||
jellyfin=_api_service_read(cfg.jellyfin.url, cfg.jellyfin.api_key),
|
||||
seerr=_api_service_read(cfg.seerr.url, cfg.seerr.api_key),
|
||||
radarr=_api_service_read(cfg.radarr),
|
||||
sonarr=_api_service_read(cfg.sonarr),
|
||||
prowlarr=_api_service_read(cfg.prowlarr),
|
||||
jellyfin=_api_service_read(cfg.jellyfin),
|
||||
seerr=_api_service_read(cfg.seerr),
|
||||
qbittorrent=QbittorrentSettingsRead(
|
||||
url=cfg.qbittorrent.url,
|
||||
username=cfg.qbittorrent.username,
|
||||
@@ -104,7 +106,7 @@ def _settings_read() -> SettingsRead:
|
||||
|
||||
def client_from_config() -> RadarrClient:
|
||||
cfg = read_config()
|
||||
url = (cfg.radarr.url or "").rstrip("/")
|
||||
url = build_base_url(cfg.radarr.url, cfg.radarr.port)
|
||||
api_key = cfg.radarr.api_key
|
||||
if not url or not api_key:
|
||||
raise HTTPException(status_code=409, detail="La connexion Radarr n'est pas configurée")
|
||||
@@ -124,8 +126,12 @@ def health() -> HealthRead:
|
||||
|
||||
@app.get("/api/settings/radarr", response_model=RadarrSettingsRead)
|
||||
def get_radarr_settings() -> RadarrSettingsRead:
|
||||
cfg = read_config()
|
||||
return RadarrSettingsRead(url=cfg.radarr.url, api_key_configured=cfg.radarr.api_key is not None)
|
||||
radarr = read_config().radarr
|
||||
return RadarrSettingsRead(
|
||||
url=radarr.url,
|
||||
port=radarr.port,
|
||||
api_key_configured=radarr.api_key is not None,
|
||||
)
|
||||
|
||||
|
||||
@app.put("/api/settings/radarr", response_model=RadarrSettingsRead)
|
||||
@@ -133,12 +139,17 @@ def put_radarr_settings(
|
||||
payload: RadarrSettingsWrite,
|
||||
) -> RadarrSettingsRead:
|
||||
cfg = read_config()
|
||||
url = str(payload.url).rstrip("/")
|
||||
url = str(payload.url).strip().rstrip("/")
|
||||
cfg.radarr.url = url
|
||||
cfg.radarr.port = payload.port
|
||||
if payload.api_key:
|
||||
cfg.radarr.api_key = payload.api_key
|
||||
write_config(cfg)
|
||||
return RadarrSettingsRead(url=url, api_key_configured=cfg.radarr.api_key is not None)
|
||||
return RadarrSettingsRead(
|
||||
url=url,
|
||||
port=cfg.radarr.port,
|
||||
api_key_configured=cfg.radarr.api_key is not None,
|
||||
)
|
||||
|
||||
|
||||
@app.get("/api/settings", response_model=SettingsRead)
|
||||
@@ -192,16 +203,20 @@ async def test_radarr() -> ConnectionResult:
|
||||
|
||||
def qbittorrent_from_config() -> QbittorrentClient:
|
||||
qb = read_config().qbittorrent
|
||||
if not qb.url or not qb.username or not qb.password:
|
||||
url = build_base_url(qb.url, qb.port)
|
||||
if not url or not qb.username or not qb.password:
|
||||
raise HTTPException(status_code=409, detail="La connexion qBittorrent n'est pas configurée")
|
||||
return QbittorrentClient(qb.url, qb.username, qb.password)
|
||||
return QbittorrentClient(url, qb.username, qb.password)
|
||||
|
||||
|
||||
@app.get("/api/settings/qbittorrent", response_model=QbittorrentSettingsRead)
|
||||
def get_qbittorrent_settings() -> QbittorrentSettingsRead:
|
||||
qb = read_config().qbittorrent
|
||||
return QbittorrentSettingsRead(
|
||||
url=qb.url, username=qb.username, password_configured=qb.password is not None
|
||||
url=qb.url,
|
||||
port=qb.port,
|
||||
username=qb.username,
|
||||
password_configured=qb.password is not None,
|
||||
)
|
||||
|
||||
|
||||
@@ -209,7 +224,8 @@ def get_qbittorrent_settings() -> QbittorrentSettingsRead:
|
||||
def put_qbittorrent_settings(payload: QbittorrentSettingsWrite) -> QbittorrentSettingsRead:
|
||||
cfg = read_config()
|
||||
if payload.url is not None:
|
||||
cfg.qbittorrent.url = str(payload.url).rstrip("/")
|
||||
cfg.qbittorrent.url = str(payload.url).strip().rstrip("/")
|
||||
cfg.qbittorrent.port = payload.port
|
||||
if payload.username:
|
||||
cfg.qbittorrent.username = payload.username
|
||||
if payload.password:
|
||||
@@ -217,6 +233,7 @@ def put_qbittorrent_settings(payload: QbittorrentSettingsWrite) -> QbittorrentSe
|
||||
write_config(cfg)
|
||||
return QbittorrentSettingsRead(
|
||||
url=cfg.qbittorrent.url,
|
||||
port=cfg.qbittorrent.port,
|
||||
username=cfg.qbittorrent.username,
|
||||
password_configured=cfg.qbittorrent.password is not None,
|
||||
)
|
||||
|
||||
+10
-4
@@ -1,36 +1,42 @@
|
||||
from datetime import datetime
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, HttpUrl
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
|
||||
class RadarrSettingsRead(BaseModel):
|
||||
url: str | None
|
||||
port: int | None = None
|
||||
api_key_configured: bool
|
||||
|
||||
|
||||
class RadarrSettingsWrite(BaseModel):
|
||||
url: HttpUrl
|
||||
url: str
|
||||
port: int | None = Field(default=None, ge=1, le=65535)
|
||||
api_key: str | None = Field(default=None, min_length=1)
|
||||
|
||||
|
||||
class ApiServiceSettingsRead(BaseModel):
|
||||
url: str | None
|
||||
port: int | None = None
|
||||
api_key_configured: bool
|
||||
|
||||
|
||||
class ApiServiceSettingsWrite(BaseModel):
|
||||
url: HttpUrl | None = None
|
||||
url: str | None = None
|
||||
port: int | None = Field(default=None, ge=1, le=65535)
|
||||
api_key: str | None = Field(default=None, min_length=1)
|
||||
|
||||
|
||||
class QbittorrentSettingsRead(BaseModel):
|
||||
url: str | None
|
||||
port: int | None = None
|
||||
username: str | None
|
||||
password_configured: bool
|
||||
|
||||
|
||||
class QbittorrentSettingsWrite(BaseModel):
|
||||
url: HttpUrl | None = None
|
||||
url: str | None = None
|
||||
port: int | None = Field(default=None, ge=1, le=65535)
|
||||
username: str | None = Field(default=None, min_length=1)
|
||||
password: str | None = Field(default=None, min_length=1)
|
||||
|
||||
|
||||
@@ -6,11 +6,12 @@ The project now externalises its configuration into ./config/conf.json.
|
||||
This module is kept temporarily because some imports may still reference it.
|
||||
"""
|
||||
|
||||
from app.conf_store import read_config
|
||||
from app.conf_store import build_base_url, read_config
|
||||
|
||||
|
||||
def radarr_credentials(_: object | None = None) -> tuple[str, str]:
|
||||
cfg = read_config()
|
||||
if not cfg.radarr.url or not cfg.radarr.api_key:
|
||||
url = build_base_url(cfg.radarr.url, cfg.radarr.port)
|
||||
if not url or not cfg.radarr.api_key:
|
||||
raise ValueError("La connexion Radarr n'est pas configurée")
|
||||
return cfg.radarr.url.rstrip("/"), cfg.radarr.api_key
|
||||
return url, cfg.radarr.api_key
|
||||
Reference in new issue
Block a user