From dfff28c237b78997608425cf18027c21286038a3 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Fri, 25 Aug 2023 23:11:54 -0700 Subject: [PATCH] Remove calibre-web custom proxy handler --- src/widgets/calibreweb/proxy.js | 65 -------------------------------- src/widgets/calibreweb/widget.js | 4 +- 2 files changed, 2 insertions(+), 67 deletions(-) delete mode 100644 src/widgets/calibreweb/proxy.js diff --git a/src/widgets/calibreweb/proxy.js b/src/widgets/calibreweb/proxy.js deleted file mode 100644 index 366833c4..00000000 --- a/src/widgets/calibreweb/proxy.js +++ /dev/null @@ -1,65 +0,0 @@ -import { formatApiCall } from "utils/proxy/api-helpers"; -import { httpProxy } from "utils/proxy/http"; -import getServiceWidget from "utils/config/service-helpers"; -import createLogger from "utils/logger"; -import widgets from "widgets/widgets"; - -const proxyName = "calibreWebProxyHandler"; -const logger = createLogger(proxyName); - -async function getWidget(req) { - const { group, service } = req.query; - - if (!group || !service) { - logger.debug("Invalid or missing service '%s' or group '%s'", service, group); - return null; - } - - const widget = await getServiceWidget(group, service); - - if (!widget) { - logger.debug("Invalid or missing widget for service '%s' in group '%s'", service, group); - return null; - } - - return widget; -} - -async function apiCall(widget, endpoint) { - const { api } = widgets[widget.type]; - const apiUrl = new URL(formatApiCall(api, { endpoint, ...widget })); - const headers = { - Authorization: `Basic ${Buffer.from(`${widget.username}:${widget.password}`).toString("base64")}` - }; - - const [status, contentType, data] = await httpProxy(apiUrl, { - withCredentials: true, - credentials: "include", - headers, - }); - - if (status !== 200) { - logger.error("Error getting data from CalibreWeb: %s status %d. Data: %s", apiUrl, status, data); - return { status, contentType, data: null }; - } - - return {status, data: JSON.parse(data), contentType}; -} - -export default async function calibreWebProxyHandler(req, res) { - const widget = await getWidget(req); - - const { endpoint } = req.query; - - if (!widget) { - return res.status(400).json({ error: "Invalid proxy service type" }); - } - - const { status, data } = await apiCall(widget, endpoint); - - if (status !== 200) { - return res.status(status).json({error: {message: "HTTP error communicating with CalibreWeb API", data: Buffer.from(data).toString()}}); - } - - return res.status(status).json(data); -} diff --git a/src/widgets/calibreweb/widget.js b/src/widgets/calibreweb/widget.js index a2730056..31172c34 100644 --- a/src/widgets/calibreweb/widget.js +++ b/src/widgets/calibreweb/widget.js @@ -1,8 +1,8 @@ -import calibreWebProxyHandler from "./proxy"; +import genericProxyHandler from "../../utils/proxy/handlers/generic"; const widget = { api: "{url}/{endpoint}", - proxyHandler: calibreWebProxyHandler, + proxyHandler: genericProxyHandler, mappings: { stats: {