Class: ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/holistics/api_client.rb

Constant Summary collapse

SERVER_URL =
'https://secure.holistics.io/'

Instance Method Summary collapse

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

Returns:

  • (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, options)
  params = options.merge(from_ds_type: from_ds_type, dest_ds_type: dest_ds_type, _utoken: get_key)

  configs = {}
  if options[:config_path]
    config_content = File.read(File.join(ENV['ROOT_PATH'], options[:config_path]))
    configs = JSON.parse(config_content)
  end

  configs[:from_table_name] = options[:table_name] if options[:table_name]
  configs[:dest_table_name] = options[:rename] if options[:rename]

  params[:config] = configs.to_json # should be a string
  params
end

#config_filepathObject



128
129
130
# File 'lib/holistics/api_client.rb', line 128

def config_filepath
  File.expand_path('~/.holistics.yml', __FILE__)
end

#ds_listObject



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_keyObject



124
125
126
# File 'lib/holistics/api_client.rb', line 124

def get_key
  File.read(config_filepath).strip
end

#has_more?(status) ⇒ Boolean

Returns:

  • (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 options
  job_id = options[: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  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


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, options)
  puts 'Submitting transport job ...'
  params = build_submit_params(dest_ds_type, from_ds_type, options)
  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_urlObject



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