From 59a981c9f792626e2538300d776f330233ad2f70 Mon Sep 17 00:00:00 2001 From: sloppyjosh Date: Fri, 23 Feb 2024 01:21:27 -0500 Subject: [PATCH] addtl commenting --- src/rdrama/services/CommentFetcher.ts | 29 ++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/rdrama/services/CommentFetcher.ts b/src/rdrama/services/CommentFetcher.ts index 6321e0d..844c97f 100644 --- a/src/rdrama/services/CommentFetcher.ts +++ b/src/rdrama/services/CommentFetcher.ts @@ -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} 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 { 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; } } +