From af5b35589d0900ad2d74963ba905b1b02fd2834f Mon Sep 17 00:00:00 2001 From: sloppyjosh Date: Mon, 4 Mar 2024 00:28:20 -0500 Subject: [PATCH] reddit service implementation --- src/index.ts | 6 +----- src/reddit/services/reddit.ts | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 src/reddit/services/reddit.ts diff --git a/src/index.ts b/src/index.ts index 454d480..40ec440 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,11 +31,7 @@ async function startApplication() { console.log('Database Maintenance Start') await databaseMaintenance.runMaintenanceTasks() - const redditSessionManager = await redditSession.getInstance(databaseService) - - const response = await redditSessionManager.axiosInstance.get('/me') - - console.log(JSON.stringify(response.data, null, 4)) + await redditSession.getInstance(databaseService) // Initialize services with any required dependencies const commentFetcher = new CommentProcessor(databaseService); diff --git a/src/reddit/services/reddit.ts b/src/reddit/services/reddit.ts new file mode 100644 index 0000000..fddc199 --- /dev/null +++ b/src/reddit/services/reddit.ts @@ -0,0 +1,35 @@ +import axios, { AxiosError } from 'axios'; +import SessionManager from '../session/SessionManager'; + +export class RedditService { + private sessionManager: SessionManager; + + constructor(sessionManager: SessionManager) { + this.sessionManager = sessionManager; + } + + async getUserInfo(username: string): Promise { + try { + const response = await this.sessionManager.axiosInstance.get(`/user/${username}/about`); + return response.data; + } catch (error) { + console.error('Error fetching user info:', error); + throw error; + } + } + + async sendMessage(username: string, subject: string, message: string): Promise { + try { + await this.sessionManager.axiosInstance.post('/api/compose', { + api_type: 'json', + to: username, + subject: subject, + text: message, + }); + console.log(`Message sent to ${username}`); + } catch (error) { + console.error('Error sending message:', error); + throw error; + } + } +}