Skip to content

Commit

Permalink
fix: set default language for new profiles to any
Browse files Browse the repository at this point in the history
* this only applies to products which do have languages in profiles
  • Loading branch information
BlackDark committed Nov 20, 2024
1 parent b9ad772 commit ffd6faa
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 12 deletions.
31 changes: 26 additions & 5 deletions src/clients/radarr-client.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { KyHttpClient } from "../__generated__/ky-client";
import { Api } from "../__generated__/radarr/Api";
import { CustomFormatResource, QualityDefinitionResource, QualityProfileResource } from "../__generated__/radarr/data-contracts";
import {
CustomFormatResource,
LanguageResource,
QualityDefinitionResource,
QualityProfileResource,
} from "../__generated__/radarr/data-contracts";
import { logger } from "../logger";
import { cloneWithJSON } from "../util";
import { IArrClient, validateClientParams } from "./unified-client";

export class RadarrClient implements IArrClient<QualityProfileResource, QualityDefinitionResource, CustomFormatResource> {
export class RadarrClient implements IArrClient<QualityProfileResource, QualityDefinitionResource, CustomFormatResource, LanguageResource> {
private api!: Api<unknown>;
private languageMap: Map<string, LanguageResource> = new Map();

constructor(baseUrl: string, apiKey: string) {
this.initialize(baseUrl, apiKey);
Expand All @@ -24,13 +31,18 @@ export class RadarrClient implements IArrClient<QualityProfileResource, QualityD
this.api = new Api(httpClient);
}

async getLanguages() {
return this.api.v3LanguageList();
}

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

async updateQualityDefinitions(definitions: QualityDefinitionResource[]) {
await this.api.v3QualitydefinitionUpdateUpdate(definitions);
this.api.v3LanguageList();
return this.getQualityDefinitions();
}

Expand All @@ -39,10 +51,19 @@ export class RadarrClient implements IArrClient<QualityProfileResource, QualityD
return this.api.v3QualityprofileList();
}

createQualityProfile(profile: QualityProfileResource): Promise<QualityProfileResource> {
if (profile.language) {
async createQualityProfile(profile: QualityProfileResource): Promise<QualityProfileResource> {
const cloned = cloneWithJSON(profile);

if (this.languageMap.size <= 0) {
const languages = await this.getLanguages();
this.languageMap = new Map(languages.map((i) => [i.name!, i]));
}
return this.api.v3QualityprofileCreate(profile);

if (profile.language == null) {
cloned.language = this.languageMap.get("Any");
}

return this.api.v3QualityprofileCreate(cloned);
}

updateQualityProfile(id: string, profile: QualityProfileResource): Promise<QualityProfileResource> {
Expand Down
9 changes: 8 additions & 1 deletion src/clients/readarr-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import { KyHttpClient } from "../__generated__/ky-client";
import { Api } from "../__generated__/readarr/Api";
import {
CustomFormatResource,
LanguageResource,
MetadataProfileResource,
QualityDefinitionResource,
QualityProfileResource,
} from "../__generated__/readarr/data-contracts";
import { logger } from "../logger";
import { IArrClient, validateClientParams } from "./unified-client";

export class ReadarrClient implements IArrClient<QualityProfileResource, QualityDefinitionResource, CustomFormatResource> {
export class ReadarrClient
implements IArrClient<QualityProfileResource, QualityDefinitionResource, CustomFormatResource, LanguageResource>
{
private api!: Api<unknown>;

constructor(baseUrl: string, apiKey: string) {
Expand All @@ -29,6 +32,10 @@ export class ReadarrClient implements IArrClient<QualityProfileResource, Quality
this.api = new Api(httpClient);
}

async getLanguages() {
return this.api.v1LanguageList();
}

// Quality Management
getQualityDefinitions() {
return this.api.v1QualitydefinitionList();
Expand Down
13 changes: 11 additions & 2 deletions src/clients/sonarr-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { KyHttpClient } from "../__generated__/ky-client";
import { Api } from "../__generated__/sonarr/Api";
import { CustomFormatResource, QualityDefinitionResource, QualityProfileResource } from "../__generated__/sonarr/data-contracts";
import {
CustomFormatResource,
LanguageResource,
QualityDefinitionResource,
QualityProfileResource,
} from "../__generated__/sonarr/data-contracts";
import { logger } from "../logger";
import { IArrClient, validateClientParams } from "./unified-client";

Expand All @@ -10,7 +15,7 @@ export type SonarrQualityProfileResource = {
// Add other common properties that all quality profiles share
};

export class SonarrClient implements IArrClient<QualityProfileResource, QualityDefinitionResource, CustomFormatResource> {
export class SonarrClient implements IArrClient<QualityProfileResource, QualityDefinitionResource, CustomFormatResource, LanguageResource> {
private api!: Api<unknown>;

constructor(baseUrl: string, apiKey: string) {
Expand All @@ -30,6 +35,10 @@ export class SonarrClient implements IArrClient<QualityProfileResource, QualityD
this.api = new Api(httpClient);
}

async getLanguages() {
return this.api.v3LanguageList();
}

// Quality Management
getQualityDefinitions() {
return this.api.v3QualitydefinitionList();
Expand Down
13 changes: 13 additions & 0 deletions src/clients/unified-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,17 @@ export type ArrClientQualityProfile = {
// Add other common properties that all quality profiles share
};

export type ArrClientLanguageResource = {
id?: number;
name?: string | null;
nameLower?: string | null;
};

export interface IArrClient<
QP extends ArrClientQualityProfile = MergedQualityProfileResource,
QD extends ArrClientQualityDefinition = MergedQualityDefinitionResource,
CF extends ArrClientCustomFormat = MergedCustomFormatResource,
L extends ArrClientLanguageResource = ArrClientLanguageResource,
> {
// Quality Management
getQualityDefinitions(): Promise<QD[]>;
Expand All @@ -104,6 +111,8 @@ export interface IArrClient<
updateCustomFormat(id: string, format: CF): Promise<CF>;
deleteCustomFormat(id: string): Promise<void>;

getLanguages(): Promise<L[]>;

// System/Health Check
getSystemStatus(): Promise<any>;
testConnection(): Promise<boolean>;
Expand Down Expand Up @@ -138,6 +147,10 @@ export class UnifiedClient implements IArrClient {
return this.api as T;
}

async getLanguages() {
return this.api.getLanguages();
}

async getQualityDefinitions() {
return await this.api.getQualityDefinitions();
}
Expand Down
41 changes: 37 additions & 4 deletions src/clients/whisparr-client.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
import { KyHttpClient } from "../__generated__/ky-client";
import { Api } from "../__generated__/whisparr/Api";
import { CustomFormatResource, QualityDefinitionResource, QualityProfileResource } from "../__generated__/whisparr/data-contracts";
import {
CustomFormatResource,
LanguageResource,
QualityDefinitionResource,
QualityProfileResource,
} from "../__generated__/whisparr/data-contracts";
import { logger } from "../logger";
import { cloneWithJSON } from "../util";
import { IArrClient, validateClientParams } from "./unified-client";

export class WhisparrClient implements IArrClient<QualityProfileResource, QualityDefinitionResource, CustomFormatResource> {
/**
* Overwrite wrong types for now
*/
declare module "../__generated__/whisparr/data-contracts" {
export interface QualityProfileResource {
language?: Language;
}
}

export class WhisparrClient
implements IArrClient<QualityProfileResource, QualityDefinitionResource, CustomFormatResource, LanguageResource>
{
private api!: Api<unknown>;
private languageMap: Map<string, LanguageResource> = new Map();

constructor(baseUrl: string, apiKey: string) {
this.initialize(baseUrl, apiKey);
Expand All @@ -24,6 +42,10 @@ export class WhisparrClient implements IArrClient<QualityProfileResource, Qualit
this.api = new Api(httpClient);
}

async getLanguages() {
return this.api.v3LanguageList();
}

// Quality Management
getQualityDefinitions() {
return this.api.v3QualitydefinitionList();
Expand All @@ -39,8 +61,19 @@ export class WhisparrClient implements IArrClient<QualityProfileResource, Qualit
return this.api.v3QualityprofileList();
}

createQualityProfile(profile: QualityProfileResource) {
return this.api.v3QualityprofileCreate(profile);
async createQualityProfile(profile: QualityProfileResource): Promise<QualityProfileResource> {
const cloned = cloneWithJSON(profile);

if (this.languageMap.size <= 0) {
const languages = await this.getLanguages();
this.languageMap = new Map(languages.map((i) => [i.name!, i]));
}

if (profile.language == null) {
cloned.language = this.languageMap.get("Any");
}

return this.api.v3QualityprofileCreate(cloned);
}

updateQualityProfile(id: string, profile: QualityProfileResource) {
Expand Down

0 comments on commit ffd6faa

Please sign in to comment.