Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ability to set proxy #75

Merged
merged 1 commit into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@
# request_headers: headers,
# http_options: {open_timeout: 15, read_timeout: 30})

# If you want to make request via proxy, you can set your proxy server in two ways.
#
# (1) Pass proxy_options hash
#
# client = SendGrid::Client.new(host: host,
# request_headers: headers,
# proxy_options: { host: '127.0.0.1', port: 8080 })
#
# (2) Set 'http_proxy' environment variable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about https_proxy ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, you cannot use https_proxy. It is from ruby's `URI::Generic#find_proxy'.

#
# ENV['http_proxy'] = 'user:pass@127.0.0.1:8080'
# client = SendGrid::Client.new(host: host, request_headers: headers)

# GET Collection
query_params = { 'limit' => 100, 'offset' => 0 }
response = client.version('v3').api_keys.get(query_params: query_params)
Expand Down
17 changes: 15 additions & 2 deletions lib/ruby_http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ class Client
# Or just pass the version as part of the URL
# (e.g. client._("/v3"))
# - +url_path+ -> A list of the url path segments
# - +proxy_options+ -> A hash of proxy settings.
# (e.g. { host: '127.0.0.1', port: 8080 })
#
def initialize(host: nil, request_headers: nil, version: nil, url_path: nil, http_options: {})
def initialize(host: nil, request_headers: nil, version: nil, url_path: nil, http_options: {}, proxy_options: {})
@host = host
@request_headers = request_headers || {}
@version = version
Expand All @@ -45,6 +47,7 @@ def initialize(host: nil, request_headers: nil, version: nil, url_path: nil, htt
@query_params = nil
@request_body = nil
@http_options = http_options
@proxy_options = proxy_options
end

# Update the headers for the request
Expand Down Expand Up @@ -139,7 +142,7 @@ def build_url(query_params: nil)
def build_request(name, args)
build_args(args) if args
uri = build_url(query_params: @query_params)
@http = add_ssl(Net::HTTP.new(uri.host, uri.port))
@http = build_http(uri.host, uri.port)
net_http = Kernel.const_get('Net::HTTP::' + name.to_s.capitalize)
@request = build_request_headers(net_http.new(uri.request_uri))
if @request_body &&
Expand Down Expand Up @@ -173,6 +176,16 @@ def make_request(http, request)
Response.new(response)
end

# Build HTTP request object
#
# * *Returns* :
# - Request object
def build_http(host, port)
params = [host, port]
params = params + @proxy_options.values_at(:host, :port, :user, :pass) unless @proxy_options.empty?
add_ssl(Net::HTTP.new(*params))
end

# Allow for https calls
#
# * *Args* :
Expand Down
32 changes: 32 additions & 0 deletions test/test_ruby_http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,38 @@ def test_http_options
assert_equal(['test'], url1.url_path)
end

def test_proxy_options
proxy_options = {
host: '127.0.0.1', port: 8080, user: 'anonymous', pass: 'secret'
}
client = MockRequest.new(
host: 'https://api.sendgrid.com',
request_headers: { 'Authorization' => 'Bearer xxx' },
proxy_options: proxy_options
).version('v3').api_keys

assert(client.proxy_address, '127.0.0.1')
assert(client.proxy_pass, 'secret')
assert(client.proxy_port, 8080)
assert(client.proxy_user, 'anonymous')
end

def test_proxy_from_http_proxy_environment_variable
ENV['http_proxy'] = 'anonymous:secret@127.0.0.1:8080'

client = MockRequest.new(
host: 'https://api.sendgrid.com',
request_headers: { 'Authorization' => 'Bearer xxx' }
).version('v3').api_keys

assert(client.proxy_address, '127.0.0.1')
assert(client.proxy_pass, 'secret')
assert(client.proxy_port, 8080)
assert(client.proxy_user, 'anonymous')
ensure
ENV.delete('http_proxy')
end

def test_docker_exists
assert(File.file?('./Dockerfile') || File.file?('./docker/Dockerfile'))
end
Expand Down