tc-agent / static /js /modules /api-client.js
togitoon's picture
Initial
bf5f290
/**
* API Client Module
* Handles all API communications with the backend
*/
class APIClient {
constructor() {
this.baseURL = '';
}
/**
* Fetch challenges based on user preferences (dry run)
* @param {string} preferences - User preferences
* @returns {Promise<Object>} Challenge data
*/
async fetchChallenges(preferences) {
const response = await fetch('/api/topcoder-dry-run', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
preferences: preferences || ''
})
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
if (data.error) {
throw new Error(data.error);
}
return data;
}
/**
* Register or update user preferences
* @param {string} email - User email
* @param {string} preferences - User preferences
* @returns {Promise<Object>} Registration result
*/
async registerUser(email, preferences) {
const response = await fetch('/api/register-user', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: email,
preferences: preferences || ''
})
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || `HTTP error! status: ${response.status}`);
}
return await response.json();
}
/**
* Update existing user preferences
* @param {string} email - User email
* @param {string} preferences - Updated preferences
* @returns {Promise<Object>} Update result
*/
async updateUserPreferences(email, preferences) {
const response = await fetch('/api/update-preferences', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: email,
preferences: preferences || ''
})
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || `HTTP error! status: ${response.status}`);
}
return await response.json();
}
/**
* Toggle user active status
* @param {string} email - User email
* @param {boolean} active - Active status
* @returns {Promise<Object>} Toggle result
*/
async toggleUserActive(email, active) {
const response = await fetch('/api/toggle-user', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: email,
active: active
})
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || `HTTP error! status: ${response.status}`);
}
return await response.json();
}
/**
* Get user by email
* @param {string} email - User email
* @returns {Promise<Object>} User data
*/
async getUserByEmail(email) {
const response = await fetch(`/api/user/${encodeURIComponent(email)}`);
if (!response.ok) {
if (response.status === 404) {
return null; // User not found
}
const errorData = await response.json();
throw new Error(errorData.detail || `HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data.success ? data.user : null;
}
/**
* Trigger daily notifications for testing
* @returns {Promise<Object>} Trigger result
*/
async triggerDailyNotifications() {
const response = await fetch('/api/trigger-daily-notifications', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || `HTTP error! status: ${response.status}`);
}
return await response.json();
}
}
// Create and export a singleton instance
const apiClient = new APIClient();