Skip to content

Commit 1d5668a

Browse files
Merge pull request #20 from jrectenwald/http-options
Add option to set http attributes when creating an instance of Client
2 parents 22935da + 7761e55 commit 1d5668a

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

Diff for: examples/example.rb

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
host = 'https://api.sendgrid.com'
1010
client = SendGrid::Client.new(host: host, request_headers: headers)
1111

12+
# You can pass in an http_options hash to set values for NET::HTTP attributes
13+
# https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTP.html
14+
# client = SendGrid::Client.new(host: host,
15+
# request_headers: headers,
16+
# http_options: {open_timeout: 15, read_timeout: 30})
17+
1218
# GET Collection
1319
query_params = { 'limit' => 100, 'offset' => 0 }
1420
response = client.version('v3').api_keys.get(query_params: query_params)

Diff for: lib/ruby_http_client.rb

+7-2
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ class Client
3636
# (e.g. client._("/v3"))
3737
# - +url_path+ -> A list of the url path segments
3838
#
39-
def initialize(host: nil, request_headers: nil, version: nil, url_path: nil)
39+
def initialize(host: nil, request_headers: nil, version: nil, url_path: nil, http_options: {})
4040
@host = host
4141
@request_headers = request_headers || {}
4242
@version = version
4343
@url_path = url_path || []
4444
@methods = %w[delete get patch post put]
4545
@query_params = nil
4646
@request_body = nil
47+
@http_options = http_options
4748
end
4849

4950
# Update the headers for the request
@@ -152,6 +153,9 @@ def build_request(name, args)
152153
else
153154
@request.body = @request_body
154155
end
156+
@http_options.each do |attribute, value|
157+
@http.send("#{attribute}=", value)
158+
end
155159
make_request(@http, @request)
156160
end
157161

@@ -198,7 +202,8 @@ def _(name = nil)
198202
url_path = name ? @url_path.push(name) : @url_path
199203
@url_path = []
200204
Client.new(host: @host, request_headers: @request_headers,
201-
version: @version, url_path: url_path)
205+
version: @version, url_path: url_path,
206+
http_options: @http_options)
202207
end
203208

204209
# Dynamically add segments to the url, then call a method.

Diff for: test/test_ruby_http_client.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,14 @@ def setup
3232
')
3333
@host = 'http://localhost:4010'
3434
@version = 'v3'
35+
@http_options = {open_timeout: 60, read_timeout: 60}
3536
@client = MockRequest.new(host: @host,
3637
request_headers: @headers,
3738
version: @version)
39+
@client_with_options = MockRequest.new(host: @host,
40+
request_headers: @headers,
41+
version: @version,
42+
http_options: @http_options)
3843
end
3944

4045
def test_init
@@ -173,6 +178,13 @@ def test_method_missing
173178
assert_equal({'headers' => 'test'}, response.headers)
174179
end
175180

181+
def test_http_options
182+
url1 = @client_with_options._('test')
183+
assert_equal(@host, @client_with_options.host)
184+
assert_equal(@headers, @client_with_options.request_headers)
185+
assert_equal(['test'], url1.url_path)
186+
end
187+
176188
def test_docker_exists
177189
assert(File.file?('./Dockerfile') || File.file?('./docker/Dockerfile'))
178190
end
@@ -242,5 +254,4 @@ def test_license_date_is_updated
242254
current_year = Time.new.year
243255
assert_equal(current_year, license_end_year)
244256
end
245-
246257
end

0 commit comments

Comments
 (0)