refactor widget api design

this passes all widget API calls through the backend, with a pluggable design and reusable API handlers
This commit is contained in:
Ben Phelps
2022-09-04 21:58:42 +03:00
parent 975f79f6cc
commit 97bf174b78
27 changed files with 370 additions and 252 deletions

View File

@@ -1,32 +1,15 @@
import useSWR from "swr";
import RuTorrent from "rutorrent-promise";
import { formatBytes } from "utils/stats-helpers";
import Widget from "../widget";
import Block from "../block";
import { formatApiUrl } from "utils/api-helpers";
import { formatBytes } from "utils/stats-helpers";
export default function Rutorrent({ service }) {
const config = service.widget;
function buildApiUrl() {
const { url, username, password } = config;
const options = {
url: `${url}/plugins/httprpc/action.php`,
};
if (username && password) {
options.username = username;
options.password = password;
}
const params = new URLSearchParams(options);
return `/api/widgets/rutorrent?${params.toString()}`;
}
const { data: statusData, error: statusError } = useSWR(buildApiUrl());
const { data: statusData, error: statusError } = useSWR(formatApiUrl(config));
if (statusError) {
return <Widget error="Nzbget API Error" />;