Module: VCloud::RestApi
- Included in:
- BaseVCloudEntity
- Defined in:
- lib/vcloud/rest_api.rb
Instance Method Summary collapse
- #build_generic_http_opts(url, payload, content_type, session, opts) ⇒ Object
- #create ⇒ Object
- #delete(url, payload, content_type, session = self.session, opts = {}) ⇒ Object
- #http_request(http_opts) ⇒ Object
-
#parse_response(response) ⇒ Object
override to provide custom parsing.
- #post(url, payload, content_type, session = self.session, opts = {}) ⇒ Object
- #refresh(session = self.session) ⇒ Object
- #update ⇒ Object
Instance Method Details
#build_generic_http_opts(url, payload, content_type, session, opts) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/vcloud/rest_api.rb', line 74 def build_generic_http_opts(url, payload, content_type, session, opts) headers = {:accept => VCloud::Constants::ACCEPT_HEADER+";version=#{session.api_version}"}.merge(session.token) headers.merge!(:content_type => content_type) if content_type { :url => url, :payload => payload, :verify_ssl => session.verify_ssl, :headers => headers }.merge(opts) end |
#create ⇒ Object
12 13 |
# File 'lib/vcloud/rest_api.rb', line 12 def create end |
#delete(url, payload, content_type, session = self.session, opts = {}) ⇒ Object
18 19 20 21 22 |
# File 'lib/vcloud/rest_api.rb', line 18 def delete(url, payload, content_type, session = self.session, opts={}) http_opts = build_generic_http_opts(url, payload, content_type, session, opts) http_opts.merge!(:method => 'delete') http_request(http_opts) end |
#http_request(http_opts) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/vcloud/rest_api.rb', line 30 def http_request(http_opts) request = RestClient::Request.new(http_opts) response = request.execute { |response, request| case response.code when 200, 201, 202, 204 then return response.body when 303 then # 303 See Other, provides a Location header # TODO: How do we handle this? return response.body when 400, 401, 403, 404, 405, 500, 501, 503 then # If there is a body, try to parse as Error xml doc and raise exception if not response.body.strip.empty? error = VCloud::Error.from_xml(response.body) if error.instance_of?(VCloud::Error) raise VCloud::VCloudError.new( error., error.major_error_code, error.minor_error_code, error.vendor_specific_error_code, error.stack_trace) end end # No body was supplied or body wasn't an Error xml doc, create and raise exception major_error_code = response.code = VCloud::Errors::HTTPMessage[response.code][:short_message] = VCloud::Errors::HTTPMessage[response.code][:message] = "#{} - #{}" raise VCloud::VCloudError.new(, major_error_code) else raise VCloud::VCloudError.new('An unexpected return code was received', response.code) end } response.nil? ? '' : response.body rescue OpenSSL::SSL::SSLError => err raise VCloud::VCloudError.new("Failed to verify SSL certifcate at API target URL. Either verify that the target server has a properly named SSL certificate or create the client to skip ssl verification. Eg: session = VCloud::Client.new('https://api.vcloud.com', '1.5', :verify_ssl => false)", 'na') end |
#parse_response(response) ⇒ Object
override to provide custom parsing
86 87 88 |
# File 'lib/vcloud/rest_api.rb', line 86 def parse_response(response) parse_xml(response.body) end |
#post(url, payload, content_type, session = self.session, opts = {}) ⇒ Object
24 25 26 27 28 |
# File 'lib/vcloud/rest_api.rb', line 24 def post(url, payload, content_type, session = self.session, opts={}) http_opts = build_generic_http_opts(url, payload, content_type, session, opts) http_opts.merge!(:method => 'post') http_request(http_opts) end |
#refresh(session = self.session) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/vcloud/rest_api.rb', line 4 def refresh(session = self.session) http_opts = build_generic_http_opts(@href, nil, nil, session, {}) http_opts.merge!(:method => 'get') http_opts[:headers][:accept] = self.class.type+";version=#{session.api_version}" response = http_request(http_opts) parse_response(response) end |
#update ⇒ Object
15 16 |
# File 'lib/vcloud/rest_api.rb', line 15 def update end |