lib/amee/v3/connection.rb in amee-4.3.2 vs lib/amee/v3/connection.rb in amee-4.4.0
- old
+ new
@@ -4,65 +4,101 @@
require 'active_support/core_ext/string'
module AMEE
class Connection
+ # API version used in URLs
def self.api_version
'3'
end
-
- def v3_connection
- @v3_http ||= begin
- @v3_http = Net::HTTP.new(v3_hostname, @port)
- if @ssl == true
- @v3_http.use_ssl = true
- if File.exists? RootCA
- @v3_http.ca_file = RootCA
- @v3_http.verify_mode = OpenSSL::SSL::VERIFY_PEER
- @v3_http.verify_depth = 5
- end
- end
- @v3_http.set_debug_output($stdout) if @debug
- @v3_http
- end
- end
+
+ # Perform a GET request
+ # options hash should contain query parameters
def v3_get(path, options = {})
- # Create URL parameters
- unless options.empty?
- path += "?" + options.map { |x| "#{CGI::escape(x[0].to_s)}=#{CGI::escape(x[1].to_s)}" }.join('&')
- end
- cache(path) { v3_request(Net::HTTP::Get.new(path)) }
+ # Create request parameters
+ get_params = {
+ :method => "get"
+ }
+ get_params[:params] = options unless options.empty?
+ # Send request (with caching)
+ v3_do_request(get_params, path, :cache => true)
end
+
+ # Perform a PUT request
+ # options hash should contain request body parameters
def v3_put(path, options = {})
+ # Expire cached objects from parent on down
expire_matching "#{parent_path(path)}.*"
- put = Net::HTTP::Put.new(path)
- if options[:body]
- put.body = options[:body]
- put['Content-Type'] = content_type :xml if options[:body].is_xml?
- put['Content-Type'] = content_type :json if options[:body].is_json?
- else
- put.set_form_data(options)
+ # Create request parameters
+ put_params = {
+ :method => "put",
+ :body => options[:body] ? options[:body] : form_encode(options)
+ }
+ if options[:content_type]
+ put_params[:headers] = {
+ :'Content-Type' => content_type(options[:content_type])
+ }
end
- v3_request put
+ # Request
+ v3_do_request(put_params, path)
end
+
+ # Perform a POST request
+ # options hash should contain request body parameters
+ # It can also contain a :returnobj parameter which will cause
+ # a full reponse object to be returned instead of just the body
def v3_post(path, options = {})
+ # Expire cached objects from here on down
expire_matching "#{raw_path(path)}.*"
- post = Net::HTTP::Post.new(path)
- returnobj=options.delete(:returnobj) || false
- post.set_form_data(options)
- v3_request post,returnobj
+ # Get 'return full response object' flag
+ return_obj = options.delete(:returnobj) || false
+ # Create request parameters
+ post_params = {
+ :method => "post",
+ :body => form_encode(options)
+ }
+ if options[:content_type]
+ post_params[:headers] = {
+ :'Content-Type' => content_type(options[:content_type])
+ }
+ end
+ # Request
+ v3_do_request(post_params, path, :return_obj => return_obj)
end
- def v3_delete(path, options = {})
+
+ # Perform a POST request
+ def v3_delete(path)
+ # Expire cached objects from here on down
expire_matching "#{parent_path(path)}.*"
- delete = Net::HTTP::Delete.new(path)
- v3_request delete
+ # Create request parameters
+ delete_params = {
+ :method => "delete"
+ }
+ # Request
+ v3_do_request(delete_params, path)
end
- def v3_auth
- # now the same as v2, i.e.
- [@username,@password]
- end
+
private
+
+ # Default request parameters
+ def v3_defaults
+ {
+ :verbose => DEBUG,
+ :follow_location => true,
+ :username => @username,
+ :password => @password
+ }
+ end
+
+ # Wrap up parameters into a request and execute it
+ def v3_do_request(params, path, options = {})
+ req = Typhoeus::Request.new("https://#{v3_hostname}#{path}", v3_defaults.merge(params))
+ response = do_request(req, :xml, options)
+ options[:return_obj]==true ? response : response.body
+ end
+
+ # Work out v3 hostname corresponding to v2 hostname
def v3_hostname
unless @server.starts_with?("platform-api-")
if @server.starts_with?("platform-")
@server.gsub("platform-", "platform-api-")
else
@@ -70,37 +106,8 @@
end
else
@server
end
end
- def v3_request(req,returnobj=false)
- # Open HTTP connection
- v3_connection.start
- # Set auth
- req.basic_auth *v3_auth
- # Do request
- timethen=Time.now
- response = send_request(v3_connection, req, :xml)
- Logger.log.debug("Requested #{req.class} at #{req.path} with #{req.body}, taking #{(Time.now-timethen)*1000} miliseconds")
- v3_response_ok? response, req
- returnobj ? response : response.body
- ensure
- v3_connection.finish if v3_connection.started?
- end
-
- def v3_response_ok?(response, request)
- case response.code
- when '200', '201', '204'
- return true
- when '404'
- raise AMEE::NotFound.new("The URL was not found on the server.\nRequest: #{request.method} #{request.path}")
- when '403'
- raise AMEE::PermissionDenied.new("You do not have permission to perform the requested operation.\nRequest: #{request.method} #{request.path}\n#{request.body}\Response: #{response.body}")
- when '401'
- raise AMEE::AuthFailed.new("Authentication failed. Please check your username and password.")
- when '400'
- raise AMEE::BadRequest.new("Bad request. This is probably due to malformed input data.\nRequest: #{request.method} #{request.path}\n#{request.body}\Response: #{response.body}")
- end
- raise AMEE::UnknownError.new("An error occurred while talking to AMEE: HTTP response code #{response.code}.\nRequest: #{request.method} #{request.path}\n#{request.body}\Response: #{response.body}")
- end
+
end
end