LoginSignup
2
2

More than 3 years have passed since last update.

GASにてredash APIを触ってみた

Last updated at Posted at 2019-09-20

Javascriptを使ってredash API

実行環境はGAS。ライブラリ公開すれば、汎用性が高まるのでおすすめ。
取得したデータをspreadsheetに書き出すと便利。業務で使う場合は、結構おすすめ。

Poll for Fresh Query Results (including parameters)
参考ソース(python)



var httpOptions = {
  method: "",
  headers: {
    Authorization: "",
    Accept: 'application/json',
    'Content-Type': 'application/json'
  }
}

function setApiKey(api_key){
  httpOptions.headers.Authorization = "Key " + api_key
}

// redash refresh query
function getRefreshQuery(redash_url, query_id){
  var refreshUrl = redash_url + '/api/queries/' + query_id + '/refresh'
  httpOptions.method = 'POST'
  var res = UrlFetchApp.fetch(refreshUrl, httpOptions)
  if(res.getResponseCode() !== 200){
    Logger.log('Refresh Failed')
    return 'Refresh Failed'
  }
  return JSON.parse(res)
}

// redash query result
function getQueryResult(redash_url, query_id, result_id){
  var resultUrl = redash_url + '/api/queries/' + query_id + '/results/'+ result_id + '.json'
  httpOptions.method = 'GET'
  var res = UrlFetchApp.fetch(resultUrl, httpOptions)
  var results = JSON.parse(res).query_result.data.rows
  return results
}

// redash check status 
function getQueryJobStatus(redash_url, job_id){
  var jobcheckUrl = redash_url + '/api/jobs/' + job_id
  httpOptions.method = 'GET'
  var res = UrlFetchApp.fetch(jobcheckUrl, httpOptions)
  return JSON.parse(res)
}

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2