Class: GoodData::Rest::Connection

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/gooddata/rest/connection.rb

Overview

Wrapper of low-level HTTP/REST client/library

Constant Summary collapse

DEFAULT_URL =
'https://secure.gooddata.com'
LOGIN_PATH =
'/gdc/account/login'
TOKEN_PATH =
'/gdc/account/token'
KEYS_TO_SCRUB =
[:password, :verifyPassword, :authorizationToken]
ID_LENGTH =
16
DEFAULT_HEADERS =
{
  :content_type => :json,
  :accept => [:json, :zip],
  :user_agent => GoodData.gem_version_string
}
DEFAULT_WEBDAV_HEADERS =
{
  :user_agent => GoodData.gem_version_string
}
DEFAULT_LOGIN_PAYLOAD =
{
  :headers => DEFAULT_HEADERS,
  :verify_ssl => true
}
RETRYABLE_ERRORS =
[
  RestClient::InternalServerError,
  RestClient::RequestTimeout,
  RestClient::MethodNotAllowed,
  SystemCallError,
  Timeout::Error
]
RETRIES_ON_TOO_MANY_REQUESTS_ERROR =
12
RETRY_TIME_INITIAL_VALUE =
1
RETRY_TIME_COEFFICIENT =
1.5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Connection

Returns a new instance of Connection.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/gooddata/rest/connection.rb', line 161

def initialize(opts)
  super()
  @stats = ThreadSafe::Hash.new

  headers = opts[:headers] || {}
  @webdav_headers = DEFAULT_WEBDAV_HEADERS.merge(headers)

  @user = nil
  @server = nil
  @opts = opts

  # Initialize cookies
  reset_cookies!

  @at_exit_handler_installed = nil
end

Instance Attribute Details

#request_paramsObject (readonly) Also known as: cookies

Returns the value of attribute request_params.



153
154
155
# File 'lib/gooddata/rest/connection.rb', line 153

def request_params
  @request_params
end

#serverObject (readonly)

Returns the value of attribute server.



157
158
159
# File 'lib/gooddata/rest/connection.rb', line 157

def server
  @server
end

#statsObject (readonly)

Returns the value of attribute stats.



158
159
160
# File 'lib/gooddata/rest/connection.rb', line 158

def stats
  @stats
end

#userObject (readonly)

Returns the value of attribute user.



159
160
161
# File 'lib/gooddata/rest/connection.rb', line 159

def user
  @user
end

Class Method Details

.construct_login_payload(username, password) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/gooddata/rest/connection.rb', line 99

def (username, password)
  res = {
    'postUserLogin' => {
      'login' => username,
      'password' => password,
      'remember' => 1
    }
  }
  res
end

.generate_string(length = ID_LENGTH) ⇒ String

Generate random string with URL safe base64 encoding

Parameters:

  • length (String) (defaults to: ID_LENGTH)

    Length of random string to be generated

Returns:

  • (String)

    Generated random string



115
116
117
# File 'lib/gooddata/rest/connection.rb', line 115

def generate_string(length = ID_LENGTH)
  SecureRandom.urlsafe_base64(length)
end

.retryable(options = {}, &_block) ⇒ Object

Retry block if exception thrown



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/gooddata/rest/connection.rb', line 120

def retryable(options = {}, &_block)
  opts = { :tries => 1, :on => RETRYABLE_ERRORS }.merge(options)

  retry_exception = opts[:on]
  retries = opts[:tries]
  too_many_requests_tries = RETRIES_ON_TOO_MANY_REQUESTS_ERROR

  unless retry_exception.is_a?(Array)
    retry_exception = [retry_exception]
  end

  retry_time = RETRY_TIME_INITIAL_VALUE
  begin
    return yield
  rescue RestClient::Unauthorized, RestClient::Forbidden => e # , RestClient::Unauthorized => e
    raise e unless options[:refresh_token]
    raise e if options[:dont_reauth]
    options[:refresh_token].call # (dont_reauth: true)
    retry if (retries -= 1) > 0
  rescue RestClient::TooManyRequests, RestClient::ServiceUnavailable
    GoodData.logger.warn "Too many requests, retrying in #{retry_time} seconds"
    sleep retry_time
    retry_time *= RETRY_TIME_COEFFICIENT
    # 10 requests with 1.5 coefficent should take ~ 3 mins to finish
    retry if (too_many_requests_tries -= 1) > 1
  rescue *retry_exception => e
    GoodData.logger.warn e.inspect
    retry if (retries -= 1) > 1
  end
  yield
