Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add http_options as a param in the SendGrid::API's constructor #455

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Metrics/BlockLength:
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 2006
Exclude:
eshanholtz marked this conversation as resolved.
Show resolved Hide resolved
- 'test/sendgrid/test_sendgrid-ruby.rb'

# Offense count: 41
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods.
Expand Down
9 changes: 6 additions & 3 deletions lib/sendgrid/base_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
Expand All @@ -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
5 changes: 3 additions & 2 deletions lib/sendgrid/sendgrid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions test/sendgrid/test_sendgrid-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,31 @@ def test_init
assert_equal(subuser, sg.impersonate_subuser)
assert_equal('6.3.8', SendGrid::VERSION)
assert_instance_of(SendGrid::Client, sg.client)
assert_equal({}, sg.http_options)
end

def test_init_when_impersonate_subuser_is_not_given
sg = SendGrid::API.new(api_key: 'SENDGRID_API_KEY', host: 'https://api.test.com', version: 'v3')
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}')
Expand Down