From 453fa1a5569374ba07e639ba38936de00d1495ae Mon Sep 17 00:00:00 2001 From: James Rectenwald Date: Tue, 10 Oct 2017 20:12:56 -0400 Subject: [PATCH 1/2] Add option to set http attributes when creating an instance of Client --- examples/example.rb | 6 ++++++ lib/ruby_http_client.rb | 9 +++++++-- test/test_ruby_http_client.rb | 4 +++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/examples/example.rb b/examples/example.rb index 1832d06..1bfc0d3 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 ab0ebdb..112ff2f 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 ebf6474..b8628b3 100644 --- a/test/test_ruby_http_client.rb +++ b/test/test_ruby_http_client.rb @@ -31,9 +31,11 @@ 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) + version: @version, + http_options: @http_options) end def test_init From 3d8e9605a1cc692a860697b5dc8ce9152b76e2a7 Mon Sep 17 00:00:00 2001 From: James Rectenwald Date: Wed, 11 Oct 2017 21:33:04 -0400 Subject: [PATCH 2/2] separate test for http_options attribute --- test/test_ruby_http_client.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/test_ruby_http_client.rb b/test/test_ruby_http_client.rb index b8628b3..569a0d9 100644 --- a/test/test_ruby_http_client.rb +++ b/test/test_ruby_http_client.rb @@ -33,6 +33,9 @@ def setup @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) @@ -173,4 +176,11 @@ def test_method_missing assert_equal({'message' => 'success'}, response.body) 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 end