mirror of
https://github.com/FJurmanovic/sugarr.git
synced 2026-11-11 15:51:09 +00:00
Add RadarrClient
This commit is contained in:
45
src/app/clients/radarr/RadarrClient.ts
Normal file
45
src/app/clients/radarr/RadarrClient.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { ApiClient } from '../../../core/http/ApiClient';
|
||||
import { LookupEndpoint, type RadarrMovie } from './endpoints/LookupEndpoint';
|
||||
import { AddMovieEndpoint } from './endpoints/AddMovieEndpoint';
|
||||
import { GetExistingEndpoint } from './endpoints/GetExistingEndpoint';
|
||||
|
||||
export type { RadarrMovie } from './endpoints/LookupEndpoint';
|
||||
|
||||
export class RadarrClient extends ApiClient {
|
||||
private readonly lookupEndpoint = new LookupEndpoint();
|
||||
private readonly addMovieEndpoint = new AddMovieEndpoint();
|
||||
private readonly getExistingEndpoint = new GetExistingEndpoint();
|
||||
|
||||
constructor(
|
||||
baseUrl: string,
|
||||
private readonly apiKey: string,
|
||||
private readonly qualityProfileId: number,
|
||||
private readonly rootFolder: string
|
||||
) {
|
||||
super(baseUrl);
|
||||
}
|
||||
|
||||
protected buildHeaders(): Record<string, string> {
|
||||
return { ...super.buildHeaders(), 'X-Api-Key': this.apiKey };
|
||||
}
|
||||
|
||||
async lookup(tmdbId: number): Promise<RadarrMovie> {
|
||||
const result = await this.request(this.lookupEndpoint, { tmdbId });
|
||||
return {
|
||||
title: result.title,
|
||||
tmdbId: result.tmdbId,
|
||||
qualityProfileId: this.qualityProfileId,
|
||||
rootFolderPath: this.rootFolder,
|
||||
monitored: true,
|
||||
addOptions: { searchForMovie: true },
|
||||
};
|
||||
}
|
||||
|
||||
addMovie(movie: RadarrMovie): Promise<void> {
|
||||
return this.request(this.addMovieEndpoint, movie);
|
||||
}
|
||||
|
||||
getExisting(): Promise<RadarrMovie[]> {
|
||||
return this.request(this.getExistingEndpoint, undefined);
|
||||
}
|
||||
}
|
||||
16
src/app/clients/radarr/endpoints/AddMovieEndpoint.ts
Normal file
16
src/app/clients/radarr/endpoints/AddMovieEndpoint.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { Endpoint, type HttpMethod } from '../../../../core/http/Endpoint';
|
||||
import type { RadarrMovie } from './LookupEndpoint';
|
||||
|
||||
export class AddMovieEndpoint extends Endpoint<void, RadarrMovie> {
|
||||
readonly method: HttpMethod = 'POST';
|
||||
readonly acceptableStatuses = [400];
|
||||
path(): string {
|
||||
return '/api/v3/movie';
|
||||
}
|
||||
body(params: RadarrMovie): unknown {
|
||||
return params;
|
||||
}
|
||||
parseResponse(): void {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
12
src/app/clients/radarr/endpoints/GetExistingEndpoint.ts
Normal file
12
src/app/clients/radarr/endpoints/GetExistingEndpoint.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Endpoint, type HttpMethod } from '../../../../core/http/Endpoint';
|
||||
import type { RadarrMovie } from './LookupEndpoint';
|
||||
|
||||
export class GetExistingEndpoint extends Endpoint<RadarrMovie[], void> {
|
||||
readonly method: HttpMethod = 'GET';
|
||||
path(): string {
|
||||
return '/api/v3/movie';
|
||||
}
|
||||
parseResponse(json: unknown): RadarrMovie[] {
|
||||
return json as RadarrMovie[];
|
||||
}
|
||||
}
|
||||
32
src/app/clients/radarr/endpoints/LookupEndpoint.ts
Normal file
32
src/app/clients/radarr/endpoints/LookupEndpoint.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { Endpoint, type HttpMethod } from '../../../../core/http/Endpoint';
|
||||
|
||||
export interface RadarrMovie {
|
||||
title: string;
|
||||
tmdbId: number;
|
||||
qualityProfileId: number;
|
||||
rootFolderPath: string;
|
||||
monitored: boolean;
|
||||
addOptions: { searchForMovie: boolean };
|
||||
}
|
||||
|
||||
interface RadarrLookupResult {
|
||||
title: string;
|
||||
tmdbId: number;
|
||||
}
|
||||
|
||||
export interface LookupParams {
|
||||
tmdbId: number;
|
||||
}
|
||||
|
||||
export class LookupEndpoint extends Endpoint<RadarrLookupResult, LookupParams> {
|
||||
readonly method: HttpMethod = 'GET';
|
||||
path(): string {
|
||||
return '/api/v3/movie/lookup/tmdb';
|
||||
}
|
||||
query(params: LookupParams): Record<string, string> {
|
||||
return { tmdbId: String(params.tmdbId) };
|
||||
}
|
||||
parseResponse(json: unknown): RadarrLookupResult {
|
||||
return json as RadarrLookupResult;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user