Class: VCloudSdk::Connection::Connection
- Inherits:
-
Object
- Object
- VCloudSdk::Connection::Connection
- Defined in:
- lib/ruby_vcloud_sdk/connection/connection.rb
Class Method Summary collapse
Instance Method Summary collapse
- #connect(username, password) ⇒ Object
- #delete(destination) ⇒ Object
-
#get(destination) ⇒ Object
GET an object from REST and return the unmarshalled object.
-
#initialize(url, request_timeout = nil, rest_client = nil, site = nil, file_uploader = nil, rest_throttle = nil) ⇒ Connection
constructor
A new instance of Connection.
- #post(destination, data, content_type = "*/*") ⇒ Object
- #put(destination, data, content_type = "*/*") ⇒ Object
-
#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.
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
Instance Method Details
#connect(username, password) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# 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) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# 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) ⇒ Object
GET an object from REST and return the unmarshalled object
43 44 45 46 47 48 49 50 51 52 |
# 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 = "*/*") ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# 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 = "*/*") ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# 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) ⇒ 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.
117 118 119 120 121 122 |
# 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 |