Skip to content

Commit

Permalink
refactor: rewrite api client usage
Browse files Browse the repository at this point in the history
* further work will be needed if APIs diverge further
  • Loading branch information
BlackDark committed Nov 17, 2024
1 parent ffd9e64 commit 146a56f
Show file tree
Hide file tree
Showing 10 changed files with 589 additions and 129 deletions.
109 changes: 0 additions & 109 deletions src/api.ts

This file was deleted.

94 changes: 94 additions & 0 deletions src/clients/radarr-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { KyHttpClient } from "../__generated__/ky-client";
import { MergedCustomFormatResource } from "../__generated__/mergedTypes";
import { Api } from "../__generated__/radarr/Api";
import { logger } from "../logger";
import { IArrClient, validateClientParams } from "./unified-client";

export class RadarrClient implements IArrClient {
private api!: Api<unknown>;

constructor(baseUrl: string, apiKey: string) {
this.initialize(baseUrl, apiKey);
}

private initialize(baseUrl: string, apiKey: string) {
validateClientParams(baseUrl, apiKey, "RADARR");

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

this.api = new Api(httpClient);
}

// Quality Management
getQualityDefinitions() {
return this.api.v3QualitydefinitionList();
}

updateQualityDefinitions(definitions: any) {
return this.api.v3QualitydefinitionUpdateUpdate(definitions);
}

// Quality Profiles
getQualityProfiles() {
return this.api.v3QualityprofileList();
}

createQualityProfile(profile: any) {
return this.api.v3QualityprofileCreate(profile);
}

updateQualityProfile(id: string, profile: any) {
return this.api.v3QualityprofileUpdate(id, profile);
}

// Custom Formats
getCustomFormats() {
return this.api.v3CustomformatList();
}

createCustomFormat(format: MergedCustomFormatResource) {
return this.api.v3CustomformatCreate(format);
}

updateCustomFormat(id: string, format: MergedCustomFormatResource) {
return this.api.v3CustomformatUpdate(id, format);
}

deleteCustomFormat(id: string) {
return this.api.v3CustomformatDelete(+id);
}

// Metadata Profiles
async getMetadataProfiles() {
throw new Error("Metadata profiles are not supported in Radarr");
}

async createMetadataProfile(profile: any) {
throw new Error("Metadata profiles are not supported in Radarr");
}

async updateMetadataProfile(id: number, profile: any) {
throw new Error("Metadata profiles are not supported in Radarr");
}

// System/Health Check
getSystemStatus() {
return this.api.v3SystemStatusList();
}

async testConnection() {
try {
await this.api.v3HealthList();
} catch (error) {
logger.error(error);
return false;
}

return true;
}
}
94 changes: 94 additions & 0 deletions src/clients/readarr-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { KyHttpClient } from "../__generated__/ky-client";
import { MergedCustomFormatResource } from "../__generated__/mergedTypes";
import { Api } from "../__generated__/readarr/Api";
import { logger } from "../logger";
import { IArrClient, validateClientParams } from "./unified-client";

export class ReadarrClient implements IArrClient {
private api!: Api<unknown>;

constructor(baseUrl: string, apiKey: string) {
this.initialize(baseUrl, apiKey);
}

private initialize(baseUrl: string, apiKey: string) {
validateClientParams(baseUrl, apiKey, "READARR");

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

this.api = new Api(httpClient);
}

// Quality Management
getQualityDefinitions() {
return this.api.v1QualitydefinitionList();
}

updateQualityDefinitions(definitions: any) {
return this.api.v1QualitydefinitionUpdateUpdate(definitions);
}

// Quality Profiles
getQualityProfiles() {
return this.api.v1QualityprofileList();
}

createQualityProfile(profile: any) {
return this.api.v1QualityprofileCreate(profile);
}

updateQualityProfile(id: string, profile: any) {
return this.api.v1QualityprofileUpdate(id, profile);
}

// Custom Formats
getCustomFormats() {
return this.api.v1CustomformatList();
}

createCustomFormat(format: MergedCustomFormatResource) {
return this.api.v1CustomformatCreate(format);
}

updateCustomFormat(id: string, format: MergedCustomFormatResource) {
return this.api.v1CustomformatUpdate(id, format);
}

deleteCustomFormat(id: string) {
return this.api.v1CustomformatDelete(+id);
}

// Metadata Profiles
async getMetadataProfiles() {
return this.api.v1MetadataprofileList();
}

async createMetadataProfile(profile: any) {
return this.api.v1MetadataprofileCreate(profile);
}

async updateMetadataProfile(id: number, profile: any) {
return this.api.v1MetadataprofileUpdate(id.toString(), profile);
}

// System/Health Check
getSystemStatus() {
return this.api.v1SystemStatusList();
}

async testConnection() {
try {
await this.api.v1HealthList();
} catch (error) {
logger.error(error);
return false;
}

return true;
}
}
94 changes: 94 additions & 0 deletions src/clients/sonarr-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { KyHttpClient } from "../__generated__/ky-client";
import { MergedCustomFormatResource } from "../__generated__/mergedTypes";
import { Api } from "../__generated__/sonarr/Api";
import { logger } from "../logger";
import { IArrClient, validateClientParams } from "./unified-client";

export class SonarrClient implements IArrClient {
private api!: Api<unknown>;

constructor(baseUrl: string, apiKey: string) {
this.initialize(baseUrl, apiKey);
}

private initialize(baseUrl: string, apiKey: string) {
validateClientParams(baseUrl, apiKey, "SONARR");

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

this.api = new Api(httpClient);
}

// Quality Management
getQualityDefinitions() {
return this.api.v3QualitydefinitionList();
}

updateQualityDefinitions(definitions: any) {
return this.api.v3QualitydefinitionUpdateUpdate(definitions);
}

// Quality Profiles
getQualityProfiles() {
return this.api.v3QualityprofileList();
}

createQualityProfile(profile: any) {
return this.api.v3QualityprofileCreate(profile);
}

updateQualityProfile(id: string, profile: any) {
return this.api.v3QualityprofileUpdate(id, profile);
}

// Custom Formats
getCustomFormats() {
return this.api.v3CustomformatList();
}

createCustomFormat(format: MergedCustomFormatResource) {
return this.api.v3CustomformatCreate(format);
}

updateCustomFormat(id: string, format: MergedCustomFormatResource) {
return this.api.v3CustomformatUpdate(id, format);
}

deleteCustomFormat(id: string) {
return this.api.v3CustomformatDelete(+id);
}

// Metadata Profiles
async getMetadataProfiles() {
throw new Error("Metadata profiles are not supported in Sonarr");
}

async createMetadataProfile(profile: any) {
throw new Error("Metadata profiles are not supported in Sonarr");
}

async updateMetadataProfile(id: number, profile: any) {
throw new Error("Metadata profiles are not supported in Sonarr");
}

// System/Health Check
getSystemStatus() {
return this.api.v3SystemStatusList();
}

async testConnection() {
try {
await this.api.v3HealthList();
} catch (error) {
logger.error(error);
return false;
}

return true;
}
}
Loading

0 comments on commit 146a56f

Please sign in to comment.