end

Instance Method Details

#connect(username, password, options = {}) ⇒ Object

Connect using username and password



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/gooddata/rest/connection.rb', line 179

def connect(username, password, options = {})
  server = options[:server] || Helpers::AuthHelper.read_server
  options = DEFAULT_LOGIN_PAYLOAD.merge(options)
  headers = options[:headers] || {}

  options = options.merge(headers)
  @server = RestClient::Resource.new server, options

  # Install at_exit handler first
  unless @at_exit_handler_installed
    begin
      at_exit { disconnect if @user }
    rescue RestClient::Unauthorized
      GoodData.logger.info 'Already logged out'
    ensure
      @at_exit_handler_installed = true
    end
  end

  # Reset old cookies first
  if options[:sst_token]
    merge_cookies!('GDCAuthSST' => options[:sst_token])
    get('/gdc/account/token', @request_params)

    @user = get(get('/gdc/app/account/bootstrap')['bootstrapResource']['accountSetting']['links']['self'])
    GoodData.logger.info("Connected using SST to server #{@server.url} to profile \"#{@user['accountSetting']['login']}\"")
    @auth = {}
    refresh_token :dont_reauth => true
  else
    GoodData.logger.info("Connected using username \"#{username}\" to server #{@server.url}")
    credentials = Connection.(username, password)
    generate_session_id
    @auth = post(LOGIN_PATH, credentials, :dont_reauth => true)['userLogin']

    refresh_token :dont_reauth => true
    @user = get(@auth['profile'])
  end
  GoodData.logger.info('Connection successful')
rescue RestClient::Unauthorized => e
  GoodData.logger.info('Bad Login or Password')
  GoodData.logger.info('Connection failed')
  raise e
rescue RestClient::Forbidden => e
  GoodData.logger.info('Connection failed')
  raise e
end

#delete(uri, options = {}) ⇒ Object

HTTP DELETE

Parameters:

  • uri (String)

    Target URI



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/gooddata/rest/connection.rb', line 331

def delete(uri, options = {})
  options = log_info(options)
  GoodData.rest_logger.info "DELETE: #{@server.url}#{uri}"
  profile "DELETE #{uri}" do
    b = proc do
      params = fresh_request_params(options[:request_id])
      begin
        @server[uri].delete(params)
      rescue RestClient::Exception => e
        # log the error if it happens
        log_error(e, uri, params, options)
        raise e
      end
    end
    process_response(options, &b)
  end
end

#disconnectObject

Disconnect



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/gooddata/rest/connection.rb', line 227

def disconnect
  # TODO: Wrap somehow
  url = @auth['state']

  begin
    clear_session_id
    delete url if url
  rescue RestClient::Unauthorized
    GoodData.logger.info 'Already disconnected'
  end

  @auth = nil
  @server = nil
  @user = nil

  reset_cookies!
end

#download(what, where, options = {}) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/gooddata/rest/connection.rb', line 245

