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" ) ;
5
6
6
- let m2m = null
7
+ let m2m = null ;
7
8
8
9
/**
9
10
* Function to get M2M token
10
11
*/
11
- async function getM2MToken ( ) {
12
+ async function getM2MToken ( ) {
12
13
if ( _ . isNull ( m2m ) ) {
13
14
m2m = m2mAuth (
14
15
_ . 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" ,
19
20
] )
20
- )
21
+ ) ;
21
22
}
23
+ logger . info (
24
+ `Getting M2M token for client ID=${ config . TOPCODER . AUTH0_CLIENT_ID } `
25
+ ) ;
26
+
22
27
return m2m . getMachineToken (
23
28
config . TOPCODER . AUTH0_CLIENT_ID ,
24
29
config . TOPCODER . AUTH0_CLIENT_SECRET
25
- )
30
+ ) ;
26
31
}
27
32
28
33
/**
@@ -31,80 +36,79 @@ async function getM2MToken () {
31
36
* @param {String } path Complete path of the API URL
32
37
* @param {Object } reqBody Body of the request
33
38
*/
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 } ` } : { } ;
37
42
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" ] ;
40
45
41
46
if ( _ . indexOf ( validReqTypes , _ . upperCase ( reqType ) ) === - 1 ) {
42
- throw new Error ( ' Invalid request type' )
47
+ throw new Error ( " Invalid request type" ) ;
43
48
}
44
- const reqMethod = request [ _ . lowerCase ( reqType ) ]
49
+ const reqMethod = request [ _ . lowerCase ( reqType ) ] ;
45
50
46
51
if ( _ . indexOf ( hasBody , _ . upperCase ( reqType ) ) === - 1 ) {
47
52
return reqMethod ( path )
48
53
. set ( authHeader )
49
- . set ( ' Content-Type' , ' application/json' )
54
+ . set ( " Content-Type" , " application/json" ) ;
50
55
} else {
51
56
return reqMethod ( path )
52
57
. set ( authHeader )
53
- . set ( ' Content-Type' , ' application/json' )
54
- . send ( reqBody )
58
+ . set ( " Content-Type" , " application/json" )
59
+ . send ( reqBody ) ;
55
60
}
56
61
}
57
62
58
63
/**
59
64
* Gets the user's handle given the user's ID
60
65
* @param {String } userId User's ID (6-digit numeric)
61
66
*/
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 ) ;
65
70
}
66
71
67
72
/**
68
73
* Gets the user by Topcoder's handle
69
74
* @param {String } handle
70
75
*/
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 ) ;
74
79
}
75
80
76
81
/**
77
82
* Gets the challenge
78
83
* @param {String } challengeId Challenge's ID (uuid)
79
84
*/
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 ) ;
83
88
}
84
89
85
90
/**
86
91
* Update the challenge
87
92
* @param {String } challengeId Challenge's ID (uuid)
88
93
*/
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 ) ;
92
97
}
93
98
94
99
/**
95
100
* Gets the roles for an user
96
101
* @param {int } userId User's ID
97
102
*/
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 ) ;
101
106
}
102
107
103
-
104
108
module . exports = {
105
109
getUserDetailsById,
106
110
getUserDetailsByHandle,
107
111
getChallenge,
108
112
updateChallenge,
109
- getRoles
110
- }
113
+ getRoles,
114
+ } ;
0 commit comments