Comment API Interaction Logic

pull/1/head
sloppyjosh 2024-02-23 01:17:31 -05:00
parent 9d536f48e9
commit 31b25229cd
1 changed files with 32 additions and 0 deletions

View File

@ -0,0 +1,32 @@
import { AxiosInstance } from 'axios';
import SessionManager from '../session/SessionManager';
import { Comment } from '../models/Comment';
export class CommentFetcher {
private axiosInstance: AxiosInstance;
private maxPages: number;
constructor(maxPages: number = 10) {
this.axiosInstance = SessionManager.getInstance().axiosInstance;
this.maxPages = maxPages;
}
async fetchComments(): Promise<Comment[]> {
let comments: Comment[] = [];
for (let page = 1; page <= this.maxPages; page++) {
console.time(`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
if (response.data.data.length === 0) break;
} catch (error) {
console.error(`Failed to fetch comments for page ${page}:`, error);
console.timeEnd(`page: ${page}`)
break;
}
}
return comments;
}
}