Class: VCloudSdk::Connection::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_vcloud_sdk/connection/connection.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, request_timeout = nil, rest_client = nil, site = nil, file_uploader = nil, rest_throttle = nil) ⇒ Connection

Returns a new instance of Connection.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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

Class Method Details

.get_href(destination) ⇒ Object



213
214
215
216
217
218
219
# File 'lib/ruby_vcloud_sdk/connection/connection.rb', line 213

def get_href(destination)
  if destination.is_a?(Xml::Wrapper) && destination.href
    destination.href
  else
    destination
  end
end

Instance Method Details

#connect(username, password) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ruby_vcloud_sdk/connection/connection.rb', line 32

def connect(username, password)
   = "#{username}:#{password}"
  auth_header_value = "Basic #{Base64.encode64()}"
  response = @site[].post(
      Authorization: auth_header_value, Accept: ACCEPT)
  Config.logger.debug(response)
  @cookies = response.cookies
  unless @cookies["vcloud-token"].gsub!("+", "%2B").nil?
    Config.logger.debug("@cookies: #{@cookies.inspect}.")
  end
  wrap_response(response)
end

#delete(destination) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ruby_vcloud_sdk/connection/connection.rb', line 100

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,
      cookies: @cookies
  )
  @rest_logger.debug(response)
  if response && !response.strip.empty?
    wrap_response(response)
  else
    nil
  end
end

#get(destination) ⇒ Object

GET an object from REST and return the unmarshalled object



46
47
48
49
50
51
52
53
54
55
# File 'lib/ruby_vcloud_sdk/connection/connection.rb', line 46

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,
      cookies: @cookies)
  @rest_logger.debug(response)
  wrap_response(response)
end

#post(destination, data, content_type = "*/*") ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ruby_vcloud_sdk/connection/connection.rb', line 57

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,
      cookies: @cookies,
      content_type: content_type
  })
  fail ApiRequestError if http_error?(response)
  @rest_logger.debug(response)
  wrap_response(response)
end

#put(destination, data, content_type = "*/*") ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ruby_vcloud_sdk/connection/connection.rb', line 76

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,
      cookies: @cookies,
      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) ⇒ Object

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.



120
121
122
123
124
125
# File 'lib/ruby_vcloud_sdk/connection/connection.rb', line 120

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, @cookies)
  response
end