class RedashDynamicQuery::Client

Attributes

api_key[R]
endpoint[R]
interval[R]
timeout[R]

Public Class Methods

new(user_apikey:, endpoint:, interval: 0.1, timeout: 10) click to toggle source
# File lib/redash_dynamic_query/client.rb, line 7
def initialize(user_apikey:, endpoint:, interval: 0.1, timeout: 10)
  @api_key  = user_apikey
  @endpoint = endpoint
  @interval = interval
  @timeout  = timeout
end

Public Instance Methods

query(query_id:, params:) click to toggle source
# File lib/redash_dynamic_query/client.rb, line 14
def query(query_id:, params:)
  job_id = post_job_request(query_id: query_id, params: params)

  raise 'job_id not fuond.' if job_id.nil?

  query_result_id = poll_job(job_id: job_id)

  raise 'query_result_id not fuond.' if query_result_id.nil?

  get_query_result(query_result_id: query_result_id)
end

Private Instance Methods

get_query_result(query_result_id:) click to toggle source
# File lib/redash_dynamic_query/client.rb, line 54
def get_query_result(query_result_id:)
  res = with_error_handling do
    Net::HTTP.get(query_result_url(id: query_result_id))
  end

  JSON.parse(res)
end
poll_job(job_id:, count: 0) click to toggle source
# File lib/redash_dynamic_query/client.rb, line 40
def poll_job(job_id:, count: 0)
  res = with_error_handling do
    Net::HTTP.get(poll_job_url(id: job_id))
  end
  id = JSON.parse(res).dig('job', 'query_result_id')

  return id unless id.nil?

  raise "wait timeout with #{timeout} seconds." if reach_limit?(count)

  sleep interval
  poll_job(job_id: job_id, count: count + 1)
end
poll_job_url(id:) click to toggle source
# File lib/redash_dynamic_query/client.rb, line 68
def poll_job_url(id:)
  URI.parse("#{endpoint}/api/jobs/#{id}?api_key=#{api_key}")
end
post_job_request(query_id:, params:) click to toggle source
# File lib/redash_dynamic_query/client.rb, line 28
def post_job_request(query_id:, params:)
  res = with_error_handling do
    Net::HTTP.post_form(
      post_job_url(id: query_id, queries: query_string(params: params)),
      {}
    )
  end

  json = JSON.parse(res.body)
  json.dig('job', 'id')
end
post_job_url(id:, queries:) click to toggle source
# File lib/redash_dynamic_query/client.rb, line 62
def post_job_url(id:, queries:)
  URI.parse(
    "#{endpoint}/api/queries/#{id}/refresh?api_key=#{api_key}&#{queries}"
  )
end
query_result_url(id:) click to toggle source
# File lib/redash_dynamic_query/client.rb, line 72
def query_result_url(id:)
  URI.parse("#{endpoint}/api/query_results/#{id}.json?api_key=#{api_key}")
end
query_string(params:) click to toggle source
# File lib/redash_dynamic_query/client.rb, line 76
def query_string(params:)
  URI.encode_www_form(params.transform_keys { |k| "p_#{k}" })
end
reach_limit?(count) click to toggle source
# File lib/redash_dynamic_query/client.rb, line 80
def reach_limit?(count)
  count * interval > timeout
end
with_error_handling() { || ... } click to toggle source
# File lib/redash_dynamic_query/client.rb, line 84
def with_error_handling
  res = yield
  raise res.body if res.is_a? Net::HTTPClientError
  res
end