✨ Champ port optionnel (Radarr + qBittorrent) et normalisation des adresses locales

This commit is contained in:
Orionis312050 committed 2026-10-11 20:16:06 +00:00
1 parent f0b5f2b21e
commit 76dbd7e425
6 files changed
+127 -35

No files matched your search

+21
View File
@@ -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
View File
@@ -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
View File
@@ -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)
+4 -3
View File
@@ -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
+20 -5
View File
@@ -76,19 +76,27 @@ def test_scan_only_imports_iso_files() -> None:
@respx.mock
def test_qbittorrent_connection() -> None:
respx.post("http://qbit:8080/api/v2/auth/login").mock(
def test_qbittorrent_connection_local_address_with_port() -> None:
# Adresse locale sans schema + port separe: l'URL de base doit etre
# reconstruite en http://192.168.1.50:8080.
respx.post("http://192.168.1.50:8080/api/v2/auth/login").mock(
return_value=Response(200, text="Ok.")
)
respx.get("http://qbit:8080/api/v2/app/version").mock(
respx.get("http://192.168.1.50:8080/api/v2/app/version").mock(
return_value=Response(200, text="v5.0.2")
)
with TestClient(app) as client:
response = client.put(
"/api/settings/qbittorrent",
json={"url": "http://qbit:8080", "username": "admin", "password": "pass"},
json={
"url": "192.168.1.50",
"port": 8080,
"username": "admin",
"password": "pass",
},
)
assert response.status_code == 200
assert response.json()["port"] == 8080
test = client.post("/api/settings/qbittorrent/test")
assert test.status_code == 200
assert test.json()["version"] == "v5.0.2"
@@ -98,13 +106,20 @@ def test_settings_persist_to_conf_json() -> None:
with TestClient(app) as client:
put = client.put(
"/api/settings/qbittorrent",
json={"url": "http://qbit:8080/", "username": "admin", "password": "pass"},
json={
"url": "http://qbit:8080/",
"port": 8080,
"username": "admin",
"password": "pass",
},
)
got = client.get("/api/settings/qbittorrent")
assert put.status_code == 200
assert put.json()["password_configured"] is True
assert got.json()["url"] == "http://qbit:8080"
assert got.json()["port"] == 8080
raw = json.loads(config_path().read_text(encoding="utf-8"))
assert raw["qbittorrent"]["url"] == "http://qbit:8080"
assert raw["qbittorrent"]["port"] == 8080
assert raw["qbittorrent"]["username"] == "admin"
assert raw["qbittorrent"]["password"] == "pass"
+38 -6
View File
@@ -17,12 +17,17 @@ type Movie = {
last_seen_at: string;
};
type RadarrSettings = { url: string | null; api_key_configured: boolean };
type RadarrSettings = {
url: string | null;
port: number | null;
api_key_configured: boolean;
};
type NotificationKind = "info" | "error" | "warning";
type Notification = { id: number; text: string; kind: NotificationKind };
type QbittorrentSettings = {
url: string | null;
port: number | null;
username: string | null;
password_configured: boolean;
};
@@ -107,9 +112,11 @@ export default function App() {
const [tab, setTab] = useState<Tab>("pending");
const [linkedOnly, setLinkedOnly] = useState(false);
const [url, setUrl] = useState("");
const [radarrPort, setRadarrPort] = useState("");
const [apiKey, setApiKey] = useState("");
const [keyConfigured, setKeyConfigured] = useState(false);
const [qbitUrl, setQbitUrl] = useState("");
const [qbitPort, setQbitPort] = useState("");
const [qbitUsername, setQbitUsername] = useState("");
const [qbitPassword, setQbitPassword] = useState("");
const [qbitConfigured, setQbitConfigured] = useState(false);
@@ -199,8 +206,10 @@ export default function App() {
])
.then(([radarr, qbittorrent]) => {
setUrl(radarr.url ?? "");
setRadarrPort(radarr.port?.toString() ?? "");
setKeyConfigured(radarr.api_key_configured);
setQbitUrl(qbittorrent.url ?? "");
setQbitPort(qbittorrent.port?.toString() ?? "");
setQbitUsername(qbittorrent.username ?? "");
setQbitConfigured(qbittorrent.password_configured);
if (!radarr.api_key_configured || !qbittorrent.password_configured) {
@@ -296,7 +305,7 @@ export default function App() {
const settings = await api<RadarrSettings>("/api/settings/radarr", {
method: "PUT",
headers: jsonHeaders,
body: JSON.stringify({ url, api_key: apiKey || null }),
body: JSON.stringify({ url, port: radarrPort ? Number(radarrPort) : null, api_key: apiKey || null }),
});
setKeyConfigured(settings.api_key_configured);
setApiKey("");
@@ -320,6 +329,7 @@ export default function App() {
headers: jsonHeaders,
body: JSON.stringify({
url: qbitUrl,
port: qbitPort ? Number(qbitPort) : null,
username: qbitUsername,
password: qbitPassword || null,
}),
@@ -912,13 +922,24 @@ export default function App() {
<label>
URL Radarr
<input
type="url"
type="text"
required
placeholder="http://radarr:7878"
placeholder="http://radarr:7878 ou 192.168.1.50"
value={url}
onChange={(event) => setUrl(event.target.value)}
/>
</label>
<label>
Port (optionnel)
<input
type="number"
min={1}
max={65535}
placeholder="7878"
value={radarrPort}
onChange={(event) => setRadarrPort(event.target.value)}
/>
</label>
<label>
Clé API
<input
@@ -947,13 +968,24 @@ export default function App() {
<label>
URL qBittorrent
<input
type="url"
type="text"
required
placeholder="http://qbittorrent:8080"
placeholder="http://qbittorrent:8080 ou 192.168.1.50"
value={qbitUrl}
onChange={(event) => setQbitUrl(event.target.value)}
/>
</label>
<label>
Port (optionnel)
<input
type="number"
min={1}
max={65535}
placeholder="8080"
value={qbitPort}
onChange={(event) => setQbitPort(event.target.value)}
/>
</label>
<label>
Utilisateur
<input