addtl commenting
parent
6f4b05448b
commit
59a981c9f7
|
@ -2,31 +2,50 @@ import { AxiosInstance } from 'axios';
|
|||
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 {
|
||||
private axiosInstance: AxiosInstance;
|
||||
private maxPages: number;
|
||||
|
||||
/**
|
||||
* Creates an instance of CommentFetcher.
|
||||
* @param {number} maxPages The maximum number of pages to fetch from the r/Drama API. Defaults to 10.
|
||||
*/
|
||||
constructor(maxPages: number = 10) {
|
||||
this.axiosInstance = SessionManager.getInstance().axiosInstance;
|
||||
this.maxPages = maxPages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches comments from the r/Drama API across multiple pages, up to the specified maximum.
|
||||
* Iterates through pages starting from the first page until the maximum page limit is reached
|
||||
* or there are no more comments to fetch. 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.
|
||||
*/
|
||||
async fetchComments(): Promise<Comment[]> {
|
||||
let comments: Comment[] = [];
|
||||
for (let page = 1; page <= this.maxPages; page++) {
|
||||
console.time(`page: ${page}`)
|
||||
console.time(`Fetching page: ${page}`);
|
||||
try {
|
||||
const response = await this.axiosInstance.get(`/comments?page=${page}`);
|
||||
comments = [...comments, ...response.data.data];
|
||||
console.timeEnd(`page: ${page}`)
|
||||
// Optional: Break early if the API finds existing comment
|
||||
console.timeEnd(`Fetching page: ${page}`);
|
||||
// Set up logic for DB checks of existing comments
|
||||
// Optional: Break early if the API returns an empty array (no more comments)
|
||||
if (response.data.data.length === 0) break;
|
||||
} catch (error) {
|
||||
console.error(`Failed to fetch comments for page ${page}:`, error);
|
||||
console.timeEnd(`page: ${page}`)
|
||||
break;
|
||||
console.timeEnd(`Fetching page: ${page}`);
|
||||
throw error; // Rethrow or handle as needed
|
||||
}
|
||||
}
|
||||
return comments;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue