DramaHarvester/src/rdrama/services/CommentFetcher.ts

31 lines
1.3 KiB
TypeScript

import SessionManager from '../session/SessionManager';
import { Comment } from '../models/Comment';
/**
* CommentFetcher is responsible for fetching comments from the r/Drama API.
* It utilizes a SessionManager instance to manage API requests with appropriate
* headers and configurations, including rate limiting and retries.
*/
export class CommentFetcher {
/**
* Fetches comments from the r/Drama API across multiple pages.
* Each page's fetch operation is timed for performance analysis.
*
* @returns {Promise<Comment[]>} A promise that resolves to an array of comments fetched from the API.
* @throws {Error} Throws an error if there is a failure in fetching comments from the API.
*/
static async fetchComments(page: number): Promise<Comment[]> {
console.time(`${new Date()} Fetching page: ${page}`);
try {
const axiosInstance = SessionManager.getInstance().axiosInstance;
const response = await axiosInstance.get(`/comments?page=${page}`);
console.timeEnd(`Fetching page: ${page}`);
return response.data.data;
} catch (error) {
console.error(`Failed to fetch comments for page ${page}:`, error);
console.timeEnd(`Fetching page: ${page}`);
throw error; //Rethrow
}
}
}