import cacheManager, { AvailableCacheIds } from '../../lib/cache'; import { DVRSettings } from '../../lib/settings'; import ExternalAPI from '../externalapi'; export interface RootFolder { id: number; path: string; freeSpace: number; totalSpace: number; unmappedFolders: { name: string; path: string; }[]; } export interface QualityProfile { id: number; name: string; } interface QueueItem { size: number; title: string; sizeleft: number; timeleft: string; estimatedCompletionTime: string; status: string; trackedDownloadStatus: string; trackedDownloadState: string; downloadId: string; protocol: string; downloadClient: string; indexer: string; id: number; } export interface Tag { id: number; label: string; } interface QueueResponse { page: number; pageSize: number; sortKey: string; sortDirection: string; totalRecords: number; records: (QueueItem & QueueItemAppendT)[]; } class ServarrBase extends ExternalAPI { static buildUrl(settings: DVRSettings, path?: string): string { return `${settings.useSsl ? 'https' : 'http'}://${settings.hostname}:${ settings.port }${settings.baseUrl ?? ''}${path}`; } protected apiName: string; constructor({ url, apiKey, cacheName, apiName, }: { url: string; apiKey: string; cacheName: AvailableCacheIds; apiName: string; }) { super( url, { apikey: apiKey, }, { nodeCache: cacheManager.getCache(cacheName).data, } ); this.apiName = apiName; } public getProfiles = async (): Promise => { try { const data = await this.getRolling( `/qualityProfile`, undefined, 3600 ); return data; } catch (e) { throw new Error( `[${this.apiName}] Failed to retrieve profiles: ${e.message}` ); } }; public getRootFolders = async (): Promise => { try { const data = await this.getRolling( `/rootfolder`, undefined, 3600 ); return data; } catch (e) { throw new Error( `[${this.apiName}] Failed to retrieve root folders: ${e.message}` ); } }; public getQueue = async (): Promise<(QueueItem & QueueItemAppendT)[]> => { try { const response = await this.axios.get>( `/queue` ); return response.data.records; } catch (e) { throw new Error( `[${this.apiName}] Failed to retrieve queue: ${e.message}` ); } }; public getTags = async (): Promise => { try { const response = await this.axios.get(`/tag`); return response.data; } catch (e) { throw new Error( `[${this.apiName}] Failed to retrieve tags: ${e.message}` ); } }; public createTag = async ({ label }: { label: string }): Promise => { try { const response = await this.axios.post(`/tag`, { label, }); return response.data; } catch (e) { throw new Error(`[${this.apiName}] Failed to create tag: ${e.message}`); } }; protected async runCommand( commandName: string, options: Record ): Promise { try { await this.axios.post(`/command`, { name: commandName, ...options, }); } catch (e) { throw new Error(`[${this.apiName}] Failed to run command: ${e.message}`); } } } export default ServarrBase;