parent
fb04972701
commit
eb67cf1d6f
@ -1,37 +0,0 @@
|
||||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { widget } = service;
|
||||
const { data: booksData, error: booksError } = useWidgetAPI(widget, "books");
|
||||
const { data: authorsData, error: authorsError } = useWidgetAPI(widget, "authors");
|
||||
const { data: seriesData, error: seriesError } = useWidgetAPI(widget, "series");
|
||||
|
||||
if (booksError || authorsError || seriesError) {
|
||||
const finalError = booksError ?? authorsError ?? seriesError;
|
||||
return <Container service={service} error={finalError} />;
|
||||
}
|
||||
|
||||
if (!booksData || !authorsData || !seriesData) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="calibreweb.books" />
|
||||
<Block label="calibreweb.authors" />
|
||||
<Block label="calibreweb.series" />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="calibreweb.books" value={t("common.number", { value: booksData.feed.entry?.length ?? 0 })} />
|
||||
<Block label="calibreweb.authors" value={t("common.number", { value: authorsData.feed.entry?.length ?? 0 })} />
|
||||
<Block label="calibreweb.series" value={t("common.number", { value: seriesData.feed.entry?.length ?? 0 })} />
|
||||
</Container>
|
||||
);
|
||||
}
|
@ -1,73 +0,0 @@
|
||||
import { xml2json } from "xml-js";
|
||||
|
||||
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 };
|
||||
}
|
||||
|
||||
try {
|
||||
const dataDecoded = xml2json(data.toString(), { compact: true });
|
||||
return {status, data: JSON.parse(dataDecoded), contentType};
|
||||
} catch (e) {
|
||||
logger.error("Error decoding CalibreWeb API data. Data: %s", data.toString());
|
||||
return {status, data: null, 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);
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
import calibreWebProxyHandler from "./proxy";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/{endpoint}",
|
||||
proxyHandler: calibreWebProxyHandler,
|
||||
|
||||
mappings: {
|
||||
books: {
|
||||
endpoint: "opds/books/letter/00",
|
||||
},
|
||||
authors: {
|
||||
endpoint: "opds/author/letter/00",
|
||||
},
|
||||
series: {
|
||||
endpoint: "opds/series/letter/00",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default widget;
|
Loading…
Reference in new issue