feat(integration): improve integration test connection (#3005)
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
import dayjs from "dayjs";
|
||||
import type { fetch as undiciFetch } from "undici";
|
||||
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/certificates/server";
|
||||
import { ResponseError } from "@homarr/common/server";
|
||||
|
||||
import type { IntegrationTestingInput } from "../../base/integration";
|
||||
import type { TestingResult } from "../../base/test-connection/test-connection-service";
|
||||
import type { DownloadClientJobsAndStatus } from "../../interfaces/downloads/download-client-data";
|
||||
import { DownloadClientIntegration } from "../../interfaces/downloads/download-client-integration";
|
||||
import type { DownloadClientItem } from "../../interfaces/downloads/download-client-items";
|
||||
@@ -9,8 +13,11 @@ import type { DownloadClientStatus } from "../../interfaces/downloads/download-c
|
||||
import type { NzbGetClient } from "./nzbget-types";
|
||||
|
||||
export class NzbGetIntegration extends DownloadClientIntegration {
|
||||
public async testConnectionAsync(): Promise<void> {
|
||||
await this.nzbGetApiCallAsync("version");
|
||||
protected async testingAsync(input: IntegrationTestingInput): Promise<TestingResult> {
|
||||
await this.nzbGetApiCallWithCustomFetchAsync(input.fetchAsync, "version");
|
||||
return {
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
|
||||
public async getClientJobsAndStatusAsync(): Promise<DownloadClientJobsAndStatus> {
|
||||
@@ -93,24 +100,34 @@ export class NzbGetIntegration extends DownloadClientIntegration {
|
||||
private async nzbGetApiCallAsync<CallType extends keyof NzbGetClient>(
|
||||
method: CallType,
|
||||
...params: Parameters<NzbGetClient[CallType]>
|
||||
): Promise<ReturnType<NzbGetClient[CallType]>> {
|
||||
return await this.nzbGetApiCallWithCustomFetchAsync(fetchWithTrustedCertificatesAsync, method, ...params);
|
||||
}
|
||||
|
||||
private async nzbGetApiCallWithCustomFetchAsync<CallType extends keyof NzbGetClient>(
|
||||
fetchAsync: typeof undiciFetch,
|
||||
method: CallType,
|
||||
...params: Parameters<NzbGetClient[CallType]>
|
||||
): Promise<ReturnType<NzbGetClient[CallType]>> {
|
||||
const username = this.getSecretValue("username");
|
||||
const password = this.getSecretValue("password");
|
||||
const url = this.url(`/${encodeURIComponent(username)}:${encodeURIComponent(password)}/jsonrpc`);
|
||||
const body = JSON.stringify({ method, params });
|
||||
return await fetchWithTrustedCertificatesAsync(url, { method: "POST", body })
|
||||
return await fetchAsync(url, { method: "POST", body })
|
||||
.then(async (response) => {
|
||||
if (!response.ok) {
|
||||
throw new Error(response.statusText);
|
||||
throw new ResponseError(response);
|
||||
}
|
||||
return ((await response.json()) as { result: ReturnType<NzbGetClient[CallType]> }).result;
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error instanceof Error) {
|
||||
throw new Error(error.message);
|
||||
} else {
|
||||
throw new Error("Error communicating with NzbGet");
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw new Error("Error communicating with NzbGet", {
|
||||
cause: error,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user