class DTK::Client::Conn
Constants
- REST_PREFIX
- REST_VERSION
Attributes
connection_error[R]
Public Class Methods
get_timeout()
click to toggle source
# File lib/client/conn.rb, line 29 def self.get_timeout default_rest_opts[:timeout] end
new()
click to toggle source
# File lib/client/conn.rb, line 21 def initialize @cookies = {} @connection_error = nil login end
set_timeout(timeout_sec)
click to toggle source
# File lib/client/conn.rb, line 33 def self.set_timeout(timeout_sec) default_rest_opts[:timeout] = timeout_sec end
Public Instance Methods
connection_error?()
click to toggle source
# File lib/client/conn.rb, line 88 def connection_error? !connection_error.nil? end
connection_refused_error_code?()
click to toggle source
# File lib/client/conn.rb, line 92 def connection_refused_error_code? error_code? == 'connection_refused' or (original_exception? and original_exception?.kind_of?(::Errno::EPIPE)) end
get(route, query_string_hash = {})
click to toggle source
# File lib/client/conn.rb, line 41 def get(route, query_string_hash = {}) url = rest_url(route) ap "GET #{url}" if verbose_mode_on? check_and_wrap_response { json_parse_if_needed(get_raw(url, query_string_hash)) } end
get_username()
click to toggle source
# File lib/client/conn.rb, line 37 def get_username get_credentials[:username] end
post(route, post_body = {})
click to toggle source
# File lib/client/conn.rb, line 48 def post(route, post_body = {}) url = rest_url(route) if verbose_mode_on? ap "POST (REST) #{url}" ap "params: " ap post_body end check_and_wrap_response { json_parse_if_needed(post_raw(url, post_body)) } end
post_file(route, post_body = {})
click to toggle source
# File lib/client/conn.rb, line 59 def post_file(route, post_body = {}) url = rest_url(route) if verbose_mode_on? ap "POST (FILE) #{url}" ap "params: " ap post_body end check_and_wrap_response { json_parse_if_needed(post_raw(url,post_body,{:content_type => 'avro/binary'})) } end
print_warning()
click to toggle source
Method will warn user that connection could not be established. User should check configuration to make sure that connection is properly set.
# File lib/client/conn.rb, line 74 def print_warning creds = get_credentials puts "[ERROR] 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 error_code = error_code? OsUtil.print_error("Error code: #{error_code}") end end
Private Instance Methods
check_and_wrap_response(&rest_method_func)
click to toggle source
method will repeat request in case session has expired
# File lib/client/conn.rb, line 130 def check_and_wrap_response(&rest_method_func) response = rest_method_func.call if Response::ErrorHandler.check_for_session_expiried(response) # re-logging user and repeating request OsUtil.print_warning("Session expired: re-establishing session & re-trying request ...") @cookies = Session.re_initialize response = rest_method_func.call end response_obj = Response.new(response) # queue messages from server to be displayed later #TODO: DTK-2554: put in processing of messages Shell::MessageQueue.process_response(response_obj) response_obj end
default_rest_opts()
click to toggle source
# File lib/client/conn.rb, line 180 def default_rest_opts @default_rest_opts ||= get_default_rest_opts end
error_code?()
click to toggle source
# File lib/client/conn.rb, line 99 def error_code? connection_error['errors'].first['code'] rescue nil end
get_credentials()
click to toggle source
# File lib/client/conn.rb, line 176 def get_credentials @parsed_credentials ||= Configurator.get_credentials end
get_default_rest_opts()
click to toggle source
# File lib/client/conn.rb, line 184 def get_default_rest_opts # In development mode we want bigger timeout allowing us to debbug on server while still # keeping connection alive and receivinga response timeout = Config[:development_mode] ? 2000 : 150 { :timeout => timeout, :open_timeout => 10, :verify_ssl => OpenSSL::SSL::VERIFY_PEER, :ssl_ca_file => File.expand_path('../client/config/cacert.pem', File.dirname(__FILE__)), } end
get_raw(url, query_string_hash = {})
click to toggle source
# File lib/client/conn.rb, line 196 def get_raw(url, query_string_hash = {}) Response::RestClientWrapper.get_raw(url, query_string_hash, default_rest_opts.merge(:cookies => @cookies)) end
get_rest_url_base()
click to toggle source
# File lib/client/conn.rb, line 119 def get_rest_url_base protocol, port = if "#{Config[:secure_connection]}" == 'true' ['https', Config[:secure_connection_server_port].to_s] else ['http', Config[:server_port].to_s] end "#{protocol}://#{Config[:server_host]}:#{port}" end
json_parse_if_needed(item)
click to toggle source
# File lib/client/conn.rb, line 204 def json_parse_if_needed(item) Response::RestClientWrapper.json_parse_if_needed(item) end
login()
click to toggle source
# File lib/client/conn.rb, line 156 def login response = post_raw rest_url('auth/login'), get_credentials if response.kind_of?(::DTK::Common::Response) and ! response.ok? @connection_error = response else @cookies = response.cookies end end
logout()
click to toggle source
# File lib/client/conn.rb, line 165 def logout response = get_raw rest_url('auth/logout') # TODO: see if response can be nil raise Error, "Failed to logout, and terminate session!" unless response @cookies = nil end
original_exception?()
click to toggle source
# File lib/client/conn.rb, line 103 def original_exception? connection_error['errors'].first['original_exception'] rescue nil end
post_raw(url, post_body, params = {})
click to toggle source
# File lib/client/conn.rb, line 200 def post_raw(url, post_body, params = {}) Response::RestClientWrapper.post_raw(url, post_body, default_rest_opts.merge(:cookies => @cookies).merge(params)) end
rest_url(route = nil)
click to toggle source
REST_PREFIX
= “rest”
# File lib/client/conn.rb, line 111 def rest_url(route = nil) "#{rest_url_base}/#{REST_PREFIX}/#{route}" end
rest_url_base()
click to toggle source
# File lib/client/conn.rb, line 115 def rest_url_base @@rest_url_base ||= get_rest_url_base end
set_credentials(username, password)
click to toggle source
# File lib/client/conn.rb, line 172 def set_credentials(username, password) @parsed_credentials = { :username => username, :password => password} end
verbose_mode_on?()
click to toggle source
# File lib/client/conn.rb, line 147 def verbose_mode_on? if @verbose_mode_on.nil? if @verbose_mode_on ||= !!Config[:verbose_rest_calls] require 'ap' end end @verbose_mode_on end