lib/3scale/client.rb in 3scale_client-2.3.4 vs lib/3scale/client.rb in 3scale_client-2.4.0.pre.1

- old
+ new

@@ -1,9 +1,10 @@ require 'cgi' require 'uri' require 'net/http' require 'nokogiri' +require '3scale/client/http_client' require '3scale/response' require '3scale/authorize_response' module ThreeScale @@ -43,15 +44,17 @@ if options[:provider_key].nil? || options[:provider_key] =~ /^\s*$/ raise ArgumentError, 'missing :provider_key' end @provider_key = options[:provider_key] - @host = options[:host] || DEFAULT_HOST + + @host = options[:host] ||= DEFAULT_HOST + + @http = ThreeScale::Client::HTTPClient.new(options) end - attr_reader :provider_key - attr_reader :host + attr_reader :provider_key, :host, :http # Authorize and report an application. # TODO (in the mean time read authorize comments or head over to https://support.3scale.net/reference/activedocs#operation/66 for details # def authrep(options) @@ -79,12 +82,11 @@ log << "#{escaped_key}=#{CGI.escape(value)}" end path += "&#{log.join('&')}" end - uri = URI.parse("http://#{host}#{path}") - http_response = Net::HTTP.get_response(uri) + http_response = @http.get(path) case http_response when Net::HTTPSuccess,Net::HTTPConflict build_authorize_response(http_response.body) when Net::HTTPClientError @@ -137,12 +139,11 @@ raise ArgumentError, 'no transactions to report' if transactions.empty? payload = encode_transactions(transactions) payload['provider_key'] = CGI.escape(provider_key) - uri = URI.parse("http://#{host}/transactions.xml") - http_response = Net::HTTP.post_form(uri, payload) + http_response = @http.post('/transactions.xml', payload) case http_response when Net::HTTPSuccess build_report_response when Net::HTTPClientError @@ -183,12 +184,11 @@ # end # def authorize(options) path = "/transactions/authorize.xml" + options_to_params(options, ALL_PARAMS) - uri = URI.parse("http://#{host}#{path}") - http_response = Net::HTTP.get_response(uri) + http_response = @http.get(path) case http_response when Net::HTTPSuccess,Net::HTTPConflict build_authorize_response(http_response.body) when Net::HTTPClientError @@ -230,11 +230,10 @@ # end # def oauth_authorize(options) path = "/transactions/oauth_authorize.xml" + options_to_params(options, OAUTH_PARAMS) - uri = URI.parse("http://#{host}#{path}") - http_response = Net::HTTP.get_response(uri) + http_response = @http.get(path) case http_response when Net::HTTPSuccess,Net::HTTPConflict build_authorize_response(http_response.body) when Net::HTTPClientError