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
- #config_filepath ⇒ Object
- #ds_list ⇒ Object
- #fetch_job_status(job_id) ⇒ Object
- #get_key ⇒ Object
- #has_more?(status) ⇒ Boolean
- #job_show(options) ⇒ Object
- #login(token) ⇒ Object
- #print_log(log) ⇒ Object
- #send_transport(from_ds_type, dest_ds_type, options) ⇒ Object
- #server_url ⇒ Object
- #submit_transport_job(params) ⇒ Object
- #tail_job(job_id) ⇒ Object
Instance Method Details
#api_url_for(path, token = nil) ⇒ Object
132 133 134 |
# File 'lib/holistics/api_client.rb', line 132 def api_url_for(path, token = nil) "#{server_url}#{path}?_utoken=#{token || get_key}" end |
#authenticated? ⇒ Boolean
120 121 122 |
# File 'lib/holistics/api_client.rb', line 120 def authenticated? File.exists?(config_filepath) end |
#build_submit_params(dest_ds_type, from_ds_type, options) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/holistics/api_client.rb', line 104 def build_submit_params(dest_ds_type, from_ds_type, ) params = .merge(from_ds_type: from_ds_type, dest_ds_type: dest_ds_type, _utoken: get_key) configs = {} if [:config_path] config_content = File.read(File.join(ENV['ROOT_PATH'], [:config_path])) configs = JSON.parse(config_content) end configs[:from_table_name] = [:table_name] if [:table_name] configs[:dest_table_name] = [:rename] if [:rename] params[:config] = configs.to_json # should be a string params end |
#config_filepath ⇒ Object
128 129 130 |
# File 'lib/holistics/api_client.rb', line 128 def config_filepath File.('~/.holistics.yml', __FILE__) end |
#ds_list ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/holistics/api_client.rb', line 32 def ds_list url = api_url_for('data_sources.json') response = HTTParty.get(url) if response.code != 200 puts "Error retrieving list of data sources. Code: #{response.code}" puts response.body exit 1 end 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 |
#fetch_job_status(job_id) ⇒ Object
86 87 88 |
# File 'lib/holistics/api_client.rb', line 86 def fetch_job_status(job_id) HTTParty.get(api_url_for("jobs/#{job_id}.json")) end |
#get_key ⇒ Object
124 125 126 |
# File 'lib/holistics/api_client.rb', line 124 def get_key File.read(config_filepath).strip end |
#has_more?(status) ⇒ Boolean
90 91 92 |
# File 'lib/holistics/api_client.rb', line 90 def has_more?(status) %w(running created).include?(status) end |
#job_show(options) ⇒ Object
27 28 29 30 |
# File 'lib/holistics/api_client.rb', line 27 def job_show job_id = [:job_id] tail_job(job_id) end |
#login(token) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/holistics/api_client.rb', line 7 def login token puts 'Authenticating token...' url = api_url_for('users/info.json', token) response = HTTParty.get(url) if response.code != 200 puts 'Error authenticating. Please check your token again.' exit 1 end parsed = JSON.parse(response.body) puts 'Authentication successful. Info:' puts "- ID: #{parsed['id']}" puts "- Email: #{parsed['email']}" file_path = File.join(ENV['HOME'], '.holistics.yml') File.write(file_path, token) end |
#print_log(log) ⇒ Object
136 137 138 |
# File 'lib/holistics/api_client.rb', line 136 def print_log log puts "#{log['created_at'][0..18]} - #{log['level']} - #{log['message']}" end |
#send_transport(from_ds_type, dest_ds_type, options) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/holistics/api_client.rb', line 51 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) if response.code != 200 puts "Error submitting transport job. Code: #{response.code}" puts response.body exit 1 end 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
98 99 100 101 102 |
# File 'lib/holistics/api_client.rb', line 98 def server_url host = (ENV['HOST'] || SERVER_URL).dup host += '/' if host[-1] != '/' host end |
#submit_transport_job(params) ⇒ Object
94 95 96 |
# File 'lib/holistics/api_client.rb', line 94 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
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/holistics/api_client.rb', line 69 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 |