All checks were successful
- Remove instanceConfig parameter from all retriever methods (getTags, getQueue, getHistory) - Retriever instances now use this.url, this.apiKey, this.id instead of passed parameter - Convert ArrRetrieverRegistry from class with convenience functions to pure singleton object - Export singleton instance directly instead of class + convenience functions - Update poller.js and historyFetcher.js to call methods on singleton directly - All 261 tests pass with zero behavior changes
97 lines
2.7 KiB
JavaScript
97 lines
2.7 KiB
JavaScript
// Copyright (c) 2026 Gordon Bolton. MIT License.
|
|
const axios = require('axios');
|
|
const ArrRetriever = require('./ArrRetriever');
|
|
const { logToFile } = require('../utils/logger');
|
|
|
|
/**
|
|
* Polling-based Sonarr data retriever.
|
|
* Implements the ArrRetriever interface using direct HTTP polling.
|
|
*/
|
|
class PollingSonarrRetriever extends ArrRetriever {
|
|
constructor(instanceConfig) {
|
|
super(instanceConfig);
|
|
}
|
|
|
|
getRetrieverType() {
|
|
return 'sonarr';
|
|
}
|
|
|
|
/**
|
|
* Get tags from Sonarr instance
|
|
* @returns {Promise<Array>} Array of tag objects
|
|
*/
|
|
async getTags() {
|
|
try {
|
|
const response = await axios.get(`${this.url}/api/v3/tag`, {
|
|
headers: { 'X-Api-Key': this.apiKey }
|
|
});
|
|
return response.data;
|
|
} catch (error) {
|
|
logToFile(`[PollingSonarrRetriever] ${this.id} tags error: ${error.message}`);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get queue from Sonarr instance
|
|
* @returns {Promise<Object>} Queue object with records array
|
|
*/
|
|
async getQueue() {
|
|
try {
|
|
const response = await axios.get(`${this.url}/api/v3/queue`, {
|
|
headers: { 'X-Api-Key': this.apiKey },
|
|
params: { includeSeries: true, includeEpisode: true }
|
|
});
|
|
return response.data;
|
|
} catch (error) {
|
|
logToFile(`[PollingSonarrRetriever] ${this.id} queue error: ${error.message}`);
|
|
return { records: [] };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get history from Sonarr instance
|
|
* @param {Object} options - Optional parameters for history fetch
|
|
* @param {number} [options.pageSize=10] - Number of records to fetch
|
|
* @param {string} [options.sortKey] - Field to sort by
|
|
* @param {string} [options.sortDir] - Sort direction ('ascending' or 'descending')
|
|
* @param {boolean} [options.includeSeries=true] - Include series data
|
|
* @param {boolean} [options.includeEpisode=true] - Include episode data
|
|
* @param {string} [options.startDate] - ISO date string for filtering
|
|
* @returns {Promise<Object>} History object with records array
|
|
*/
|
|
async getHistory(options = {}) {
|
|
const {
|
|
pageSize = 10,
|
|
sortKey,
|
|
sortDir,
|
|
includeSeries = true,
|
|
includeEpisode = true,
|
|
startDate
|
|
} = options;
|
|
|
|
try {
|
|
const params = {
|
|
pageSize,
|
|
includeSeries,
|
|
includeEpisode
|
|
};
|
|
|
|
if (sortKey) params.sortKey = sortKey;
|
|
if (sortDir) params.sortDir = sortDir;
|
|
if (startDate) params.startDate = startDate;
|
|
|
|
const response = await axios.get(`${this.url}/api/v3/history`, {
|
|
headers: { 'X-Api-Key': this.apiKey },
|
|
params
|
|
});
|
|
return response.data;
|
|
} catch (error) {
|
|
logToFile(`[PollingSonarrRetriever] ${this.id} history error: ${error.message}`);
|
|
return { records: [] };
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = PollingSonarrRetriever;
|