@@ -19,7 +19,7 @@ const topcoderM2M = m2mAuth({ ...topcoderM2MConfig, AUTH0_AUDIENCE: topcoderM2MC
19
19
* js version of sleep()
20
20
* @param {Number } ms Timeout in ms
21
21
*/
22
- async function sleep ( ms ) {
22
+ async function sleep ( ms ) {
23
23
if ( ! ms ) {
24
24
ms = config . SLEEP_TIME
25
25
}
@@ -33,33 +33,63 @@ async function sleep (ms) {
33
33
* (U-Bahn APIs only)
34
34
* @returns {Promise }
35
35
*/
36
- async function getUbahnM2Mtoken ( ) {
36
+ async function getUbahnM2Mtoken ( ) {
37
37
return ubahnM2M . getMachineToken ( config . AUTH0_CLIENT_ID , config . AUTH0_CLIENT_SECRET )
38
38
}
39
39
40
40
/* Function to get M2M token
41
41
* (Topcoder APIs only)
42
42
* @returns {Promise }
43
43
*/
44
- async function getTopcoderM2Mtoken ( ) {
44
+ async function getTopcoderM2Mtoken ( ) {
45
45
return topcoderM2M . getMachineToken ( config . AUTH0_CLIENT_ID , config . AUTH0_CLIENT_SECRET )
46
46
}
47
47
48
48
/**
49
49
* Get Kafka options
50
50
* @return {Object } the Kafka options
51
51
*/
52
- function getKafkaOptions ( ) {
52
+ function getKafkaOptions ( ) {
53
53
const options = { connectionString : config . KAFKA_URL , groupId : config . KAFKA_GROUP_ID }
54
54
if ( config . KAFKA_CLIENT_CERT && config . KAFKA_CLIENT_CERT_KEY ) {
55
55
options . ssl = { cert : config . KAFKA_CLIENT_CERT , key : config . KAFKA_CLIENT_CERT_KEY }
56
56
}
57
57
return options
58
58
}
59
59
60
+ /**
61
+ * Returns the member details
62
+ *
63
+ * @param {String } handle The member handle
64
+ */
65
+ async function getMember ( handle , token ) {
66
+ const res = await axios . get ( `${ config . MEMBERS_API_URL } /${ qs . escape ( handle ) } ` , { headers : { Authorization : `Bearer ${ token } ` } } )
67
+ return _ . get ( res , 'data' , { } )
68
+ }
69
+
70
+ /**
71
+ * Send Kafka event message
72
+ * @params {String} topic the topic name
73
+ * @params {Object} payload the payload
74
+ */
75
+ async function postEvent ( topic , payload , token ) {
76
+ logger . debug ( `Posting event to Kafka topic ${ topic } , ${ JSON . stringify ( payload , null , 2 ) } ` )
77
+ const message = {
78
+ topic,
79
+ originator : config . KAFKA_MESSAGE_ORIGINATOR ,
80
+ timestamp : new Date ( ) . toISOString ( ) ,
81
+ 'mime-type' : 'application/json' ,
82
+ payload
83
+ }
84
+
85
+ await axios . post ( `${ config . V5_API_URL } /bus/events` , message , { headers : { Authorization : `Bearer ${ token } ` } } )
86
+ logger . debug ( `Posted event to Kafka topic` )
87
+ }
60
88
61
89
module . exports = {
62
90
getKafkaOptions,
63
91
getTopcoderM2Mtoken,
64
- getUbahnM2Mtoken
92
+ getUbahnM2Mtoken,
93
+ getMember,
94
+ postEvent
65
95
}
0 commit comments