Skip to content

Commit 5187a42

Browse files
Merge pull request #75 from eitoball/add-ability-to-set-proxy
Add ability to set proxy
2 parents 1e24ce0 + 031983a commit 5187a42

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

examples/example.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@
1515
# request_headers: headers,
1616
# http_options: {open_timeout: 15, read_timeout: 30})
1717

18+
# If you want to make request via proxy, you can set your proxy server in two ways.
19+
#
20+
# (1) Pass proxy_options hash
21+
#
22+
# client = SendGrid::Client.new(host: host,
23+
# request_headers: headers,
24+
# proxy_options: { host: '127.0.0.1', port: 8080 })
25+
#
26+
# (2) Set 'http_proxy' environment variable
27+
#
28+
# ENV['http_proxy'] = 'user:[email protected]:8080'
29+
# client = SendGrid::Client.new(host: host, request_headers: headers)
30+
1831
# GET Collection
1932
query_params = { 'limit' => 100, 'offset' => 0 }
2033
response = client.version('v3').api_keys.get(query_params: query_params)

lib/ruby_http_client.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ class Client
3535
# Or just pass the version as part of the URL
3636
# (e.g. client._("/v3"))
3737
# - +url_path+ -> A list of the url path segments
38+
# - +proxy_options+ -> A hash of proxy settings.
39+
# (e.g. { host: '127.0.0.1', port: 8080 })
3840
#
39-
def initialize(host: nil, request_headers: nil, version: nil, url_path: nil, http_options: {})
41+
def initialize(host: nil, request_headers: nil, version: nil, url_path: nil, http_options: {}, proxy_options: {})
4042
@host = host
4143
@request_headers = request_headers || {}
4244
@version = version
@@ -45,6 +47,7 @@ def initialize(host: nil, request_headers: nil, version: nil, url_path: nil, htt
4547
@query_params = nil
4648
@request_body = nil
4749
@http_options = http_options
50+
@proxy_options = proxy_options
4851
end
4952

5053
# Update the headers for the request
@@ -139,7 +142,7 @@ def build_url(query_params: nil)
139142
def build_request(name, args)
140143
build_args(args) if args
141144
uri = build_url(query_params: @query_params)
142-
@http = add_ssl(Net::HTTP.new(uri.host, uri.port))
145+
@http = build_http(uri.host, uri.port)
143146
net_http = Kernel.const_get('Net::HTTP::' + name.to_s.capitalize)
144147
@request = build_request_headers(net_http.new(uri.request_uri))
145148
if @request_body &&
@@ -173,6 +176,16 @@ def make_request(http, request)
173176
Response.new(response)
174177
end
175178

179+
# Build HTTP request object
180+
#
181+
# * *Returns* :
182+
# - Request object
183+
def build_http(host, port)
184+
params = [host, port]
185+
params = params + @proxy_options.values_at(:host, :port, :user, :pass) unless @proxy_options.empty?
186+
add_ssl(Net::HTTP.new(*params))
187+
end
188+
176189
# Allow for https calls
177190
#
178191
# * *Args* :

test/test_ruby_http_client.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,38 @@ def test_http_options
185185
assert_equal(['test'], url1.url_path)
186186
end
187187

188+
def test_proxy_options
189+
proxy_options = {
190+
host: '127.0.0.1', port: 8080, user: 'anonymous', pass: 'secret'
191+
}
192+
client = MockRequest.new(
193+
host: 'https://api.sendgrid.com',
194+
request_headers: { 'Authorization' => 'Bearer xxx' },
195+
proxy_options: proxy_options
196+
).version('v3').api_keys
197+
198+
assert(client.proxy_address, '127.0.0.1')
199+
assert(client.proxy_pass, 'secret')
200+
assert(client.proxy_port, 8080)
201+
assert(client.proxy_user, 'anonymous')
202+
end
203+
204+
def test_proxy_from_http_proxy_environment_variable
205+
ENV['http_proxy'] = 'anonymous:[email protected]:8080'
206+
207+
client = MockRequest.new(
208+
host: 'https://api.sendgrid.com',
209+
request_headers: { 'Authorization' => 'Bearer xxx' }
210+
).version('v3').api_keys
211+
212+
assert(client.proxy_address, '127.0.0.1')
213+
assert(client.proxy_pass, 'secret')
214+
assert(client.proxy_port, 8080)
215+
assert(client.proxy_user, 'anonymous')
216+
ensure
217+
ENV.delete('http_proxy')
218+
end
219+
188220
def test_docker_exists
189221
assert(File.file?('./Dockerfile') || File.file?('./docker/Dockerfile'))
190222
end

0 commit comments

Comments
 (0)