Skip to content

Commit

Permalink
feat: Add http_options as a params of contructor
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangtuanictvn committed Jan 10, 2021
1 parent 865a2ac commit 885a2f9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
8 changes: 5 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 @@ -15,7 +15,7 @@ class BaseInterface
# - +impersonate_subuser+ -> the subuser to impersonate, will be passed
# in the "On-Behalf-Of" header
#
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 +31,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 @@ -5,16 +5,17 @@ class API < BaseInterface
# - +api_key+ -> your Twilio SendGrid API key
# - +host+ -> the base URL for the API
# - +request_headers+ -> any headers that you want to be globally applied
# - +http_options+ -> http options that you want to be globally applied to each request
# - +version+ -> the version of the API you wish to access,
# currently only "v3" is supported
# - +impersonate_subuser+ -> the subuser to impersonate, will be passed
# in the "On-Behalf-Of" header
#
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
15 changes: 15 additions & 0 deletions test/sendgrid/test_sendgrid-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,28 @@ 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
headers = JSON.parse('{"X-Mock": 200}')
http_options = {
open_timeout: 40,
read_timeout: 40
}

sg = SendGrid::API.new(api_key: 'SENDGRID_API_KEY', host: 'https://api.test.com', request_headers: headers, version: 'v3', http_options: http_options)
sg.client.get

assert_equal(40, sg.client.http.open_timeout)
assert_equal(40, sg.client.http.read_timeout)
end

def test_access_settings_activity_get
params = JSON.parse('{"limit": 1}')
headers = JSON.parse('{"X-Mock": 200}')
Expand Down

0 comments on commit 885a2f9

Please sign in to comment.