diff --git a/examples/example.rb b/examples/example.rb index 14ed077..3fa0526 100644 --- a/examples/example.rb +++ b/examples/example.rb @@ -9,6 +9,12 @@ host = 'https://api.sendgrid.com' client = SendGrid::Client.new(host: host, request_headers: headers) +# You can pass in an http_options hash to set values for NET::HTTP attributes +# https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTP.html +# client = SendGrid::Client.new(host: host, +# request_headers: headers, +# http_options: {open_timeout: 15, read_timeout: 30}) + # GET Collection query_params = { 'limit' => 100, 'offset' => 0 } response = client.version('v3').api_keys.get(query_params: query_params) diff --git a/lib/ruby_http_client.rb b/lib/ruby_http_client.rb index 16dada0..0a64b11 100644 --- a/lib/ruby_http_client.rb +++ b/lib/ruby_http_client.rb @@ -36,7 +36,7 @@ class Client # (e.g. client._("/v3")) # - +url_path+ -> A list of the url path segments # - def initialize(host: nil, request_headers: nil, version: nil, url_path: nil) + def initialize(host: nil, request_headers: nil, version: nil, url_path: nil, http_options: {}) @host = host @request_headers = request_headers || {} @version = version @@ -44,6 +44,7 @@ def initialize(host: nil, request_headers: nil, version: nil, url_path: nil) @methods = %w[delete get patch post put] @query_params = nil @request_body = nil + @http_options = http_options end # Update the headers for the request @@ -152,6 +153,9 @@ def build_request(name, args) else @request.body = @request_body end + @http_options.each do |attribute, value| + @http.send("#{attribute}=", value) + end make_request(@http, @request) end @@ -198,7 +202,8 @@ def _(name = nil) url_path = name ? @url_path.push(name) : @url_path @url_path = [] Client.new(host: @host, request_headers: @request_headers, - version: @version, url_path: url_path) + version: @version, url_path: url_path, + http_options: @http_options) end # Dynamically add segments to the url, then call a method. diff --git a/test/test_ruby_http_client.rb b/test/test_ruby_http_client.rb index 520d59a..cfa5cbb 100644 --- a/test/test_ruby_http_client.rb +++ b/test/test_ruby_http_client.rb @@ -32,9 +32,14 @@ def setup ') @host = 'http://localhost:4010' @version = 'v3' + @http_options = {open_timeout: 60, read_timeout: 60} @client = MockRequest.new(host: @host, request_headers: @headers, version: @version) + @client_with_options = MockRequest.new(host: @host, + request_headers: @headers, + version: @version, + http_options: @http_options) end def test_init @@ -173,6 +178,13 @@ def test_method_missing assert_equal({'headers' => 'test'}, response.headers) end + def test_http_options + url1 = @client_with_options._('test') + assert_equal(@host, @client_with_options.host) + assert_equal(@headers, @client_with_options.request_headers) + assert_equal(['test'], url1.url_path) + end + def test_docker_exists assert(File.file?('./Dockerfile') || File.file?('./docker/Dockerfile')) end @@ -242,5 +254,4 @@ def test_license_date_is_updated current_year = Time.new.year assert_equal(current_year, license_end_year) end - end