Total Complexity | 5 |
Complexity/F | 2.5 |
Lines of Code | 41 |
Function Count | 2 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 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)); |
||
|
|||
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)); |
||
38 | return response; |
||
39 | } |
||
40 | |||
41 | exports.callMessageApi = callMessageApi; |
||
42 |