def download(what, where, options = {})
  # handle the path (directory) given in what
  ilast_slash = what.rindex('/')
  if ilast_slash.nil?
    what_dir = ''
  else
    # take the directory from the path
    what_dir = what[0..ilast_slash - 1]
    # take the filename from the path
    what = what[ilast_slash + 1..-1]
  end

  option_dir = options[:directory] || ''
  option_dir = option_dir[0..-2] if option_dir[-1] == '/'

  # join the otion dir with the what_dir
  # [option dir empty, what dir empty] => the joined dir
  dir_hash = {
    [true, true] => '',
    [true, false] => what_dir,
    [false, true] => option_dir,
    [false, false] => "#{what_dir}/#{option_dir}"
  }
  dir = dir_hash[[option_dir.empty?, what_dir.empty?]]

  staging_uri = options[:staging_url].to_s

  base_url = dir.empty? ? staging_uri : URI.join(staging_uri, "#{dir}/").to_s
  url = URI.join(base_url, CGI.escape(what)).to_s

  b = proc do
    raw = {
      :headers => {
        :user_agent => GoodData.gem_version_string
      },
      :method => :get,
      :url => url,
      :verify_ssl => (@opts[:verify_ssl] == false || @opts[:verify_ssl] == OpenSSL::SSL::VERIFY_NONE) ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
    }.merge(cookies)

    if where.is_a?(IO) || where.is_a?(StringIO)
      RestClient::Request.execute(raw) do |chunk, _x, response|
        if response.code.to_s != '200'
          fail ArgumentError, "Error downloading #{url}. Got response: #{response.code} #{response} #{response.body}"
        end
        where.write chunk
      end
    else
      # Assume it is a string or file
      File.open(where, 'w') do |f|
        RestClient::Request.execute(raw) do |chunk, _x, response|
          if response.code.to_s != '200'
            fail ArgumentError, "Error downloading #{url}. Got response: #{response.code} #{response} #{response.body}"
          end
          f.write chunk
        end
      end
    end
  end

  res = nil
  GoodData::Rest::Connection.retryable(:tries => 2, :refresh_token => proc { refresh_token }) do
    res = b.call
  end
  res
end

#generate_request_idObject



502
503
504
# File 'lib/gooddata/rest/connection.rb', line 502

def generate_request_id
  "#{session_id}:#{call_id}"
end

#get(uri, options = {}, &user_block) ⇒ Object

HTTP GET

Parameters:

  • uri (String)

    Target URI



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/gooddata/rest/connection.rb', line 365

def get(uri, options = {}, &user_block)
  options = log_info(options)
  GoodData.rest_logger.info "GET: #{@server.url}#{uri}, #{options}"
  profile "GET #{uri}" do
    b = proc do
      params = fresh_request_params(options[:request_id]).merge(options)
      begin
        @server[uri].get(params, &user_block)
      rescue RestClient::Exception => e
        # log the error if it happens
        log_error(e, uri, params, options)
        raise e
      end
    end
    process_response(options, &b)
  end
end

#log_error(e, uri, params, options = {}) ⇒ Object

Helper for logging error

Parameters:

  • e (RuntimeException)

    Exception to log

  • uri (String)

    Uri on which the request failed

  • uri (Hash)

    Additional params



354
355
356
357
358
359
360
# File 'lib/gooddata/rest/connection.rb', line 354

def log_error(e, uri, params, options = {})
  return if e.response && e.response.code == 401 && !uri.include?('token') && !uri.include?('login')

  if options[:do_not_log].nil? || options[:do_not_log].index(e.class).nil?
    GoodData.logger.error(format_error(e, params))
  end
end

#post(uri, data = nil, options = {}) ⇒ Object

HTTP POST

Parameters:

  • uri (String)

    Target URI



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/gooddata/rest/connection.rb', line 408

def post(uri, data = nil, options = {})
  options = log_info(options)
  GoodData.rest_logger.info "POST: #{@server.url}#{uri}, #{scrub_params(data, KEYS_TO_SCRUB)}"
  profile "POST #{uri}" do
    payload = data.is_a?(Hash) ? data.to_json : data
    b = proc do
      params = fresh_request_params(options[:request_id])
      begin
        @server[uri].post(payload, params)
      rescue RestClient::Exception => e
        # log the error if it happens
        log_error(e, uri, params, options)
        raise e
      end
    end
    process_response(options, &b)
  end
