Skip to content

Commit

Permalink
refactor: use configureApi
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackDark committed Nov 16, 2024
1 parent 1dc0d09 commit 27aed23
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 65 deletions.
62 changes: 0 additions & 62 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ export const getArrApi = (): SonarrApi<unknown> | RadarrApi<unknown> | WhisparrA
throw new Error("Please configure API first.");
};

export const getSonarrApi = () => {
if (sonarrClient) {
return sonarrClient;
}

throw new Error("Please configure API first.");
};

const validateParams = (url: string, apiKey: string, arrType: ArrType) => {
const arrLabel = arrType.toLowerCase();

Expand Down Expand Up @@ -70,60 +62,6 @@ const handleErrorApi = (error: any, arrType: ArrType) => {
throw new Error(message);
};

export const configureSonarrApi = async (url: string, apiKey: string) => {
unsetApi();
validateParams(url, apiKey, "SONARR");

const httpClient = new KyHttpClient({
headers: {
"X-Api-Key": apiKey,
},
prefixUrl: url,
});
const api = new SonarrApi(httpClient);

sonarrClient = api;

try {
await sonarrClient.v3MetadataList();
} catch (error: any) {
handleErrorApi(error, "SONARR");
}

return sonarrClient;
};

export const getRadarrpi = () => {
if (radarrClient) {
return radarrClient;
}

throw new Error("Please configure API first.");
};

export const configureRadarrApi = async (url: string, apiKey: string) => {
unsetApi();
validateParams(url, apiKey, "RADARR");

const httpClient = new KyHttpClient({
headers: {
"X-Api-Key": apiKey,
},
prefixUrl: url,
});
const api = new RadarrApi(httpClient);

radarrClient = api;

try {
await radarrClient.v3MetadataList();
} catch (error: any) {
handleErrorApi(error, "RADARR");
}

return radarrClient;
};

export const configureApi = async (type: ArrType, url: string, apiKey: string) => {
unsetApi();
validateParams(url, apiKey, type);
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "dotenv/config";

import fs from "node:fs";
import { MergedCustomFormatResource } from "./__generated__/mergedTypes";
import { configureApi, configureRadarrApi, configureSonarrApi, getArrApi, unsetApi } from "./api";
import { configureApi, getArrApi, unsetApi } from "./api";
import { getConfig, validateConfig } from "./config";
import { calculateCFsToManage, loadCFFromConfig, loadLocalCfs, loadServerCustomFormats, manageCf, mergeCfSources } from "./custom-formats";
import { loadLocalRecyclarrTemplate } from "./local-importer";
Expand Down Expand Up @@ -312,7 +312,7 @@ const run = async () => {

for (const [instanceName, instance] of Object.entries(sonarrConfig)) {
logger.info(`Processing Sonarr Instance: ${instanceName}`);
await configureSonarrApi(instance.base_url, instance.api_key);
await configureApi("SONARR", instance.base_url, instance.api_key);
await pipeline(instance, "SONARR");
unsetApi();
}
Expand All @@ -327,7 +327,7 @@ const run = async () => {

for (const [instanceName, instance] of Object.entries(radarrConfig)) {
logger.info(`Processing Radarr Instance: ${instanceName}`);
await configureRadarrApi(instance.base_url, instance.api_key);
await configureApi("RADARR", instance.base_url, instance.api_key);
await pipeline(instance, "RADARR");
unsetApi();
}
Expand Down

0 comments on commit 27aed23

Please sign in to comment.