Skip to content

Commit 296b05b

Browse files
committed
A little extra logging
1 parent 03e1f1a commit 296b05b

File tree

1 file changed

+44
-40
lines changed

1 file changed

+44
-40
lines changed

src/utils/topcoder-api.util.js

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
1-
const config = require('config')
2-
const _ = require('lodash')
3-
const m2mAuth = require('tc-core-library-js').auth.m2m
4-
const request = require('superagent')
1+
const config = require("config");
2+
const _ = require("lodash");
3+
const m2mAuth = require("tc-core-library-js").auth.m2m;
4+
const request = require("superagent");
5+
const logger = require("logger.util");
56

6-
let m2m = null
7+
let m2m = null;
78

89
/**
910
* Function to get M2M token
1011
*/
11-
async function getM2MToken () {
12+
async function getM2MToken() {
1213
if (_.isNull(m2m)) {
1314
m2m = m2mAuth(
1415
_.pick(config.TOPCODER, [
15-
'AUTH0_URL',
16-
'AUTH0_AUDIENCE',
17-
'TOKEN_CACHE_TIME',
18-
'AUTH0_PROXY_SERVER_URL'
16+
"AUTH0_URL",
17+
"AUTH0_AUDIENCE",
18+
"TOKEN_CACHE_TIME",
19+
"AUTH0_PROXY_SERVER_URL",
1920
])
20-
)
21+
);
2122
}
23+
logger.info(
24+
`Getting M2M token for client ID=${config.TOPCODER.AUTH0_CLIENT_ID}`
25+
);
26+
2227
return m2m.getMachineToken(
2328
config.TOPCODER.AUTH0_CLIENT_ID,
2429
config.TOPCODER.AUTH0_CLIENT_SECRET
25-
)
30+
);
2631
}
2732

2833
/**
@@ -31,80 +36,79 @@ async function getM2MToken () {
3136
* @param {String} path Complete path of the API URL
3237
* @param {Object} reqBody Body of the request
3338
*/
34-
async function reqToAPI (reqType, path, reqBody) {
35-
const token = await getM2MToken()
36-
const authHeader = token ? { Authorization: `Bearer ${token}` } : {}
39+
async function reqToAPI(reqType, path, reqBody) {
40+
const token = await getM2MToken();
41+
const authHeader = token ? { Authorization: `Bearer ${token}` } : {};
3742

38-
const validReqTypes = ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE']
39-
const hasBody = ['POST', 'PUT', 'PATCH']
43+
const validReqTypes = ["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE"];
44+
const hasBody = ["POST", "PUT", "PATCH"];
4045

4146
if (_.indexOf(validReqTypes, _.upperCase(reqType)) === -1) {
42-
throw new Error('Invalid request type')
47+
throw new Error("Invalid request type");
4348
}
44-
const reqMethod = request[_.lowerCase(reqType)]
49+
const reqMethod = request[_.lowerCase(reqType)];
4550

4651
if (_.indexOf(hasBody, _.upperCase(reqType)) === -1) {
4752
return reqMethod(path)
4853
.set(authHeader)
49-
.set('Content-Type', 'application/json')
54+
.set("Content-Type", "application/json");
5055
} else {
5156
return reqMethod(path)
5257
.set(authHeader)
53-
.set('Content-Type', 'application/json')
54-
.send(reqBody)
58+
.set("Content-Type", "application/json")
59+
.send(reqBody);
5560
}
5661
}
5762

5863
/**
5964
* Gets the user's handle given the user's ID
6065
* @param {String} userId User's ID (6-digit numeric)
6166
*/
62-
async function getUserDetailsById (userId) {
63-
const path = `${config.TOPCODER.API_URL}/v3/users?filter=id%3D${userId}`
64-
return reqToAPI('GET', path)
67+
async function getUserDetailsById(userId) {
68+
const path = `${config.TOPCODER.API_URL}/v3/users?filter=id%3D${userId}`;
69+
return reqToAPI("GET", path);
6570
}
6671

6772
/**
6873
* Gets the user by Topcoder's handle
6974
* @param {String} handle
7075
*/
71-
async function getUserDetailsByHandle (handle) {
72-
const path = `${config.TOPCODER.API_URL}/v3/users?filter=handle%3D${handle}`
73-
return reqToAPI('GET', path)
76+
async function getUserDetailsByHandle(handle) {
77+
const path = `${config.TOPCODER.API_URL}/v3/users?filter=handle%3D${handle}`;
78+
return reqToAPI("GET", path);
7479
}
7580

7681
/**
7782
* Gets the challenge
7883
* @param {String} challengeId Challenge's ID (uuid)
7984
*/
80-
async function getChallenge (challengeId) {
81-
const path = `${config.TOPCODER.API_URL}/v5/challenges/${challengeId}`
82-
return reqToAPI('GET', path)
85+
async function getChallenge(challengeId) {
86+
const path = `${config.TOPCODER.API_URL}/v5/challenges/${challengeId}`;
87+
return reqToAPI("GET", path);
8388
}
8489

8590
/**
8691
* Update the challenge
8792
* @param {String} challengeId Challenge's ID (uuid)
8893
*/
89-
async function updateChallenge (challengeId, data) {
90-
const path = `${config.TOPCODER.API_URL}/v5/challenges/${challengeId}`
91-
return reqToAPI('PATCH', path, data)
94+
async function updateChallenge(challengeId, data) {
95+
const path = `${config.TOPCODER.API_URL}/v5/challenges/${challengeId}`;
96+
return reqToAPI("PATCH", path, data);
9297
}
9398

9499
/**
95100
* Gets the roles for an user
96101
* @param {int} userId User's ID
97102
*/
98-
async function getRoles (userId) {
99-
const path = `${config.TOPCODER.API_URL}/v3/roles?filter=subjectID%3D${userId}`
100-
return reqToAPI('GET', path)
103+
async function getRoles(userId) {
104+
const path = `${config.TOPCODER.API_URL}/v3/roles?filter=subjectID%3D${userId}`;
105+
return reqToAPI("GET", path);
101106
}
102107

103-
104108
module.exports = {
105109
getUserDetailsById,
106110
getUserDetailsByHandle,
107111
getChallenge,
108112
updateChallenge,
109-
getRoles
110-
}
113+
getRoles,
114+
};

0 commit comments

Comments
 (0)