Add useNet tile

This commit is contained in:
Meierschlumpf
2022-12-11 16:06:41 +01:00
parent c2571190f6
commit bbc02f38c1
13 changed files with 426 additions and 273 deletions

View File

@@ -3,9 +3,7 @@ import dayjs from 'dayjs';
import duration from 'dayjs/plugin/duration';
import { NextApiRequest, NextApiResponse } from 'next';
import { Client } from 'sabnzbd-api';
import { getConfig } from '../../../../tools/getConfig';
import { getServiceById } from '../../../../tools/hooks/useGetServiceByType';
import { Config } from '../../../../tools/types';
import { getConfig } from '../../../../tools/config/getConfig';
import { NzbgetClient } from './nzbget/nzbget-client';
dayjs.extend(duration);
@@ -17,24 +15,24 @@ export interface UsenetPauseRequestParams {
async function Post(req: NextApiRequest, res: NextApiResponse) {
try {
const configName = getCookie('config-name', { req });
const { config }: { config: Config } = getConfig(configName?.toString() ?? 'default').props;
const config = getConfig(configName?.toString() ?? 'default');
const { serviceId } = req.query as any as UsenetPauseRequestParams;
const service = getServiceById(config, serviceId);
const service = config.services.find((x) => x.id === serviceId);
if (!service) {
throw new Error(`Service with ID "${req.query.serviceId}" could not be found.`);
}
let result;
switch (service.type) {
case 'NZBGet': {
switch (service.integration?.type) {
case 'nzbGet': {
const url = new URL(service.url);
const options = {
host: url.hostname,
port: url.port,
login: service.username,
hash: service.password,
login: service.integration.properties.username,
hash: service.integration.properties.password,
};
const nzbGet = NzbgetClient(options);
@@ -50,18 +48,18 @@ async function Post(req: NextApiRequest, res: NextApiResponse) {
});
break;
}
case 'Sabnzbd': {
if (!service.apiKey) {
case 'sabnzbd': {
if (!service.integration.properties.apiKey) {
throw new Error(`API Key for service "${service.name}" is missing`);
}
const { origin } = new URL(service.url);
result = await new Client(origin, service.apiKey).queuePause();
result = await new Client(origin, service.integration.properties.apiKey).queuePause();
break;
}
default:
throw new Error(`Service type "${service.type}" unrecognized.`);
throw new Error(`Service type "${service.integration?.type}" unrecognized.`);
}
return res.status(200).json(result);