Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 10 additions & 7 deletions lib/sendgrid/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,29 @@ module SendGrid
# Initialize the HTTP client
class API
attr_accessor :client
attr_reader :request_headers, :host, :version
attr_reader :request_headers, :host, :version, :impersonate_subuser
# * *Args* :
# - +api_key+ -> your SendGrid API key
# - +host+ -> the base URL for the API
# - +request_headers+ -> any headers that you want to be globally applied
# - +version+ -> the version of the API you wish to access,
# currently only "v3" is supported
#
def initialize(api_key: '', host: nil, request_headers: nil, version: nil)
@api_key = api_key
@host = host ? host : 'https://api.sendgrid.com'
@version = version ? version : 'v3'
@user_agent = "sendgrid/#{SendGrid::VERSION};ruby"
@request_headers = JSON.parse('
def initialize(api_key: '', host: nil, request_headers: nil, version: nil, impersonate_subuser: nil)
@api_key = api_key
@host = host ? host : 'https://api.sendgrid.com'
@version = version ? version : 'v3'
@impersonate_subuser = impersonate_subuser
@user_agent = "sendgrid/#{SendGrid::VERSION};ruby"
@request_headers = JSON.parse('
{
"Authorization": "Bearer ' + @api_key + '",
"Accept": "application/json",
"User-agent": "' + @user_agent + '"
}
')
@request_headers['On-Behalf-Of'] = @impersonate_subuser if @impersonate_subuser


@request_headers = @request_headers.merge(request_headers) if request_headers
@client = Client.new(host: "#{@host}/#{@version}",
Expand Down
12 changes: 10 additions & 2 deletions test/sendgrid/test_sendgrid-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def test_init
"X-Test": "test"
}
')
sg = SendGrid::API.new(api_key: "SENDGRID_API_KEY", host: "https://api.test.com", request_headers: headers, version: "v3")
subuser = 'test_user'
sg = SendGrid::API.new(api_key: "SENDGRID_API_KEY", host: "https://api.test.com", request_headers: headers, version: "v3", impersonate_subuser: subuser)

assert_equal("https://api.test.com", sg.host)
user_agent = "sendgrid/#{SendGrid::VERSION};ruby"
Expand All @@ -50,15 +51,22 @@ def test_init
"Authorization": "Bearer SENDGRID_API_KEY",
"Accept": "application/json",
"X-Test": "test",
"User-agent": "' + user_agent + '"
"User-agent": "' + user_agent + '",
"On-Behalf-Of": "' + subuser + '"
}
')
assert_equal(test_headers, sg.request_headers)
assert_equal("v3", sg.version)
assert_equal(subuser, sg.impersonate_subuser)
assert_equal("5.3.0", SendGrid::VERSION)
assert_instance_of(SendGrid::Client, sg.client)
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_access_settings_activity_get
params = JSON.parse('{"limit": 1}')
headers = JSON.parse('{"X-Mock": 200}')
Expand Down