widget refactoring and cleanup

This commit is contained in:
Ben Phelps 2022-09-26 02:23:02 +03:00
parent 808e79e2ac
commit 47bc073fb4
39 changed files with 92 additions and 1410 deletions

View file

@ -1,25 +1,5 @@
// const formats = {
// emby: `{url}/emby/{endpoint}?api_key={key}`,
// jellyfin: `{url}/emby/{endpoint}?api_key={key}`,
// pihole: `{url}/admin/{endpoint}`,
// speedtest: `{url}/api/{endpoint}`,
// tautulli: `{url}/api/v2?apikey={key}&cmd={endpoint}`,
// traefik: `{url}/api/{endpoint}`,
// portainer: `{url}/api/endpoints/{env}/{endpoint}`,
// rutorrent: `{url}/plugins/httprpc/action.php`,
// transmission: `{url}/transmission/rpc`,
// qbittorrent: `{url}/api/v2/{endpoint}`,
// jellyseerr: `{url}/api/v1/{endpoint}`,
// ombi: `{url}/api/v1/{endpoint}`,
// npm: `{url}/api/{endpoint}`,
// lidarr: `{url}/api/v1/{endpoint}?apikey={key}`,
// readarr: `{url}/api/v1/{endpoint}?apikey={key}`,
// sabnzbd: `{url}/api/?apikey={key}&output=json&mode={endpoint}`,
// gotify: `{url}/{endpoint}`,
// prowlarr: `{url}/api/v1/{endpoint}`,
// jackett: `{url}/api/v2.0/{endpoint}?apikey={key}&configured=true`,
// strelaysrv: `{url}/{endpoint}`,
// mastodon: `{url}/api/v1/{endpoint}`,
// };
export function formatApiCall(url, args) {
@ -45,7 +25,7 @@ function getURLSearchParams(widget, endpoint) {
export function formatProxyUrlWithSegments(widget, endpoint, segments) {
const params = getURLSearchParams(widget, endpoint);
if (segments) {
params.append("segments", JSON.stringify(segments))
params.append("segments", JSON.stringify(segments));
}
return `/api/services/proxy?${params.toString()}`;
}

View file

@ -1,42 +0,0 @@
import getServiceWidget from "utils/service-helpers";
import { formatApiCall } from "utils/api-helpers";
import widgets from "widgets/widgets";
export default async function npmProxyHandler(req, res) {
const { group, service, endpoint } = req.query;
if (group && service) {
const widget = await getServiceWidget(group, service);
if (!widgets?.[widget.type]?.api) {
return res.status(403).json({ error: "Service does not support API calls" });
}
if (widget) {
const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
const loginUrl = `${widget.url}/api/tokens`;
const body = { identity: widget.username, secret: widget.password };
const authResponse = await fetch(loginUrl, {
method: "POST",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
},
}).then((response) => response.json());
const apiResponse = await fetch(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${authResponse.token}`,
},
}).then((response) => response.json());
return res.send(apiResponse);
}
}
return res.status(400).json({ error: "Invalid proxy service type" });
}

View file

@ -1,40 +0,0 @@
import { JSONRPCClient } from "json-rpc-2.0";
import getServiceWidget from "utils/service-helpers";
export default async function nzbgetProxyHandler(req, res) {
const { group, service, endpoint } = req.query;
if (group && service) {
const widget = await getServiceWidget(group, service);
if (widget) {
const constructedUrl = new URL(widget.url);
constructedUrl.pathname = "jsonrpc";
const authorization = Buffer.from(`${widget.username}:${widget.password}`).toString("base64");
const client = new JSONRPCClient((jsonRPCRequest) =>
fetch(constructedUrl.toString(), {
method: "POST",
headers: {
"content-type": "application/json",
authorization: `Basic ${authorization}`,
},
body: JSON.stringify(jsonRPCRequest),
}).then(async (response) => {
if (response.status === 200) {
const jsonRPCResponse = await response.json();
return client.receive(jsonRPCResponse);
}
return Promise.reject(new Error(response.statusText));
})
);
return res.send(await client.request(endpoint));
}
}
return res.status(400).json({ error: "Invalid proxy service type" });
}