Class: DTK::Client::Conn

Inherits:
Object
  • Object
show all
Includes:
ParseFile
Defined in:
lib/core.rb

Constant Summary collapse

VERBOSE_MODE_ON =
::DTK::Configuration.get(:verbose_rest_calls)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ParseFile

#parse_key_value_file

Constructor Details

#initializeConn

Returns a new instance of Conn.



316
317
318
319
320
# File 'lib/core.rb', line 316

def initialize
  @cookies = Hash.new
  @connection_error = nil
  
end

Instance Attribute Details

#connection_errorObject (readonly)

Returns the value of attribute connection_error.



324
325
326
# File 'lib/core.rb', line 324

def connection_error
  @connection_error
end

#cookiesObject (readonly)

Returns the value of attribute cookies.



324
325
326
# File 'lib/core.rb', line 324

def cookies
  @cookies
end

Class Method Details

.get_timeoutObject



330
331
332
# File 'lib/core.rb', line 330

def self.get_timeout
  DefaultRestOpts[:timeout]
end

.set_timeout(timeout_sec) ⇒ Object



334
335
336
# File 'lib/core.rb', line 334

def self.set_timeout(timeout_sec)
  DefaultRestOpts[:timeout] = timeout_sec
end

Instance Method Details

#check_and_wrap_response(command_class, rest_method_func) ⇒ Object

method will repeat request in case session has expired



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/core.rb', line 376

def check_and_wrap_response(command_class, rest_method_func)
  response = rest_method_func.call

  if ResponseErrorHandler.check_for_session_expiried(response)
    # re-logging user and repeating request
    DTK::Client::OsUtil.print("Session expired: re-establishing session & re-trying request ...", :yellow)
    @cookies = DTK::Client::Session.re_initialize
    response = rest_method_func.call
  end

  response_obj = Response.new(command_class, response)

  # queue messages from server to be displayed later
  DTK::Shell::MessageQueue.process_response(response_obj)

  response_obj
end

#connection_error?Boolean

Returns:

  • (Boolean)


396
397
398
# File 'lib/core.rb', line 396

def connection_error?
  return !@connection_error.nil?
end

#get(command_class, url) ⇒ Object



349
350
351
352
353
# File 'lib/core.rb', line 349

def get(command_class,url)
  ap "GET #{url}" if VERBOSE_MODE_ON

  check_and_wrap_response(command_class, Proc.new { json_parse_if_needed(get_raw(url)) })
end

#get_usernameObject



338
339
340
# File 'lib/core.rb', line 338

def get_username
  get_credentials[:username]
end

#logoutObject



400
401
402
403
404
405
406
407
408
# File 'lib/core.rb', line 400

def logout
  response = get_raw rest_url("user/process_logout")

  # save cookies - no need to persist them
  # DiskCacher.new.save_cookie(@cookies)

  raise DTK::Client::DtkError, "Failed to logout, and terminate session!" unless response
  @cookies = nil
end

#post(command_class, url, body = nil) ⇒ Object



355
356
357
358
359
360
361
362
363
# File 'lib/core.rb', line 355

def post(command_class,url,body=nil)
  if VERBOSE_MODE_ON
    ap "POST (REST) #{url}"
    ap "params: "
    ap body
  end

  check_and_wrap_response(command_class, Proc.new { json_parse_if_needed(post_raw(url,body)) })
end

#post_file(command_class, url, body = nil) ⇒ Object



365
366
367
368
369
370
371
372
373
# File 'lib/core.rb', line 365

def post_file(command_class,url,body=nil)
  if VERBOSE_MODE_ON
    ap "POST (FILE) #{url}"
    ap "params: "
    ap body
  end

  check_and_wrap_response(command_class, Proc.new { json_parse_if_needed(post_raw(url,body,{:content_type => 'avro/binary'})) })
end

Method will warn user that connection could not be established. User should check configuration to make sure that connection is properly set.



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/core.rb', line 414

def print_warning
  creds = get_credentials
  puts   "[WARNING] Unable to connect to server, please check you configuration."
  puts   "========================== Configuration =========================="
  printf "%15s %s\n", "REST endpoint:", rest_url
  printf "%15s %s\n", "Username:", "#{creds[:username]}"
  printf "%15s %s\n", "Password:", "#{creds[:password] ? creds[:password].gsub(/./,'*') : 'No password set'}"
  puts   "==================================================================="

  if self.connection_error['errors'].first['errors']
    error_code = self.connection_error['errors'].first['errors'].first['code']
    print " Error code: "
    DTK::Client::OsUtil.print(error_code, :red)
  end
end

#rest_url(route = nil) ⇒ Object



342
343
344
345
346
347
# File 'lib/core.rb', line 342

def rest_url(route=nil)
  protocol, port = "http", Config[:server_port].to_s
  protocol, port = "https", Config[:secure_connection_server_port].to_s if Config[:secure_connection] == "true"

  "#{protocol}://#{Config[:server_host]}:#{port}/rest/#{route}"
end