parent
49a764e864
commit
5b21444c2e
@ -0,0 +1,27 @@
|
||||
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: pyloadData, error: pyloadError } = useWidgetAPI(
|
||||
widget,
|
||||
'statusServer',
|
||||
)
|
||||
|
||||
if (pyloadError || !pyloadData) {
|
||||
return <Container error={t('widget.api_error')} />
|
||||
}
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="pyload.speed" value={t("common.bitrate", { value: pyloadData.speed })} />
|
||||
<Block label="pyload.active" value={t("common.number", { value: pyloadData.active })} />
|
||||
<Block label="pyload.queue" value={t("common.number", { value: pyloadData.queue })} />
|
||||
<Block label="pyload.total" value={t("common.number", { value: pyloadData.total })} />
|
||||
</Container>
|
||||
)
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
import getServiceWidget from "utils/config/service-helpers";
|
||||
import { formatApiCall } from "utils/proxy/api-helpers";
|
||||
import widgets from "widgets/widgets";
|
||||
|
||||
export default async function pyloadProxyHandler(req, res) {
|
||||
const { group, service, endpoint } = req.query;
|
||||
|
||||
if (group && service) {
|
||||
const widget = await getServiceWidget(group, service);
|
||||
|
||||
if (widget) {
|
||||
const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
|
||||
const loginUrl = `${widget.url}/api/login`;
|
||||
|
||||
// Pyload api does not support argument passing as JSON.
|
||||
const sessionId = await fetch(loginUrl, {
|
||||
method: "POST",
|
||||
// Empty passwords are supported.
|
||||
body: `username=${widget.username}&password=${widget.password ?? ''}`,
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
}).then((response) => response.json());
|
||||
|
||||
const apiResponse = await fetch(url, {
|
||||
method: "POST",
|
||||
body: `session=${sessionId}`,
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
}).then((response) => response.json());
|
||||
|
||||
return res.send(apiResponse);
|
||||
}
|
||||
}
|
||||
|
||||
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
import pyloadProxyHandler from "./proxy";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/api/{endpoint}",
|
||||
proxyHandler: pyloadProxyHandler,
|
||||
};
|
||||
|
||||
export default widget;
|
Loading…
Reference in new issue