Skip to content

Instantly share code, notes, and snippets.

@gnilrets
Last active February 12, 2018 12:04
Show Gist options
  • Save gnilrets/b9d2ce9890d8aaa07042 to your computer and use it in GitHub Desktop.
Save gnilrets/b9d2ce9890d8aaa07042 to your computer and use it in GitHub Desktop.
Download a GoodData report as CSV using REST API and Ruby
require 'rubygems'
require 'bundler/setup'
require 'rest_client'
require 'yaml'
require 'json'
# Working example of using the GoodData REST API to download a report.
# This demonstrates two different methods, one is to download the
# the report as computed on the UI, the other is to download the
# RAW report (needed to download reports that are too large
# to display).
# Need to set GD_LOGIN and GD_PWD environment variables
class GoodDataRest
def initialize(gd_pid: nil, report_name: nil, outfile_name: nil)
@url_base = 'https://secure.gooddata.com'
@gd_pid = gd_pid
@report_name = report_name
@outfile_name = outfile_name
@gd_login = ENV['GD_LOGIN']
@gd_pwd = ENV['GD_PWD']
end
def ping
headers = {
:accept => 'application/json'
}
response = RestClient.get "#{@url_base}/gdc/ping", headers
end
# POST /gdc/account/login
def post_gdc_account_login
values = <<-EOF
{
"postUserLogin": {
"login": "#{@gd_login}",
"password": "#{@gd_pwd}",
"remember": 1
}
}
EOF
headers = {
:content_type => 'application/json',
:accept => 'application/json'
}
response = RestClient.post "#{@url_base}/gdc/account/login", values, headers
@login_response = response
end
# GET /gdc/account/token
def get_gdc_account_token
headers = {
:accept => 'application/json',
:cookie => @login_response.headers[:set_cookie].join(',')
}
response = RestClient.get "#{@url_base}/gdc/account/token", headers
@token_response = response
end
# GET gdc/md/{project-id}/query/reports
def get_gdc_md_pid_query_reports
headers = {
:accept => 'application/json',
:cookie => @token_response.headers[:set_cookie].join(',')
}
response = RestClient.get "#{@url_base}/gdc/md/#{@gd_pid}/query/reports", headers
@my_report = JSON.parse(response)['query']['entries'].select { |report| report['title'] == @report_name }
end
# POST gdc/xtab2/executor3
def post_gdc_xtab2_executor3
values = <<-EOF
{
"report_req": {
"report": "#{@my_report[0]['link']}"
}
}
EOF
headers = {
:content_type => 'application/json',
:accept => 'application/json',
:cookie => @token_response.headers[:set_cookie].join(',')
}
response = RestClient.post "#{@url_base}/gdc/xtab2/executor3", values, headers
@exec_result = JSON.parse(response)
end
# POST gdc_app_projects_pid_execute_raw
def post_gdc_app_projects_pid_execute_raw
values = <<-EOF
{
"report_req": {
"report": "#{@my_report[0]['link']}"
}
}
EOF
headers = {
:content_type => 'application/json',
:accept => 'application/json',
:cookie => @token_response.headers[:set_cookie].join(',')
}
response = RestClient.post "#{@url_base}/gdc/app/projects/#{@gd_pid}/execute/raw", values, headers
@download_uri = JSON.parse(response)['uri']
end
# POST /gdc/exporter/executor
def post_gdc_exporter_executor
values = <<-EOF
{
"result_req": {
"format": "csv",
"result": #{@exec_result.to_json}
}
}
EOF
headers = {
:content_type => 'application/json',
:accept => 'application/json',
:cookie => @token_response.headers[:set_cookie].join(',')
}
response = RestClient.post "#{@url_base}/gdc/exporter/executor", values, headers
@download_uri = JSON.parse(response)['uri']
end
def download_file
headers = {
:cookie => @token_response.headers[:set_cookie].join(',')
}
file = File.open(@outfile_name, 'wb' ) do |output|
output.write RestClient.get "#{@url_base}#{@download_uri}", headers
end
end
end
# Downloading a small report using the UI
conn = GoodDataRest.new(gd_pid: 'project id',
report_name: 'Dev - Depletions by yrmo',
outfile_name: '/tmp/dev_depletions.csv'
)
conn.post_gdc_account_login
conn.get_gdc_account_token
conn.get_gdc_md_pid_query_reports
conn.post_gdc_xtab2_executor3
conn.post_gdc_exporter_executor
conn.download_file
# Downloading a large report using the RAW method
conn = GoodDataRest.new(gd_pid: 'project id',
report_name: 'Dev - RAW RAD',
outfile_name: '/tmp/dev_raw_rad.csv'
)
conn.post_gdc_account_login
conn.get_gdc_account_token
conn.get_gdc_md_pid_query_reports
conn.post_gdc_app_projects_pid_execute_raw
conn.download_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment