diff --git a/lib/dogapi/common.rb b/lib/dogapi/common.rb index 96e715f8..06b3dad7 100644 --- a/lib/dogapi/common.rb +++ b/lib/dogapi/common.rb @@ -88,13 +88,12 @@ def initialize(api_key, application_key, silent=true, timeout=nil, endpoint=nil) def connect connection = Net::HTTP - # After ruby 2.0 Net::HTTP looks for the env variable but not ruby 1.9 - if RUBY_VERSION < '2.0.0' - proxy = ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ENV['HTTP_PROXY'] || ENV['http_proxy'] - if proxy - proxy_uri = URI.parse(proxy) - connection = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password) - end + # Expose using a proxy without setting the HTTPS_PROXY or HTTP_PROXY variables + proxy = Dogapi.find_proxy() + + if proxy + proxy_uri = URI.parse(proxy) + connection = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password) end uri = URI.parse(@api_host) @@ -211,6 +210,12 @@ def Dogapi.find_localhost @@hostname = Addrinfo.getaddrinfo(Socket.gethostname, nil, nil, nil, nil, Socket::AI_CANONNAME).first.canonname end + def Dogapi.find_proxy + ENV['DD_PROXY_HTTPS'] || ENV['dd_proxy_https'] || + ENV['DD_PROXY_HTTP'] || ENV['dd_proxy_http'] || + ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ENV['HTTP_PROXY'] || ENV['http_proxy'] + end + def Dogapi.validate_tags(tags) unless tags.is_a? Array raise ArgumentError, "The tags parameter needs to be an array of string. Current value: #{tags}" diff --git a/spec/unit/common_spec.rb b/spec/unit/common_spec.rb index dfc62f3b..db6ec713 100644 --- a/spec/unit/common_spec.rb +++ b/spec/unit/common_spec.rb @@ -61,6 +61,26 @@ ENV['http_proxy'] = nil end + it 'uses-the dogapi specific proxy if set' do + service = Dogapi::APIService.new('api_key', 'app_key') + + service.connect do |conn| + expect(conn.proxy_address).to be(nil) + expect(conn.proxy_port).to be(nil) + end + + ENV['https_proxy'] = 'https://www.proxy.com:443' + ENV['dd_proxy_https'] = 'https://www.otherproxy.com:443' + + service.connect do |conn| + expect(conn.proxy_address).to eq 'www.otherproxy.com' + expect(conn.proxy_port).to eq 443 + end + + ENV['https_proxy'] = nil + ENV['dd_proxy_https'] = nil + end + it 'respects the endpoint configuration' do service = Dogapi::APIService.new('api_key', 'app_key', true, nil, 'https://app.example.com')