Skip to content

Commit

Permalink
Proxy configuration
Browse files Browse the repository at this point in the history
* Add proxy config variables
* Use a proxy connection if proxy is enabled
  • Loading branch information
mtchavez committed Aug 1, 2016
1 parent 5d948d8 commit cf05a05
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ CircleCi.configure do |config|
end
```

Setup for proxying requests
```ruby
require 'circleci'

CircleCi.configure do |config|
config.token = ENV['CIRCLECI_TOKEN']
config.proxy_host = 'http://ciproxy.mycompany.com'
config.proxy_port = 8000
config.proxy = true
end
```

## API Endpoints

* [User](#user)
Expand Down
19 changes: 18 additions & 1 deletion lib/circleci/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,37 @@ class Config
DEFAULT_URI = "#{DEFAULT_HOST}/api/#{DEFAULT_VERSION}".freeze
DEFAULT_PORT = 443

attr_accessor :token, :host, :port, :request_overrides, :version
attr_accessor :token, :host, :port, :request_overrides, :version, :proxy,
:proxy_host, :proxy_port, :proxy_user, :proxy_pass

##
#
# @private
def initialize
@host = DEFAULT_HOST
@port = DEFAULT_PORT
@proxy = false
@version = DEFAULT_VERSION
@request_overrides = {}
end

def uri
URI.parse("#{@host || DEFAULT_HOST}:#{@port || DEFAULT_PORT}/api/#{@version || DEFAULT_VERSION}")
end

def proxy_userinfo?
@proxy_user && @proxy_pass
end

def proxy_port
@proxy_port ? @proxy_port : 80
end

def proxy_uri
return unless @proxy && @proxy_host
host_uri = URI.parse(@proxy_host)
userinfo = proxy_userinfo? ? "#{@proxy_user}:#{@proxy_pass}@" : ''
URI.parse("#{host_uri.scheme}://#{userinfo}#{host_uri.host}:#{proxy_port}#{host_uri.path}")
end
end
end
12 changes: 11 additions & 1 deletion lib/circleci/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Request
def initialize(config, path, params = {})
@config = config
@uri = build_uri(path, params)
@net = Net::HTTP.new(@uri.hostname, @uri.port)
@net = net_http
setup_http(@net)
end

Expand Down Expand Up @@ -65,6 +65,16 @@ def build_uri(path, params = {})
uri
end

def net_http
proxy_uri = @config.proxy_uri
if @config.proxy && proxy_uri
Net::HTTP.new(@uri.hostname, @uri.port,
proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
else
Net::HTTP.new(@uri.hostname, @uri.port, nil, nil, nil, nil)
end
end

def transmit(req)
response = nil
@net.start do |http|
Expand Down

0 comments on commit cf05a05

Please sign in to comment.