✨ Endpoints qBittorrent (GET/PUT/test) persistant dans conf.json
This commit is contained in:
1 parent
de4a9b212f
commit
cfc0c54181
2 files changed
+86
No files matched your search
+48
@@ -16,6 +16,7 @@ from app import auth
|
|||||||
from app.conf_store import read_config, write_config
|
from app.conf_store import read_config, write_config
|
||||||
from app.database import Base, engine, get_db
|
from app.database import Base, engine, get_db
|
||||||
from app.models import IsoMovie
|
from app.models import IsoMovie
|
||||||
|
from app.qbittorrent import QbittorrentClient
|
||||||
from app.radarr import RadarrClient, movie_file_path
|
from app.radarr import RadarrClient, movie_file_path
|
||||||
from app.schemas import (
|
from app.schemas import (
|
||||||
ApiServiceSettingsRead,
|
ApiServiceSettingsRead,
|
||||||
@@ -26,6 +27,7 @@ from app.schemas import (
|
|||||||
HealthRead,
|
HealthRead,
|
||||||
IsoMovieRead,
|
IsoMovieRead,
|
||||||
QbittorrentSettingsRead,
|
QbittorrentSettingsRead,
|
||||||
|
QbittorrentSettingsWrite,
|
||||||
RadarrSettingsRead,
|
RadarrSettingsRead,
|
||||||
RadarrSettingsWrite,
|
RadarrSettingsWrite,
|
||||||
ScanResult,
|
ScanResult,
|
||||||
@@ -172,6 +174,52 @@ async def test_radarr() -> ConnectionResult:
|
|||||||
return ConnectionResult(success=True, version=version, message=f"Radarr {version} accessible")
|
return ConnectionResult(success=True, version=version, message=f"Radarr {version} accessible")
|
||||||
|
|
||||||
|
|
||||||
|
def qbittorrent_from_config() -> QbittorrentClient:
|
||||||
|
qb = read_config().qbittorrent
|
||||||
|
if not qb.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)
|
||||||
|
|
||||||
|
|
||||||
|
@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
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.put("/api/settings/qbittorrent", response_model=QbittorrentSettingsRead)
|
||||||
|
def put_qbittorrent_settings(payload: QbittorrentSettingsWrite) -> QbittorrentSettingsRead:
|
||||||
|
cfg = read_config()
|
||||||
|
if payload.url is not None:
|
||||||
|
cfg.qbittorrent.url = str(payload.url).rstrip("/")
|
||||||
|
if payload.username:
|
||||||
|
cfg.qbittorrent.username = payload.username
|
||||||
|
if payload.password:
|
||||||
|
cfg.qbittorrent.password = payload.password
|
||||||
|
write_config(cfg)
|
||||||
|
return QbittorrentSettingsRead(
|
||||||
|
url=cfg.qbittorrent.url,
|
||||||
|
username=cfg.qbittorrent.username,
|
||||||
|
password_configured=cfg.qbittorrent.password is not None,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/api/settings/qbittorrent/test", response_model=ConnectionResult)
|
||||||
|
async def test_qbittorrent() -> ConnectionResult:
|
||||||
|
client = qbittorrent_from_config()
|
||||||
|
try:
|
||||||
|
version = await client.version()
|
||||||
|
except (httpx.HTTPError, ValueError) as error:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=502, detail=f"Connexion qBittorrent impossible: {error}"
|
||||||
|
) from error
|
||||||
|
return ConnectionResult(
|
||||||
|
success=True, version=version, message=f"qBittorrent {version} accessible"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/movies", response_model=list[IsoMovieRead])
|
@app.get("/api/movies", response_model=list[IsoMovieRead])
|
||||||
def list_movies(db: DbSession) -> list[IsoMovie]:
|
def list_movies(db: DbSession) -> list[IsoMovie]:
|
||||||
return list(db.scalars(select(IsoMovie).order_by(IsoMovie.title)).all())
|
return list(db.scalars(select(IsoMovie).order_by(IsoMovie.title)).all())
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
import respx
|
import respx
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
from httpx import Response
|
from httpx import Response
|
||||||
|
|
||||||
|
from app.conf_store import config_path
|
||||||
from app.main import app
|
from app.main import app
|
||||||
|
|
||||||
|
|
||||||
@@ -70,3 +73,38 @@ def test_scan_only_imports_iso_files() -> None:
|
|||||||
assert scan.status_code == 200
|
assert scan.status_code == 200
|
||||||
assert scan.json()["iso_files"] == 1
|
assert scan.json()["iso_files"] == 1
|
||||||
assert [movie["title"] for movie in movies.json()] == ["ISO Movie"]
|
assert [movie["title"] for movie in movies.json()] == ["ISO Movie"]
|
||||||
|
|
||||||
|
|
||||||
|
@respx.mock
|
||||||
|
def test_qbittorrent_connection() -> None:
|
||||||
|
respx.post("http://qbit:8080/api/v2/auth/login").mock(
|
||||||
|
return_value=Response(200, text="Ok.")
|
||||||
|
)
|
||||||
|
respx.get("http://qbit: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"},
|
||||||
|
)
|
||||||
|
assert response.status_code == 200
|
||||||
|
test = client.post("/api/settings/qbittorrent/test")
|
||||||
|
assert test.status_code == 200
|
||||||
|
assert test.json()["version"] == "v5.0.2"
|
||||||
|
|
||||||
|
|
||||||
|
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"},
|
||||||
|
)
|
||||||
|
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"
|
||||||
|
raw = json.loads(config_path().read_text(encoding="utf-8"))
|
||||||
|
assert raw["qbittorrent"]["url"] == "http://qbit:8080"
|
||||||
|
assert raw["qbittorrent"]["username"] == "admin"
|
||||||
|
assert raw["qbittorrent"]["password"] == "pass"
|
||||||
Reference in new issue
Block a user