Skip to content

Commit 6785741

Browse files
feat: Add http_options as a param in the SendGrid::API's constructor (#455)
Co-authored-by: Elise Shanholtz <[email protected]>
1 parent fe2e88a commit 6785741

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

.rubocop_todo.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ Metrics/BlockLength:
3434
# Configuration parameters: CountComments, CountAsOne.
3535
Metrics/ClassLength:
3636
Max: 2006
37+
Exclude:
38+
- 'test/sendgrid/test_sendgrid-ruby.rb'
3739

3840
# Offense count: 41
3941
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods.

lib/sendgrid/base_interface.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Initialize the HTTP client
55
class BaseInterface
66
attr_accessor :client
7-
attr_reader :request_headers, :host, :version, :impersonate_subuser
7+
attr_reader :request_headers, :host, :version, :impersonate_subuser, :http_options
88

99
# * *Args* :
1010
# - +auth+ -> authorization header value
@@ -14,8 +14,9 @@ class BaseInterface
1414
# currently only "v3" is supported
1515
# - +impersonate_subuser+ -> the subuser to impersonate, will be passed
1616
# in the "On-Behalf-Of" header
17+
# - +http_options+ -> http options that you want to be globally applied to each request
1718
#
18-
def initialize(auth:, host:, request_headers: nil, version: nil, impersonate_subuser: nil)
19+
def initialize(auth:, host:, request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {})
1920
@auth = auth
2021
@host = host
2122
@version = version || 'v3'
@@ -31,7 +32,9 @@ def initialize(auth:, host:, request_headers: nil, version: nil, impersonate_sub
3132
@request_headers['On-Behalf-Of'] = @impersonate_subuser if @impersonate_subuser
3233

3334
@request_headers = @request_headers.merge(request_headers) if request_headers
35+
@http_options = http_options
3436
@client = SendGrid::Client.new(host: "#{@host}/#{@version}",
35-
request_headers: @request_headers)
37+
request_headers: @request_headers,
38+
http_options: @http_options)
3639
end
3740
end

lib/sendgrid/sendgrid.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ class API < BaseInterface
99
# currently only "v3" is supported
1010
# - +impersonate_subuser+ -> the subuser to impersonate, will be passed
1111
# in the "On-Behalf-Of" header
12+
# - +http_options+ -> http options that you want to be globally applied to each request
1213
#
13-
def initialize(api_key:, host: nil, request_headers: nil, version: nil, impersonate_subuser: nil)
14+
def initialize(api_key:, host: nil, request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {})
1415
auth = "Bearer #{api_key}"
1516
host ||= 'https://api.sendgrid.com'
1617

17-
super(auth: auth, host: host, request_headers: request_headers, version: version, impersonate_subuser: impersonate_subuser)
18+
super(auth: auth, host: host, request_headers: request_headers, version: version, impersonate_subuser: impersonate_subuser, http_options: http_options)
1819
end
1920
end
2021
end

test/sendgrid/test_sendgrid-ruby.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,31 @@ def test_init
3535
assert_equal(subuser, sg.impersonate_subuser)
3636
assert_equal('6.3.9', SendGrid::VERSION)
3737
assert_instance_of(SendGrid::Client, sg.client)
38+
assert_equal({}, sg.http_options)
3839
end
3940

4041
def test_init_when_impersonate_subuser_is_not_given
4142
sg = SendGrid::API.new(api_key: 'SENDGRID_API_KEY', host: 'https://api.test.com', version: 'v3')
4243
refute_includes(sg.request_headers, 'On-Behalf-Of')
4344
end
4445

46+
def test_init_when_http_options_is_given
47+
params = JSON.parse('{"subuser": "test_string", "ip": "test_string", "limit": 1, "exclude_whitelabels": "true", "offset": 1}')
48+
headers = JSON.parse('{"X-Mock": 200}')
49+
http_options = {
50+
open_timeout: 40,
51+
read_timeout: 40
52+
}
53+
54+
sg = SendGrid::API.new(api_key: 'SENDGRID_API_KEY', version: 'v3', http_options: http_options)
55+
client = sg.client.ips
56+
response = client.get(query_params: params, request_headers: headers)
57+
58+
assert_equal(40, client.http.open_timeout)
59+
assert_equal(40, client.http.read_timeout)
60+
assert_equal('200', response.status_code)
61+
end
62+
4563
def test_access_settings_activity_get
4664
params = JSON.parse('{"limit": 1}')
4765
headers = JSON.parse('{"X-Mock": 200}')

0 commit comments

Comments
 (0)