diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index ce6105cc..c7914181 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -34,6 +34,8 @@ Metrics/BlockLength: # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: Max: 2006 + Exclude: + - 'test/sendgrid/test_sendgrid-ruby.rb' # Offense count: 41 # Configuration parameters: CountComments, CountAsOne, ExcludedMethods. diff --git a/lib/sendgrid/base_interface.rb b/lib/sendgrid/base_interface.rb index 01dadce1..0d5615a4 100644 --- a/lib/sendgrid/base_interface.rb +++ b/lib/sendgrid/base_interface.rb @@ -4,7 +4,7 @@ # Initialize the HTTP client class BaseInterface attr_accessor :client - attr_reader :request_headers, :host, :version, :impersonate_subuser + attr_reader :request_headers, :host, :version, :impersonate_subuser, :http_options # * *Args* : # - +auth+ -> authorization header value @@ -14,8 +14,9 @@ class BaseInterface # currently only "v3" is supported # - +impersonate_subuser+ -> the subuser to impersonate, will be passed # in the "On-Behalf-Of" header + # - +http_options+ -> http options that you want to be globally applied to each request # - def initialize(auth:, host:, request_headers: nil, version: nil, impersonate_subuser: nil) + def initialize(auth:, host:, request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) @auth = auth @host = host @version = version || 'v3' @@ -31,7 +32,9 @@ def initialize(auth:, host:, request_headers: nil, version: nil, impersonate_sub @request_headers['On-Behalf-Of'] = @impersonate_subuser if @impersonate_subuser @request_headers = @request_headers.merge(request_headers) if request_headers + @http_options = http_options @client = SendGrid::Client.new(host: "#{@host}/#{@version}", - request_headers: @request_headers) + request_headers: @request_headers, + http_options: @http_options) end end diff --git a/lib/sendgrid/sendgrid.rb b/lib/sendgrid/sendgrid.rb index 8207a389..03876d5a 100644 --- a/lib/sendgrid/sendgrid.rb +++ b/lib/sendgrid/sendgrid.rb @@ -9,12 +9,13 @@ class API < BaseInterface # currently only "v3" is supported # - +impersonate_subuser+ -> the subuser to impersonate, will be passed # in the "On-Behalf-Of" header + # - +http_options+ -> http options that you want to be globally applied to each request # - def initialize(api_key:, host: nil, request_headers: nil, version: nil, impersonate_subuser: nil) + def initialize(api_key:, host: nil, request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) auth = "Bearer #{api_key}" host ||= 'https://api.sendgrid.com' - super(auth: auth, host: host, request_headers: request_headers, version: version, impersonate_subuser: impersonate_subuser) + super(auth: auth, host: host, request_headers: request_headers, version: version, impersonate_subuser: impersonate_subuser, http_options: http_options) end end end diff --git a/test/sendgrid/test_sendgrid-ruby.rb b/test/sendgrid/test_sendgrid-ruby.rb index 05e90db7..e6b3892b 100644 --- a/test/sendgrid/test_sendgrid-ruby.rb +++ b/test/sendgrid/test_sendgrid-ruby.rb @@ -35,6 +35,7 @@ def test_init assert_equal(subuser, sg.impersonate_subuser) assert_equal('6.3.9', SendGrid::VERSION) assert_instance_of(SendGrid::Client, sg.client) + assert_equal({}, sg.http_options) end def test_init_when_impersonate_subuser_is_not_given @@ -42,6 +43,23 @@ def test_init_when_impersonate_subuser_is_not_given refute_includes(sg.request_headers, 'On-Behalf-Of') end + def test_init_when_http_options_is_given + params = JSON.parse('{"subuser": "test_string", "ip": "test_string", "limit": 1, "exclude_whitelabels": "true", "offset": 1}') + headers = JSON.parse('{"X-Mock": 200}') + http_options = { + open_timeout: 40, + read_timeout: 40 + } + + sg = SendGrid::API.new(api_key: 'SENDGRID_API_KEY', version: 'v3', http_options: http_options) + client = sg.client.ips + response = client.get(query_params: params, request_headers: headers) + + assert_equal(40, client.http.open_timeout) + assert_equal(40, client.http.read_timeout) + assert_equal('200', response.status_code) + end + def test_access_settings_activity_get params = JSON.parse('{"limit": 1}') headers = JSON.parse('{"X-Mock": 200}')