Pyload widget use httpProxy instead of fetch

pull/496/head
Michael Shamoon 2 years ago
parent 8b2b8d7b35
commit 69786600b1

@ -4,6 +4,7 @@ import getServiceWidget from "utils/config/service-helpers";
import { formatApiCall } from "utils/proxy/api-helpers"; import { formatApiCall } from "utils/proxy/api-helpers";
import widgets from "widgets/widgets"; import widgets from "widgets/widgets";
import createLogger from "utils/logger"; import createLogger from "utils/logger";
import { httpProxy } from "utils/proxy/http";
const proxyName = 'pyloadProxyHandler'; const proxyName = 'pyloadProxyHandler';
const logger = createLogger(proxyName); const logger = createLogger(proxyName);
@ -20,21 +21,28 @@ async function fetchFromPyloadAPI(url, sessionId, params) {
if (params) { if (params) {
options.body = Object.keys(params).map(k => `${k}=${params[k]}`).join('&'); options.body = Object.keys(params).map(k => `${k}=${params[k]}`).join('&');
} else { } else {
options.body = `session=${sessionId}` options.body = `session=${sessionId}`;
} }
return fetch(url, options).then((response) => response.json()); // eslint-disable-next-line no-unused-vars
const [status, contentType, data] = await httpProxy(url, options);
return [status, JSON.parse(Buffer.from(data).toString())];
} }
async function login(loginUrl, username, password) { async function login(loginUrl, username, password) {
const sessionId = await fetchFromPyloadAPI(loginUrl, null, { username, password }) const [status, sessionId] = await fetchFromPyloadAPI(loginUrl, null, { username, password });
if (status !== 200) {
throw new Error(`HTTP error ${status} logging into Pyload API, returned: ${sessionId}`);
} else {
cache.put(sessionCacheKey, sessionId); cache.put(sessionCacheKey, sessionId);
return sessionId; return sessionId;
}
} }
export default async function pyloadProxyHandler(req, res) { export default async function pyloadProxyHandler(req, res) {
const { group, service, endpoint } = req.query; const { group, service, endpoint } = req.query;
try {
if (group && service) { if (group && service) {
const widget = await getServiceWidget(group, service); const widget = await getServiceWidget(group, service);
@ -48,22 +56,25 @@ export default async function pyloadProxyHandler(req, res) {
sessionId = await login(loginUrl, widget.username, widget.password); sessionId = await login(loginUrl, widget.username, widget.password);
} }
let apiResponse = await fetchFromPyloadAPI(url, sessionId); let [status, data] = await fetchFromPyloadAPI(url, sessionId);
if (apiResponse?.error === 'Forbidden') { if (status === 403) {
logger.debug("Failed to retrieve data from Pyload API, login and re-try"); logger.debug("Failed to retrieve data from Pyload API, login and re-try");
cache.del(sessionCacheKey); cache.del(sessionCacheKey);
sessionId = await login(loginUrl, widget.username, widget.password); sessionId = await login(loginUrl, widget.username, widget.password);
apiResponse = await fetchFromPyloadAPI(url, sessionId); [status, data] = await fetchFromPyloadAPI(url, sessionId);
} }
if (apiResponse?.error) { if (data?.error || status !== 200) {
return res.status(500).send(apiResponse); return res.status(500).send(Buffer.from(data).toString());
} }
cache.del(sessionCacheKey);
return res.send(apiResponse); return res.json(data);
}
} }
} catch (e) {
logger.error(e);
return res.status(500).send(e.toString());
} }
return res.status(400).json({ error: "Invalid proxy service type" }); return res.status(400).json({ error: "Invalid proxy service type" });

Loading…
Cancel
Save