class VCloudSdk::Connection::Connection
Constants
- ACCEPT
- REST_THROTTLE
Public Class Methods
new(url, request_timeout = nil, rest_client = nil, site = nil, file_uploader = nil, rest_throttle = nil)
click to toggle source
# File lib/ruby_vcloud_sdk/connection/connection.rb, line 16 def initialize(url, request_timeout = nil, rest_client = nil, site = nil, file_uploader = nil, rest_throttle = nil) @rest_throttle = rest_throttle || REST_THROTTLE construct_rest_logger Config.configure(rest_logger: @rest_logger) rest_client = RestClient unless rest_client rest_client.log = @rest_logger request_timeout = 60 unless request_timeout @site = site || rest_client::Resource.new( url, timeout: request_timeout) @file_uploader = file_uploader || FileUploader end
Private Class Methods
get_href(destination)
click to toggle source
# File lib/ruby_vcloud_sdk/connection/connection.rb, line 210 def get_href(destination) if destination.is_a?(Xml::Wrapper) && destination.href destination.href else destination end end
Public Instance Methods
connect(username, password)
click to toggle source
# File lib/ruby_vcloud_sdk/connection/connection.rb, line 32 def connect(username, password) login_password = "#{username}:#{password}" auth_header_value = "Basic #{Base64.strict_encode64(login_password)}" response = @site[login_url].post("", Authorization: auth_header_value, Accept: ACCEPT) Config.logger.debug(response) @vcloud_auth_header = response.headers[:x_vcloud_authorization] wrap_response(response) end
delete(destination)
click to toggle source
# File lib/ruby_vcloud_sdk/connection/connection.rb, line 97 def delete(destination) @rest_logger.info "#{__method__.to_s.upcase} #{delay}\t " + "#{self.class.get_href(destination)}" sleep(delay) response = @site[get_nested_resource(destination)].delete( Accept: ACCEPT, x_vcloud_authorization: @vcloud_auth_header, ) @rest_logger.debug(response) if response && !response.strip.empty? wrap_response(response) else nil end end
get(destination)
click to toggle source
GET an object from REST and return the unmarshalled object
# File lib/ruby_vcloud_sdk/connection/connection.rb, line 43 def get(destination) @rest_logger.info "#{__method__.to_s.upcase} #{delay}\t " + "#{self.class.get_href(destination)}" sleep(delay) response = @site[get_nested_resource(destination)].get( Accept: ACCEPT, x_vcloud_authorization: @vcloud_auth_header) @rest_logger.debug(response) wrap_response(response) end
post(destination, data, content_type = "*/*")
click to toggle source
# File lib/ruby_vcloud_sdk/connection/connection.rb, line 54 def post(destination, data, content_type = "*/*") @rest_logger.info "#{__method__.to_s.upcase} #{delay}\t " + "#{self.class.get_href(destination)}" sleep(delay) if content_type == "*/*" @rest_logger.debug( "Warning: content type not specified. Default to '*/*'") end @rest_logger.info("#{__method__.to_s.upcase} data:#{data.to_s}") response = @site[get_nested_resource(destination)].post(data.to_s, { Accept: ACCEPT, x_vcloud_authorization: @vcloud_auth_header, content_type: content_type }) fail ApiRequestError if http_error?(response) @rest_logger.debug(response) wrap_response(response) end
put(destination, data, content_type = "*/*")
click to toggle source
# File lib/ruby_vcloud_sdk/connection/connection.rb, line 73 def put(destination, data, content_type = "*/*") @rest_logger.info "#{__method__.to_s.upcase} #{delay}\t " + "#{self.class.get_href(destination)}" sleep(delay) unless content_type @rest_logger.debug( "Warning: content type not specified. Default to '*/*'") end @rest_logger.info("#{__method__.to_s.upcase} data:#{data.to_s}") response = @site[get_nested_resource(destination)].put(data.to_s, Accept: ACCEPT, x_vcloud_authorization: @vcloud_auth_header, content_type: content_type ) fail ApiRequestError if http_error?(response) @rest_logger.debug((response && !response.strip.empty?) ? response : "Received empty response.") if response && !response.strip.empty? wrap_response(response) else nil end end
put_file(destination, file)
click to toggle source
The PUT method in rest-client cannot handle large files because it does not use the underlying Net::HTTP body_stream attribute. Without that, it will not use chunked transfer-encoding. It also reads in the whole file at once.
# File lib/ruby_vcloud_sdk/connection/connection.rb, line 117 def put_file(destination, file) href = self.class.get_href(destination) @rest_logger.info "#{__method__.to_s.upcase}\t#{href}" response = @file_uploader.upload(href, file, @vcloud_auth_header) response end
Private Instance Methods
construct_rest_logger()
click to toggle source
# File lib/ruby_vcloud_sdk/connection/connection.rb, line 158 def construct_rest_logger Config.logger.debug("constructing rest_logger") config_logger_dev = Config.logger.instance_eval { @logdev }.dev if config_logger_dev.respond_to?(:path) rest_log_filename = File.join( File.dirname(config_logger_dev.path), "rest") log_file = File.open(rest_log_filename, "w") log_file.sync = true @rest_logger = Logger.new(log_file) @rest_logger.level = Config.logger.level @rest_logger.formatter = Config.logger.formatter else @rest_logger = Config.logger end end
delay()
click to toggle source
# File lib/ruby_vcloud_sdk/connection/connection.rb, line 185 def delay @rest_throttle[:min] + rand(@rest_throttle[:max] - @rest_throttle[:min]) end
get_nested_resource(destination)
click to toggle source
# File lib/ruby_vcloud_sdk/connection/connection.rb, line 190 def get_nested_resource(destination) href = self.class.get_href(destination) if href.is_a?(String) uri = URI.parse(href) if uri.query.nil? uri.path else "#{uri.path}?#{uri.query}" end else fail ApiError, "href is not a string. href:#{href.inspect}, dst:#{destination}." end end
http_error?(response)
click to toggle source
# File lib/ruby_vcloud_sdk/connection/connection.rb, line 205 def http_error?(response) response.respond_to?(:code) && response.code.to_i >= 400 end
log_exceptions(e)
click to toggle source
# File lib/ruby_vcloud_sdk/connection/connection.rb, line 176 def log_exceptions(e) if e.is_a? RestClient::Exception Config.logger.error("HTTP Code: #{e.http_code}") Config.logger.error("HTTP Body: #{e.http_body}") Config.logger.error("Message: #{e.message}") Config.logger.error("Response: #{e.response}") end end
login_url()
click to toggle source
# File lib/ruby_vcloud_sdk/connection/connection.rb, line 130 def login_url return @login_url if @login_url default_login_url = "/api/sessions" begin url_node = get("/api/versions") if url_node.nil? Config.logger.warn %Q{ Unable to find version=#{VCLOUD_VERSION_NUMBER}. Default to #{default_login_url} } @login_url = default_login_url else # Typically url_content is the full URL such as "https://10.147.0.0/api/sessions" # In this case we need to trim the beginning of the url string @login_url = get_nested_resource(url_node.login_url.content) end rescue => ex Config.logger.warn %Q{ Caught exception when retrieving login url: #{ex.to_s}" Default to #{default_login_url} } @login_url = default_login_url end end
wrap_response(response)
click to toggle source
# File lib/ruby_vcloud_sdk/connection/connection.rb, line 126 def wrap_response(response) VCloudSdk::Xml::WrapperFactory.wrap_document response end