Comment API Interaction Logic
parent
9d536f48e9
commit
31b25229cd
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue