Passed
Push — master ( 46d449...c17029 )
by Muhammad Dyas
01:28
created

api.js ➔ callMessageApi   A

Complexity

Conditions 4

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
dl 0
loc 14
rs 9.8
c 0
b 0
f 0
1
const {google} = require('googleapis');
2
3
/**
4
 * Create google api credentials
5
 *
6
 * @returns {object} google.chat
7
 */
8
function gAuth() {
9
  // Use default credentials (service account)
10
  const credentials = new google.auth.GoogleAuth({
11
    scopes: ['https://www.googleapis.com/auth/chat.bot'],
12
  });
13
  return google.chat({
14
    version: 'v1',
15
    auth: credentials,
16
  });
17
}
18
19
/**
20
 * Call Google API using default credentials (service account)
21
 *
22
 * @param {string} action - request action(create,update,get,delete)
23
 * @param {object} request - request body
24
 * @returns {object} Response from google api
25
 */
26
async function callMessageApi(action, request) {
27
  const chatApi = gAuth();
28
  console.log('gapi request', JSON.stringify(request));
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
29
  let response;
30
  if (action === 'create') {
31
    response = await chatApi.spaces.messages.create(request);
32
  } else if (action === 'update') {
33
    response = await chatApi.spaces.messages.update(request);
34
  } else if (action === 'get') {
35
    response = await chatApi.spaces.messages.get(request);
36
  }
37
  console.log('gapi response', JSON.stringify(response));
0 ignored issues
show
Bug introduced by
The variable response does not seem to be initialized in case action === "get" on line 34 is false. Are you sure the function stringify handles undefined variables?
Loading history...
38
  return response;
39
}
40
41
exports.callMessageApi = callMessageApi;
42