-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathLeetCodeLeaderboard.js
More file actions
72 lines (68 loc) · 1.83 KB
/
LeetCodeLeaderboard.js
File metadata and controls
72 lines (68 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const logger = require('../../util/logger');
const {
OK,
SERVER_ERROR,
} = require('../../util/constants').STATUS_CODES;
const SIGN2_URL = process.env.SIGN2_URL || 'http://192.168.69.180:8080';
async function getAllUsers() {
try {
const url = new URL('/getAllUsers', SIGN2_URL);
const res = await fetch(url.href);
const data = await res.json();
if ('error' in data) {
logger.error('Error from LeetCode Leaderboard: ', data.error);
return null;
}
return data.users;
} catch (err) {
logger.error('getAllUsers encountered an error: ', err);
return null;
}
}
async function addUserToLeaderboard(userData) {
try {
const url = new URL('/user/add', SIGN2_URL);
const res = await fetch(url.href, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(userData),
});
const data = await res.json();
if ('error' in data) {
logger.error('Error from LeetCode Leaderboard: ', data.error);
return data.status_code;
}
return OK;
} catch (err) {
logger.error('addUserToLeaderboard encountered an error: ', err);
return SERVER_ERROR;
}
}
async function deleteUserFromLeaderboard(username) {
try {
const url = new URL('/user/remove', SIGN2_URL);
const res = await fetch(url.href, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username }),
});
const data = await res.json();
if ('error' in data) {
logger.error('Error from LeetCode Leaderboard: ', data.error);
return false;
}
return true;
} catch (err) {
logger.error('deleteUserFromLeaderboard encountered an error: ', err);
return false;
}
}
module.exports = {
getAllUsers,
addUserToLeaderboard,
deleteUserFromLeaderboard,
};