end

#put(uri, data, options = {}) ⇒ Object

HTTP PUT

Parameters:

  • uri (String)

    Target URI



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/gooddata/rest/connection.rb', line 386

def put(uri, data, options = {})
  options = log_info(options)
  payload = data.is_a?(Hash) ? data.to_json : data
  GoodData.rest_logger.info "PUT: #{@server.url}#{uri}, #{scrub_params(data, KEYS_TO_SCRUB)}"
  profile "PUT #{uri}" do
    b = proc do
      params = fresh_request_params(options[:request_id])
      begin
        @server[uri].put(payload, params)
      rescue RestClient::Exception => e
        # log the error if it happens
        log_error(e, uri, params, options)
        raise e
      end
    end
    process_response(options, &b)
  end
end

#refresh_token(_options = {}) ⇒ Object



312
313
314
315
316
317
318
319
# File 'lib/gooddata/rest/connection.rb', line 312

def refresh_token(_options = {})
  begin # rubocop:disable RedundantBegin
    get TOKEN_PATH, :dont_reauth => true # avoid infinite loop GET fails with 401
  rescue Exception => e # rubocop:disable RescueException
    puts e.message
    raise e
  end
end

#server_urlString

Returns server URI

Returns:

  • (String)

    server uri



324
325
326
# File 'lib/gooddata/rest/connection.rb', line 324

def server_url
  @server && @server.url
end

#sst_tokenObject

Reader method for SST token



430
431
432
# File 'lib/gooddata/rest/connection.rb', line 430

def sst_token
  request_params[:cookies]['GDCAuthSST']
end

#stats_table(values = stats) ⇒ Object



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/gooddata/rest/connection.rb', line 434

def stats_table(values = stats)
  sorted = values.sort_by { |_k, v| v[:avg] }
  Terminal::Table.new :headings => %w(title avg min max total calls) do |t|
    overall = {
      :avg => 0,
      :calls => 0,
      :total => 0
    }

    sorted.each do |l|
      avg = l[1][:avg]
      min = l[1][:min]
      max = l[1][:max]
      total = l[1][:total]
      calls = l[1][:calls]

      row = [
        l[0],
        sprintf('%.3f', avg),
        sprintf('%.3f', min),
        sprintf('%.3f', max),
        sprintf('%.3f', total),
        calls
      ]

      overall[:min] = min if overall[:min].nil? || min < overall[:min]
      overall[:max] = max if overall[:max].nil? || max > overall[:max]
      overall[:total] += total
      overall[:calls] += calls
      overall[:avg] += avg

      t.add_row row
    end

    overall[:avg] = overall[:avg] / sorted.length
    row = [
      'TOTAL',
      sprintf('%.3f', overall[:avg]),
      sprintf('%.3f', overall[:min]),
      sprintf('%.3f', overall[:max]),
      sprintf('%.3f', overall[:total]),
      overall[:calls]
    ]

    t.add_row :separator
    t.add_row row
  end
end

#tt_tokenObject

Reader method for TT token



486
487
488
# File 'lib/gooddata/rest/connection.rb', line 486

def tt_token
  request_params[:cookies]['GDCAuthTT']
end

#upload(file, options = {}) ⇒ Object

Uploads a file to GoodData server



491
492
493
494
495
496
497
498
499
500
# File 'lib/gooddata/rest/connection.rb', line 491

def upload(file, options = {})
  dir = options[:directory] || ''
  staging_uri = options[:staging_url].to_s
  url = dir.empty? ? staging_uri : URI.join(staging_uri, "#{dir}/").to_s
  # Make a directory, if needed
  create_webdav_dir_if_needed url unless dir.empty?

  webdav_filename = options[:filename] || File.basename(file)
  do_stream_file URI.join(url, CGI.escape(webdav_filename)), file
end