Class: ApiClient
- Inherits:
-
Object
- Object
- ApiClient
- Defined in:
- lib/holistics/api_client.rb
Constant Summary collapse
- SERVER_URL =
'https://secure.holistics.io/'
Instance Method Summary collapse
- #api_url_for(path, token = nil) ⇒ Object
- #authenticated? ⇒ Boolean
- #build_submit_params(dest_ds_type, from_ds_type, options) ⇒ Object
- #ds_list ⇒ Object
- #err_and_exit(message, response) ⇒ Object
- #fetch_job_status(job_id) ⇒ Object
- #get_gconfig_filepath ⇒ Object
- #get_token_from_gconfig ⇒ Object
- #has_more?(status) ⇒ Boolean
- #holistics_authenticate(token) ⇒ Object
- #job_show(options) ⇒ Object
- #login(token) ⇒ Object
- #parse_transport_config(filepath) ⇒ Object
- #print_log(log) ⇒ Object
- #response_has_error?(response) ⇒ Boolean
- #send_transport(from_ds_type, dest_ds_type, options) ⇒ Object
- #server_url ⇒ Object
- #submit_transport_job(params) ⇒ Object
- #tail_job(job_id) ⇒ Object
- #write_token_to_gconfig(token) ⇒ Object
Instance Method Details
#api_url_for(path, token = nil) ⇒ Object
145 146 147 |
# File 'lib/holistics/api_client.rb', line 145 def api_url_for(path, token = nil) "#{server_url}#{path}?_utoken=#{token || get_token_from_gconfig}" end |
#authenticated? ⇒ Boolean
128 129 130 |
# File 'lib/holistics/api_client.rb', line 128 def authenticated? File.exists?(get_gconfig_filepath) end |
#build_submit_params(dest_ds_type, from_ds_type, options) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/holistics/api_client.rb', line 98 def build_submit_params(dest_ds_type, from_ds_type, ) params = .except(:config_path).merge(from_ds_type: from_ds_type, dest_ds_type: dest_ds_type, _utoken: get_token_from_gconfig) configs = {} if [:config_path] config_filepath = File.join(ENV['ROOT_PATH'], [:config_path]) configs = parse_transport_config(config_filepath) end configs['from_table_name'] = [:table_name] if [:table_name] configs['dest_table_name'] = [:rename] if [:rename] params[:configs] = configs.to_json # should be a string params end |
#ds_list ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/holistics/api_client.rb', line 35 def ds_list url = api_url_for('data_sources.json') response = HTTParty.get(url) err_and_exit("Error retrieving list of data sources", response) if response_has_error?(response) parsed = JSON.parse(response.body) table = [%w(ID Type Name)] rows = parsed.map { |record| [record['id'], record['dbtype'], record['name']] } table.concat(rows) puts TabularFormatter.new(table).to_pretty_table end |
#err_and_exit(message, response) ⇒ Object
153 154 155 156 157 158 |
# File 'lib/holistics/api_client.rb', line 153 def err_and_exit(, response) STDERR.puts STDERR.puts "Error Response Code: #{response.code}" STDERR.puts response.body exit 1 end |
#fetch_job_status(job_id) ⇒ Object
80 81 82 |
# File 'lib/holistics/api_client.rb', line 80 def fetch_job_status(job_id) HTTParty.get(api_url_for("jobs/#{job_id}.json")) end |
#get_gconfig_filepath ⇒ Object
141 142 143 |
# File 'lib/holistics/api_client.rb', line 141 def get_gconfig_filepath File.('~/.holistics.yml', __FILE__) end |
#get_token_from_gconfig ⇒ Object
132 133 134 135 136 137 138 139 |
# File 'lib/holistics/api_client.rb', line 132 def get_token_from_gconfig if authenticated? string = YAML.load_file(get_gconfig_filepath) string['token'] else raise StandardError.new 'Holistics config file not found' end end |
#has_more?(status) ⇒ Boolean
84 85 86 |
# File 'lib/holistics/api_client.rb', line 84 def has_more?(status) %w(running created).include?(status) end |
#holistics_authenticate(token) ⇒ Object
24 25 26 27 28 |
# File 'lib/holistics/api_client.rb', line 24 def holistics_authenticate(token) url = api_url_for('users/info.json', token) response = HTTParty.get(url) return response, (response.code == 200) end |
#job_show(options) ⇒ Object
30 31 32 33 |
# File 'lib/holistics/api_client.rb', line 30 def job_show job_id = [:job_id] tail_job(job_id) end |
#login(token) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/holistics/api_client.rb', line 9 def login token puts 'Logging in...' response, ok = holistics_authenticate(token) if ok parsed = JSON.parse(response.body) puts 'Authentication successful. Info:' puts "- ID: #{parsed['id']}" puts "- Email: #{parsed['email']}" write_token_to_gconfig(token) else puts 'Error logging in. Please check your token again.' end end |
#parse_transport_config(filepath) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/holistics/api_client.rb', line 114 def parse_transport_config(filepath) file_ext = File.extname(filepath).downcase if file_ext == '.json' return JSON.parse(File.read(filepath)) elsif file_ext == '.yml' return YAML.load(File.read(filepath)) else raise StandardError.new 'Invalid config file extension. Please use either JSON or YML' end rescue StandardError => e STDERR.puts "Error parsing transport config file: #{e.message}" exit 1 end |
#print_log(log) ⇒ Object
149 150 151 |
# File 'lib/holistics/api_client.rb', line 149 def print_log log puts "#{log['created_at'][0..18]} - #{log['level']} - #{log['message']}" end |
#response_has_error?(response) ⇒ Boolean
160 161 162 |
# File 'lib/holistics/api_client.rb', line 160 def response_has_error?(response) response.code != 200 end |
#send_transport(from_ds_type, dest_ds_type, options) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/holistics/api_client.rb', line 49 def send_transport(from_ds_type, dest_ds_type, ) puts 'Submitting transport job ...' params = build_submit_params(dest_ds_type, from_ds_type, ) response = submit_transport_job(params) err_and_exit("Error submitting transport job", response) if response_has_error?(response) parsed = JSON.parse(response.body) job_id = parsed['job_id'] puts "Job submitted. Job ID: #{job_id}." tail_job(job_id) end |
#server_url ⇒ Object
92 93 94 95 96 |
# File 'lib/holistics/api_client.rb', line 92 def server_url host = (ENV['HOST'] || SERVER_URL).dup host += '/' if host[-1] != '/' host end |
#submit_transport_job(params) ⇒ Object
88 89 90 |
# File 'lib/holistics/api_client.rb', line 88 def submit_transport_job(params) HTTParty.post(server_url + 'transports/submit.json', body: params.to_json, headers: {'Content-Type' => 'application/json'}) end |
#tail_job(job_id) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/holistics/api_client.rb', line 63 def tail_job(job_id) last_ts = '' while true response = fetch_job_status(job_id) parsed = JSON.parse(response.body) logs = parsed['job_logs'] select_logs = logs.select { |log| log['created_at'] > last_ts } select_logs.each do |log| print_log(log) end last_ts = logs.last['created_at'] if logs.size > 0 break unless has_more?(parsed['status']) sleep 1 end end |
#write_token_to_gconfig(token) ⇒ Object
164 165 166 167 168 |
# File 'lib/holistics/api_client.rb', line 164 def write_token_to_gconfig(token) file_path = File.join(ENV['HOME'], '.holistics.yml') h = {'token' => token} File.write(file_path, h.to_yaml) end |