🐛 Fix issue with api endpoints caused by new integration format

This commit is contained in:
Meierschlumpf
2022-12-11 19:33:54 +01:00
parent ed64d138c5
commit 53a86e2bbc
7 changed files with 42 additions and 40 deletions

View File

@@ -1,7 +1,7 @@
import axios from 'axios'; import axios from 'axios';
import { NextApiRequest, NextApiResponse } from 'next'; import { NextApiRequest, NextApiResponse } from 'next';
import { getConfig } from '../../../tools/config/getConfig'; import { getConfig } from '../../../tools/config/getConfig';
import { ServiceIntegrationApiKeyType, ServiceType } from '../../../types/service'; import { ServiceIntegrationType, ServiceType } from '../../../types/service';
/*async function Post(req: NextApiRequest, res: NextApiResponse) { /*async function Post(req: NextApiRequest, res: NextApiResponse) {
// Parse req.body as a ServiceItem // Parse req.body as a ServiceItem
@@ -114,7 +114,7 @@ async function Get(req: NextApiRequest, res: NextApiResponse) {
const medias = await Promise.all( const medias = await Promise.all(
await mediaServices.map(async (service) => { await mediaServices.map(async (service) => {
const integration = service.integration as ServiceIntegrationApiKeyType; const integration = service.integration!;
const endpoint = IntegrationTypeEndpointMap.get(integration.type); const endpoint = IntegrationTypeEndpointMap.get(integration.type);
if (!endpoint) if (!endpoint)
return { return {
@@ -131,14 +131,11 @@ async function Get(req: NextApiRequest, res: NextApiResponse) {
const start = new Date(year, month - 1, 1); // First day of month const start = new Date(year, month - 1, 1); // First day of month
const end = new Date(year, month, 0); // Last day of month const end = new Date(year, month, 0); // Last day of month
console.log( const apiKey = integration.properties.find((x) => x.field === 'apiKey')?.value;
`${origin}${endpoint}?apiKey=${integration.properties.apiKey}&end=${end}&start=${start}` if (!apiKey) return { type: integration.type, items: [] };
);
return await axios return await axios
.get( .get(
`${origin}${endpoint}?apiKey=${ `${origin}${endpoint}?apiKey=${apiKey}&end=${end.toISOString()}&start=${start.toISOString()}`
integration.properties.apiKey
}&end=${end.toISOString()}&start=${start.toISOString()}`
) )
.then((x) => ({ type: integration.type, items: x.data as any[] })); .then((x) => ({ type: integration.type, items: x.data as any[] }));
}) })
@@ -160,5 +157,3 @@ const IntegrationTypeEndpointMap = new Map<ServiceIntegrationType['type'], strin
['lidarr', '/api/v1/calendar'], ['lidarr', '/api/v1/calendar'],
['readarr', '/api/v1/calendar'], ['readarr', '/api/v1/calendar'],
]); ]);
type ServiceIntegrationType = Exclude<ServiceType['integration'], undefined>;

View File

@@ -11,8 +11,6 @@ async function Get(req: NextApiRequest, res: NextApiResponse) {
timeout: 1, timeout: 1,
}); });
console.log(response);
// Return 200 if the alive property is true // Return 200 if the alive property is true
if (response.alive) { if (response.alive) {
return res.status(200).end(); return res.status(200).end();

View File

@@ -3,12 +3,12 @@ import dayjs from 'dayjs';
import duration from 'dayjs/plugin/duration'; import duration from 'dayjs/plugin/duration';
import { NextApiRequest, NextApiResponse } from 'next'; import { NextApiRequest, NextApiResponse } from 'next';
import { Client } from 'sabnzbd-api'; import { Client } from 'sabnzbd-api';
import { UsenetHistoryItem } from '../../../../modules';
import { getServiceById } from '../../../../tools/hooks/useGetServiceByType'; import { getServiceById } from '../../../../tools/hooks/useGetServiceByType';
import { Config } from '../../../../tools/types'; import { Config } from '../../../../tools/types';
import { NzbgetHistoryItem } from './nzbget/types'; import { NzbgetHistoryItem } from './nzbget/types';
import { NzbgetClient } from './nzbget/nzbget-client'; import { NzbgetClient } from './nzbget/nzbget-client';
import { getConfig } from '../../../../tools/config/getConfig'; import { getConfig } from '../../../../tools/config/getConfig';
import { UsenetHistoryItem } from '../../../../components/Dashboard/Tiles/UseNet/types';
dayjs.extend(duration); dayjs.extend(duration);
@@ -42,8 +42,10 @@ async function Get(req: NextApiRequest, res: NextApiResponse) {
const options = { const options = {
host: url.hostname, host: url.hostname,
port: url.port, port: url.port,
login: service.integration.properties.username, login:
hash: service.integration.properties.password, service.integration.properties.find((x) => x.field === 'username')?.value ?? undefined,
hash:
service.integration.properties.find((x) => x.field === 'password')?.value ?? undefined,
}; };
const nzbGet = NzbgetClient(options); const nzbGet = NzbgetClient(options);
@@ -79,14 +81,12 @@ async function Get(req: NextApiRequest, res: NextApiResponse) {
case 'sabnzbd': { case 'sabnzbd': {
const { origin } = new URL(service.url); const { origin } = new URL(service.url);
if (!service.integration.properties.apiKey) { const apiKey = service.integration.properties.find((x) => x.field === 'apiKey')?.value;
if (!apiKey) {
throw new Error(`API Key for service "${service.name}" is missing`); throw new Error(`API Key for service "${service.name}" is missing`);
} }
const history = await new Client(origin, service.integration.properties.apiKey).history( const history = await new Client(origin, apiKey).history(offset, limit);
offset,
limit
);
const items: UsenetHistoryItem[] = history.slots.map((slot) => ({ const items: UsenetHistoryItem[] = history.slots.map((slot) => ({
id: slot.nzo_id, id: slot.nzo_id,

View File

@@ -41,8 +41,10 @@ async function Get(req: NextApiRequest, res: NextApiResponse) {
const options = { const options = {
host: url.hostname, host: url.hostname,
port: url.port, port: url.port,
login: service.integration.properties.username, login:
hash: service.integration.properties.password, service.integration.properties.find((x) => x.field === 'username')?.value ?? undefined,
hash:
service.integration.properties.find((x) => x.field === 'password')?.value ?? undefined,
}; };
const nzbGet = NzbgetClient(options); const nzbGet = NzbgetClient(options);
@@ -72,13 +74,14 @@ async function Get(req: NextApiRequest, res: NextApiResponse) {
break; break;
} }
case 'sabnzbd': { case 'sabnzbd': {
if (!service.integration.properties.apiKey) { const apiKey = service.integration.properties.find((x) => x.field === 'apiKey')?.value;
if (!apiKey) {
throw new Error(`API Key for service "${service.name}" is missing`); throw new Error(`API Key for service "${service.name}" is missing`);
} }
const { origin } = new URL(service.url); const { origin } = new URL(service.url);
const queue = await new Client(origin, service.integration.properties.apiKey).queue(0, -1); const queue = await new Client(origin, apiKey).queue(0, -1);
const [hours, minutes, seconds] = queue.timeleft.split(':'); const [hours, minutes, seconds] = queue.timeleft.split(':');
const eta = dayjs.duration({ const eta = dayjs.duration({

View File

@@ -31,8 +31,10 @@ async function Post(req: NextApiRequest, res: NextApiResponse) {
const options = { const options = {
host: url.hostname, host: url.hostname,
port: url.port, port: url.port,
login: service.integration.properties.username, login:
hash: service.integration.properties.password, service.integration.properties.find((x) => x.field === 'username')?.value ?? undefined,
hash:
service.integration.properties.find((x) => x.field === 'password')?.value ?? undefined,
}; };
const nzbGet = NzbgetClient(options); const nzbGet = NzbgetClient(options);
@@ -49,13 +51,14 @@ async function Post(req: NextApiRequest, res: NextApiResponse) {
break; break;
} }
case 'sabnzbd': { case 'sabnzbd': {
if (!service.integration.properties.apiKey) { const apiKey = service.integration.properties.find((x) => x.field === 'apiKey')?.value;
if (!apiKey) {
throw new Error(`API Key for service "${service.name}" is missing`); throw new Error(`API Key for service "${service.name}" is missing`);
} }
const { origin } = new URL(service.url); const { origin } = new URL(service.url);
result = await new Client(origin, service.integration.properties.apiKey).queuePause(); result = await new Client(origin, apiKey).queuePause();
break; break;
} }
default: default:

View File

@@ -3,7 +3,7 @@ import dayjs from 'dayjs';
import duration from 'dayjs/plugin/duration'; import duration from 'dayjs/plugin/duration';
import { NextApiRequest, NextApiResponse } from 'next'; import { NextApiRequest, NextApiResponse } from 'next';
import { Client } from 'sabnzbd-api'; import { Client } from 'sabnzbd-api';
import { UsenetQueueItem } from '../../../../modules'; import { UsenetQueueItem } from '../../../../components/Dashboard/Tiles/UseNet/types';
import { getConfig } from '../../../../tools/config/getConfig'; import { getConfig } from '../../../../tools/config/getConfig';
import { NzbgetClient } from './nzbget/nzbget-client'; import { NzbgetClient } from './nzbget/nzbget-client';
import { NzbgetQueueItem, NzbgetStatus } from './nzbget/types'; import { NzbgetQueueItem, NzbgetStatus } from './nzbget/types';
@@ -40,8 +40,10 @@ async function Get(req: NextApiRequest, res: NextApiResponse) {
const options = { const options = {
host: url.hostname, host: url.hostname,
port: url.port, port: url.port,
login: service.integration.properties.username, login:
hash: service.integration.properties.password, service.integration.properties.find((x) => x.field === 'username')?.value ?? undefined,
hash:
service.integration.properties.find((x) => x.field === 'password')?.value ?? undefined,
}; };
const nzbGet = NzbgetClient(options); const nzbGet = NzbgetClient(options);
@@ -91,15 +93,13 @@ async function Get(req: NextApiRequest, res: NextApiResponse) {
break; break;
} }
case 'sabnzbd': { case 'sabnzbd': {
if (!service.integration.properties.apiKey) { const apiKey = service.integration.properties.find((x) => x.field === 'apiKey')?.value;
if (!apiKey) {
throw new Error(`API Key for service "${service.name}" is missing`); throw new Error(`API Key for service "${service.name}" is missing`);
} }
const { origin } = new URL(service.url); const { origin } = new URL(service.url);
const queue = await new Client(origin, service.integration.properties.apiKey).queue( const queue = await new Client(origin, apiKey).queue(offset, limit);
offset,
limit
);
const items: UsenetQueueItem[] = queue.slots.map((slot) => { const items: UsenetQueueItem[] = queue.slots.map((slot) => {
const [hours, minutes, seconds] = slot.timeleft.split(':'); const [hours, minutes, seconds] = slot.timeleft.split(':');

View File

@@ -34,8 +34,10 @@ async function Post(req: NextApiRequest, res: NextApiResponse) {
const options = { const options = {
host: url.hostname, host: url.hostname,
port: url.port, port: url.port,
login: service.integration.properties.username, login:
hash: service.integration.properties.password, service.integration.properties.find((x) => x.field === 'username')?.value ?? undefined,
hash:
service.integration.properties.find((x) => x.field === 'password')?.value ?? undefined,
}; };
const nzbGet = NzbgetClient(options); const nzbGet = NzbgetClient(options);
@@ -52,13 +54,14 @@ async function Post(req: NextApiRequest, res: NextApiResponse) {
break; break;
} }
case 'sabnzbd': { case 'sabnzbd': {
if (!service.integration.properties.apiKey) { const apiKey = service.integration.properties.find((x) => x.field === 'apiKey')?.value;
if (!apiKey) {
throw new Error(`API Key for service "${service.name}" is missing`); throw new Error(`API Key for service "${service.name}" is missing`);
} }
const { origin } = new URL(service.url); const { origin } = new URL(service.url);
result = await new Client(origin, service.integration.properties.apiKey).queueResume(); result = await new Client(origin, apiKey).queueResume();
break; break;
} }
default: default: