Skip to content

Instantly share code, notes, and snippets.

@khanhkhuu
Created March 29, 2023 10:07
Show Gist options
  • Save khanhkhuu/70553283cea1c2ba5ad61321e1927736 to your computer and use it in GitHub Desktop.
Save khanhkhuu/70553283cea1c2ba5ad61321e1927736 to your computer and use it in GitHub Desktop.
Domo.js
class Domo {
constructor(client_id, client_secret) {
this.client_id = client_id;
this.client_secret = client_secret;
}
__get_oauth_token() {
const token = Utilities.base64Encode(`${this.client_id}:${this.client_secret}`);
const headers = { 'Authorization' : 'Basic ' + token };
const res = UrlFetchApp.fetch("https://api.domo.com/oauth/token?grant_type=client_credentials&scope=data", {
headers
}).getContentText();
const data = JSON.parse(res);
return data['access_token'];
}
query(data_set_id, query_str = 'SELECT * FROM table') {
const access_token = this.__get_oauth_token();
const res = UrlFetchApp.fetch("https://api.domo.com/v1/datasets/query/execute/" + data_set_id, {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + access_token,
'Content-Type': 'application/json'
},
payload: JSON.stringify({"sql": query_str})
}).getContentText();
const data = JSON.parse(res);
const col_headers = data['columns']
const data_rows = data['rows']
return [col_headers].concat(data_rows);
}
update(data_set_id, json_data, append = false) {
const access_token = this.__get_oauth_token();
const url = "https://api.domo.com/v1/json/" + data_set_id + "/data" + (append ? '?updateMethod=APPEND': '');
const res = UrlFetchApp.fetch(url, {
method: 'PUT',
headers: {
'Authorization': 'Bearer ' + access_token,
'Content-Type': 'application/json'
},
}).getContentText();
const json = JSON.parse(res);
return json;
}
append(data_set_id, json_data) {
return this.update(data_set_id, json_data, true)
}
}
function test() {
const domo = new Domo(
'c5e26b16-f176-4207-8ed6-1afb593ce480',
'58c84eed3c971383a23f4d85ef727adaa1d0518e8b66929852004e19de412636'
);
const result = domo.query('9822feb9-d034-46d6-af5e-695a674fadf1');
console.log(result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment