From c65c8302ed6dcd51982b619410500e401ae5499f Mon Sep 17 00:00:00 2001 From: manisha1997 Date: Fri, 17 Nov 2023 11:51:24 +0530 Subject: [PATCH 01/18] feat: geolocation setter in sendgrid-ruby for GDPR compliance --- examples/dataresidency/setregion.rb | 98 +++++++++++++++++++ lib/sendgrid/base_interface.rb | 19 ++++ lib/sendgrid/sendgrid.rb | 8 +- .../helpers/mail/test_data_residency.rb | 67 +++++++++++++ 4 files changed, 188 insertions(+), 4 deletions(-) create mode 100644 examples/dataresidency/setregion.rb create mode 100644 test/sendgrid/helpers/mail/test_data_residency.rb diff --git a/examples/dataresidency/setregion.rb b/examples/dataresidency/setregion.rb new file mode 100644 index 00000000..ab9ffd46 --- /dev/null +++ b/examples/dataresidency/setregion.rb @@ -0,0 +1,98 @@ +require sendgrid-ruby +include SendGrid + +# Example 1 +# Sending using "global" data residency + +from = SendGrid::Email.new(email: 'example@abc.com') +to = SendGrid::Email.new(email: 'example@abc.com') +subject = 'Sending with Twilio SendGrid is Fun' +content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') +mail = SendGrid::Mail.new(from, subject, to, content) +sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) +sg.set_data_residency("global") +puts sg.host +response = sg.client.mail._('send').post(request_body: mail.to_json) +puts response.status_code +puts response.body +puts response.headers + +# Example 2 +# Sending using "eu" data residency + +from = SendGrid::Email.new(email: 'example@abc.com') +to = SendGrid::Email.new(email: 'example@abc.com') +subject = 'Sending with Twilio SendGrid is Fun' +content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') +mail = SendGrid::Mail.new(from, subject, to, content) +sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) +sg.set_data_residency('eu') +puts sg.host +response = sg.client.mail._('send').post(request_body: mail.to_json) +puts response.status_code +puts response.body +puts response.headers + +# Example 3 +# Sending using no data residency + +from = SendGrid::Email.new(email: 'example@abc.com') +to = SendGrid::Email.new(email: 'example@abc.com') +subject = 'Sending with Twilio SendGrid is Fun' +content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') +mail = SendGrid::Mail.new(from, subject, to, content) +sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) +puts sg.host +response = sg.client.mail._('send').post(request_body: mail.to_json) +puts response.status_code +puts response.body +puts response.headers + +# Example 4 +# Sending using "global" data residency in constructor + +from = SendGrid::Email.new(email: 'example@abc.com') +to = SendGrid::Email.new(email: 'example@abc.com') +subject = 'Sending with Twilio SendGrid is Fun' +content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') +mail = SendGrid::Mail.new(from, subject, to, content) +sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'global') +puts sg.host +response = sg.client.mail._('send').post(request_body: mail.to_json) +puts response.status_code +puts response.body +puts response.headers + +# Example 5 +# Sending using "eu" data residency in constructor + +from = SendGrid::Email.new(email: 'example@abc.com') +to = SendGrid::Email.new(email: 'example@abc.com') +subject = 'Sending with Twilio SendGrid is Fun' +content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') +mail = SendGrid::Mail.new(from, subject, to, content) +sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'],region: 'eu') +puts sg.host +response = sg.client.mail._('send').post(request_body: mail.to_json) +puts response.status_code +puts response.body +puts response.headers + + + +# Example 6 +# Sending using "nil" data residency +# This will throw exception because data residency cannot be null + +from = SendGrid::Email.new(email: 'example@abc.com') +to = SendGrid::Email.new(email: 'example@abc.com') +subject = 'Sending with Twilio SendGrid is Fun' +content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') +mail = SendGrid::Mail.new(from, subject, to, content) +sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) +sg.set_data_residency(nil) +puts sg.host +response = sg.client.mail._('send').post(request_body: mail.to_json) +puts response.status_code +puts response.body +puts response.headers \ No newline at end of file diff --git a/lib/sendgrid/base_interface.rb b/lib/sendgrid/base_interface.rb index 0d5615a4..da430322 100644 --- a/lib/sendgrid/base_interface.rb +++ b/lib/sendgrid/base_interface.rb @@ -37,4 +37,23 @@ def initialize(auth:, host:, request_headers: nil, version: nil, impersonate_sub request_headers: @request_headers, http_options: @http_options) end + + def set_host(host) + @host = host + @client = SendGrid::Client.new(host: "#{@host}/#{@version}", + request_headers: @request_headers, + http_options: @http_options) + end + + def set_data_residency(region) + region_host_dict = {"eu" => 'https://api.eu.sendgrid.com',"global" => 'https://api.sendgrid.com'} + if( region == nil || !region_host_dict.has_key?(region) ) + raise ArgumentError, "region can only be \"eu\" or \"global\"" + end + @host = region_host_dict[region] + @client = SendGrid::Client.new(host: "#{@host}/#{@version}", + request_headers: @request_headers, + http_options: @http_options) + return region_host_dict[region] + end end diff --git a/lib/sendgrid/sendgrid.rb b/lib/sendgrid/sendgrid.rb index 03876d5a..54525d16 100644 --- a/lib/sendgrid/sendgrid.rb +++ b/lib/sendgrid/sendgrid.rb @@ -10,12 +10,12 @@ class API < BaseInterface # - +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 + # - +region+ -> data residency. The values only be either 'eu' or 'global'. # - def initialize(api_key:, host: nil, request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) + def initialize(api_key:, region: nil, 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, http_options: http_options) + host_with_data_residency = host == nil ? ( region == nil ? "https://api.sendgrid.com" : set_data_residency(region) ) : host + super(auth: auth, host: host_with_data_residency, request_headers: request_headers, version: version, impersonate_subuser: impersonate_subuser, http_options: http_options) end end end diff --git a/test/sendgrid/helpers/mail/test_data_residency.rb b/test/sendgrid/helpers/mail/test_data_residency.rb new file mode 100644 index 00000000..72f042e4 --- /dev/null +++ b/test/sendgrid/helpers/mail/test_data_residency.rb @@ -0,0 +1,67 @@ +require_relative '../../../../lib/sendgrid-ruby.rb' +require 'minitest/autorun' + +class TestDataResidency < Minitest::Test + include SendGrid + + def setup + @globalEmail = 'https://api.sendgrid.com' + @euEmail = 'https://api.eu.sendgrid.com' + end + def test_with_global_data_residency + sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) + sg.set_data_residency('global') + assert_equal @globalEmail,sg.host + end + + def test_with_global_data_residency_in_constructor + sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'],region: 'global') + assert_equal @globalEmail,sg.host + end + + def test_with_global_eu_residency + sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) + sg.set_data_residency('eu') + assert_equal @euEmail,sg.host + end + def test_with_global_eu_residency_in_constructor + sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'eu') + assert_equal @euEmail,sg.host + end + + def test_with_host_set_first_and_residency_set_second + sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'eu') + sg.set_host("https://api.test.sendgrid.com") + sg.set_data_residency("eu") + assert_equal @euEmail,sg.host + end + + def test_with_residency_set_first_and_host_set_second + sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'eu') + sg.set_data_residency("eu") + sg.set_host("https://api.test.sendgrid.com") + assert_equal "https://api.test.sendgrid.com",sg.host + end + + def test_with_global_nil_residency + sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) + assert_raises(ArgumentError) do + sg.set_data_residency(nil) + end + end + + def test_with_global_invalid_residency + sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) + assert_raises(ArgumentError) do + sg.set_data_residency("abc") + end + end + + def test_with_global_empty_residency + sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) + assert_raises(ArgumentError) do + sg.set_data_residency("") + end + end + +end From 721c1a8df609602c93bc856b7af910e1d810be7e Mon Sep 17 00:00:00 2001 From: manisha1997 Date: Fri, 17 Nov 2023 13:08:03 +0530 Subject: [PATCH 02/18] feat: geolocation setter in sendgrid-ruby for GDPR compliance --- examples/dataresidency/setregion.rb | 7 ++----- lib/sendgrid/base_interface.rb | 4 ++-- test/sendgrid/helpers/mail/test_data_residency.rb | 12 ++++++------ 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/examples/dataresidency/setregion.rb b/examples/dataresidency/setregion.rb index ab9ffd46..b4643724 100644 --- a/examples/dataresidency/setregion.rb +++ b/examples/dataresidency/setregion.rb @@ -1,5 +1,4 @@ -require sendgrid-ruby -include SendGrid +require_relative 'sendgrid-ruby' # Example 1 # Sending using "global" data residency @@ -71,15 +70,13 @@ subject = 'Sending with Twilio SendGrid is Fun' content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') mail = SendGrid::Mail.new(from, subject, to, content) -sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'],region: 'eu') +sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'eu') puts sg.host response = sg.client.mail._('send').post(request_body: mail.to_json) puts response.status_code puts response.body puts response.headers - - # Example 6 # Sending using "nil" data residency # This will throw exception because data residency cannot be null diff --git a/lib/sendgrid/base_interface.rb b/lib/sendgrid/base_interface.rb index da430322..429c95df 100644 --- a/lib/sendgrid/base_interface.rb +++ b/lib/sendgrid/base_interface.rb @@ -46,8 +46,8 @@ def set_host(host) end def set_data_residency(region) - region_host_dict = {"eu" => 'https://api.eu.sendgrid.com',"global" => 'https://api.sendgrid.com'} - if( region == nil || !region_host_dict.has_key?(region) ) + region_host_dict = { "eu" => 'https://api.eu.sendgrid.com', "global" => 'https://api.sendgrid.com' } + if ( region.nil? || !region_host_dict.has_key?(region) ) raise ArgumentError, "region can only be \"eu\" or \"global\"" end @host = region_host_dict[region] diff --git a/test/sendgrid/helpers/mail/test_data_residency.rb b/test/sendgrid/helpers/mail/test_data_residency.rb index 72f042e4..9b4f3bd6 100644 --- a/test/sendgrid/helpers/mail/test_data_residency.rb +++ b/test/sendgrid/helpers/mail/test_data_residency.rb @@ -11,36 +11,36 @@ def setup def test_with_global_data_residency sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) sg.set_data_residency('global') - assert_equal @globalEmail,sg.host + assert_equal @globalEmail, sg.host end def test_with_global_data_residency_in_constructor sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'],region: 'global') - assert_equal @globalEmail,sg.host + assert_equal @globalEmail, sg.host end def test_with_global_eu_residency sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) sg.set_data_residency('eu') - assert_equal @euEmail,sg.host + assert_equal @euEmail, sg.host end def test_with_global_eu_residency_in_constructor sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'eu') - assert_equal @euEmail,sg.host + assert_equal @euEmail, sg.host end def test_with_host_set_first_and_residency_set_second sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'eu') sg.set_host("https://api.test.sendgrid.com") sg.set_data_residency("eu") - assert_equal @euEmail,sg.host + assert_equal @euEmail, sg.host end def test_with_residency_set_first_and_host_set_second sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'eu') sg.set_data_residency("eu") sg.set_host("https://api.test.sendgrid.com") - assert_equal "https://api.test.sendgrid.com",sg.host + assert_equal "https://api.test.sendgrid.com", sg.host end def test_with_global_nil_residency From 48d94fb1eb0395c56b8df7cafacf03b400f43dba Mon Sep 17 00:00:00 2001 From: manisha1997 Date: Fri, 17 Nov 2023 15:27:51 +0530 Subject: [PATCH 03/18] feat: geolocation setter in sendgrid-ruby for GDPR compliance --- examples/dataresidency/setregion.rb | 10 +++++----- lib/sendgrid/base_interface.rb | 4 ++-- lib/sendgrid/sendgrid.rb | 2 +- .../helpers/mail/test_data_residency.rb | 20 +++++++++---------- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/examples/dataresidency/setregion.rb b/examples/dataresidency/setregion.rb index b4643724..bbf0010c 100644 --- a/examples/dataresidency/setregion.rb +++ b/examples/dataresidency/setregion.rb @@ -1,4 +1,4 @@ -require_relative 'sendgrid-ruby' +require 'sendgrid-ruby' # Example 1 # Sending using "global" data residency @@ -9,7 +9,7 @@ content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') mail = SendGrid::Mail.new(from, subject, to, content) sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) -sg.set_data_residency("global") +sg.data_residency("global") puts sg.host response = sg.client.mail._('send').post(request_body: mail.to_json) puts response.status_code @@ -25,7 +25,7 @@ content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') mail = SendGrid::Mail.new(from, subject, to, content) sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) -sg.set_data_residency('eu') +sg.data_residency('eu') puts sg.host response = sg.client.mail._('send').post(request_body: mail.to_json) puts response.status_code @@ -87,9 +87,9 @@ content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') mail = SendGrid::Mail.new(from, subject, to, content) sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) -sg.set_data_residency(nil) +sg.data_residency(nil) puts sg.host response = sg.client.mail._('send').post(request_body: mail.to_json) puts response.status_code puts response.body -puts response.headers \ No newline at end of file +puts response.headers diff --git a/lib/sendgrid/base_interface.rb b/lib/sendgrid/base_interface.rb index 429c95df..6fbd1e60 100644 --- a/lib/sendgrid/base_interface.rb +++ b/lib/sendgrid/base_interface.rb @@ -38,14 +38,14 @@ def initialize(auth:, host:, request_headers: nil, version: nil, impersonate_sub http_options: @http_options) end - def set_host(host) + def setHost(host) @host = host @client = SendGrid::Client.new(host: "#{@host}/#{@version}", request_headers: @request_headers, http_options: @http_options) end - def set_data_residency(region) + def data_residency(region) region_host_dict = { "eu" => 'https://api.eu.sendgrid.com', "global" => 'https://api.sendgrid.com' } if ( region.nil? || !region_host_dict.has_key?(region) ) raise ArgumentError, "region can only be \"eu\" or \"global\"" diff --git a/lib/sendgrid/sendgrid.rb b/lib/sendgrid/sendgrid.rb index 54525d16..c8359dec 100644 --- a/lib/sendgrid/sendgrid.rb +++ b/lib/sendgrid/sendgrid.rb @@ -14,7 +14,7 @@ class API < BaseInterface # def initialize(api_key:, region: nil, host: nil, request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) auth = "Bearer #{api_key}" - host_with_data_residency = host == nil ? ( region == nil ? "https://api.sendgrid.com" : set_data_residency(region) ) : host + host_with_data_residency = host == nil ? ( region == nil ? "https://api.sendgrid.com" : data_residency(region) ) : host super(auth: auth, host: host_with_data_residency, request_headers: request_headers, version: version, impersonate_subuser: impersonate_subuser, http_options: http_options) end end diff --git a/test/sendgrid/helpers/mail/test_data_residency.rb b/test/sendgrid/helpers/mail/test_data_residency.rb index 9b4f3bd6..7a617ad6 100644 --- a/test/sendgrid/helpers/mail/test_data_residency.rb +++ b/test/sendgrid/helpers/mail/test_data_residency.rb @@ -10,18 +10,18 @@ def setup end def test_with_global_data_residency sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) - sg.set_data_residency('global') + sg.data_residency('global') assert_equal @globalEmail, sg.host end def test_with_global_data_residency_in_constructor - sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'],region: 'global') + sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'global') assert_equal @globalEmail, sg.host end def test_with_global_eu_residency sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) - sg.set_data_residency('eu') + sg.data_residency('eu') assert_equal @euEmail, sg.host end def test_with_global_eu_residency_in_constructor @@ -31,36 +31,36 @@ def test_with_global_eu_residency_in_constructor def test_with_host_set_first_and_residency_set_second sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'eu') - sg.set_host("https://api.test.sendgrid.com") - sg.set_data_residency("eu") + sg.setHost("https://api.test.sendgrid.com") + sg.data_residency("eu") assert_equal @euEmail, sg.host end def test_with_residency_set_first_and_host_set_second sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'eu') - sg.set_data_residency("eu") - sg.set_host("https://api.test.sendgrid.com") + sg.data_residency("eu") + sg.setHost("https://api.test.sendgrid.com") assert_equal "https://api.test.sendgrid.com", sg.host end def test_with_global_nil_residency sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) assert_raises(ArgumentError) do - sg.set_data_residency(nil) + sg.data_residency(nil) end end def test_with_global_invalid_residency sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) assert_raises(ArgumentError) do - sg.set_data_residency("abc") + sg.data_residency("abc") end end def test_with_global_empty_residency sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) assert_raises(ArgumentError) do - sg.set_data_residency("") + sg.data_residency("") end end From 7ea9c856107b213163564cb34e85c7891529edc3 Mon Sep 17 00:00:00 2001 From: manisha1997 Date: Fri, 17 Nov 2023 16:24:00 +0530 Subject: [PATCH 04/18] feat: geolocation setter in sendgrid-ruby for GDPR compliance --- examples/dataresidency/setregion.rb | 37 +++++++++++-------- lib/sendgrid/base_interface.rb | 13 +++++-- lib/sendgrid/sendgrid.rb | 5 +-- .../helpers/mail/test_data_residency.rb | 22 ++++++----- 4 files changed, 44 insertions(+), 33 deletions(-) diff --git a/examples/dataresidency/setregion.rb b/examples/dataresidency/setregion.rb index bbf0010c..2481de29 100644 --- a/examples/dataresidency/setregion.rb +++ b/examples/dataresidency/setregion.rb @@ -1,10 +1,13 @@ -require 'sendgrid-ruby' +# frozen_string_literal: true +#require '/Users/manisingh/github/sendgrid/sendgrid-ruby' +require_relative '/Users/manisingh/github/sendgrid/sendgrid-ruby/lib/sendgrid-ruby.rb' +include SendGrid # Example 1 # Sending using "global" data residency -from = SendGrid::Email.new(email: 'example@abc.com') -to = SendGrid::Email.new(email: 'example@abc.com') +from = SendGrid::Email.new(email: 'manisingh@twilio.com') +to = SendGrid::Email.new(email: 'manisingh@twilio.com') subject = 'Sending with Twilio SendGrid is Fun' content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') mail = SendGrid::Mail.new(from, subject, to, content) @@ -19,12 +22,12 @@ # Example 2 # Sending using "eu" data residency -from = SendGrid::Email.new(email: 'example@abc.com') -to = SendGrid::Email.new(email: 'example@abc.com') +from = SendGrid::Email.new(email: 'sburman@twilio.com') +to = SendGrid::Email.new(email: 'sburman@twilio.com') subject = 'Sending with Twilio SendGrid is Fun' content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') mail = SendGrid::Mail.new(from, subject, to, content) -sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) +sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY_EU']) sg.data_residency('eu') puts sg.host response = sg.client.mail._('send').post(request_body: mail.to_json) @@ -35,8 +38,8 @@ # Example 3 # Sending using no data residency -from = SendGrid::Email.new(email: 'example@abc.com') -to = SendGrid::Email.new(email: 'example@abc.com') +from = SendGrid::Email.new(email: 'manisingh@twilio.com') +to = SendGrid::Email.new(email: 'manisingh@twilio.com') subject = 'Sending with Twilio SendGrid is Fun' content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') mail = SendGrid::Mail.new(from, subject, to, content) @@ -50,8 +53,8 @@ # Example 4 # Sending using "global" data residency in constructor -from = SendGrid::Email.new(email: 'example@abc.com') -to = SendGrid::Email.new(email: 'example@abc.com') +from = SendGrid::Email.new(email: 'manisingh@twilio.com') +to = SendGrid::Email.new(email: 'manisingh@twilio.com') subject = 'Sending with Twilio SendGrid is Fun' content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') mail = SendGrid::Mail.new(from, subject, to, content) @@ -65,24 +68,26 @@ # Example 5 # Sending using "eu" data residency in constructor -from = SendGrid::Email.new(email: 'example@abc.com') -to = SendGrid::Email.new(email: 'example@abc.com') +from = SendGrid::Email.new(email: 'sburman@twilio.com') +to = SendGrid::Email.new(email: 'sburman@twilio.com') subject = 'Sending with Twilio SendGrid is Fun' content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') mail = SendGrid::Mail.new(from, subject, to, content) -sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'eu') +sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY_EU'],region: 'eu') puts sg.host response = sg.client.mail._('send').post(request_body: mail.to_json) puts response.status_code puts response.body puts response.headers + + # Example 6 # Sending using "nil" data residency # This will throw exception because data residency cannot be null -from = SendGrid::Email.new(email: 'example@abc.com') -to = SendGrid::Email.new(email: 'example@abc.com') +from = SendGrid::Email.new(email: 'manisingh@twilio.com') +to = SendGrid::Email.new(email: 'manisingh@twilio.com') subject = 'Sending with Twilio SendGrid is Fun' content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') mail = SendGrid::Mail.new(from, subject, to, content) @@ -92,4 +97,4 @@ response = sg.client.mail._('send').post(request_body: mail.to_json) puts response.status_code puts response.body -puts response.headers +puts response.headers \ No newline at end of file diff --git a/lib/sendgrid/base_interface.rb b/lib/sendgrid/base_interface.rb index 6fbd1e60..657443a6 100644 --- a/lib/sendgrid/base_interface.rb +++ b/lib/sendgrid/base_interface.rb @@ -16,9 +16,13 @@ class BaseInterface # 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, http_options: {}) + def initialize(auth:, host:, region:'global', request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) @auth = auth - @host = host + if not host.nil? + update_host(host) + else + data_residency(region) + end @version = version || 'v3' @impersonate_subuser = impersonate_subuser @user_agent = "sendgrid/#{SendGrid::VERSION};ruby" @@ -38,7 +42,7 @@ def initialize(auth:, host:, request_headers: nil, version: nil, impersonate_sub http_options: @http_options) end - def setHost(host) + def update_host(host) @host = host @client = SendGrid::Client.new(host: "#{@host}/#{@version}", request_headers: @request_headers, @@ -47,9 +51,10 @@ def setHost(host) def data_residency(region) region_host_dict = { "eu" => 'https://api.eu.sendgrid.com', "global" => 'https://api.sendgrid.com' } - if ( region.nil? || !region_host_dict.has_key?(region) ) + if region.nil? || !region_host_dict.key?(region) raise ArgumentError, "region can only be \"eu\" or \"global\"" end + @host = region_host_dict[region] @client = SendGrid::Client.new(host: "#{@host}/#{@version}", request_headers: @request_headers, diff --git a/lib/sendgrid/sendgrid.rb b/lib/sendgrid/sendgrid.rb index c8359dec..ce882147 100644 --- a/lib/sendgrid/sendgrid.rb +++ b/lib/sendgrid/sendgrid.rb @@ -12,10 +12,9 @@ class API < BaseInterface # - +http_options+ -> http options that you want to be globally applied to each request # - +region+ -> data residency. The values only be either 'eu' or 'global'. # - def initialize(api_key:, region: nil, host: nil, request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) + def initialize(api_key:, host: nil,region: 'global', request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) auth = "Bearer #{api_key}" - host_with_data_residency = host == nil ? ( region == nil ? "https://api.sendgrid.com" : data_residency(region) ) : host - super(auth: auth, host: host_with_data_residency, request_headers: request_headers, version: version, impersonate_subuser: impersonate_subuser, http_options: http_options) + super(auth: auth, host: host,region:region, request_headers: request_headers, version: version, impersonate_subuser: impersonate_subuser, http_options: http_options) end end end diff --git a/test/sendgrid/helpers/mail/test_data_residency.rb b/test/sendgrid/helpers/mail/test_data_residency.rb index 7a617ad6..bcb2695c 100644 --- a/test/sendgrid/helpers/mail/test_data_residency.rb +++ b/test/sendgrid/helpers/mail/test_data_residency.rb @@ -1,45 +1,47 @@ -require_relative '../../../../lib/sendgrid-ruby.rb' +require_relative '../../../../lib/sendgrid-ruby' require 'minitest/autorun' class TestDataResidency < Minitest::Test include SendGrid def setup - @globalEmail = 'https://api.sendgrid.com' - @euEmail = 'https://api.eu.sendgrid.com' + @global_email = 'https://api.sendgrid.com' + @eu_email = 'https://api.eu.sendgrid.com' end + def test_with_global_data_residency sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) sg.data_residency('global') - assert_equal @globalEmail, sg.host + assert_equal @global_email, sg.host end def test_with_global_data_residency_in_constructor sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'global') - assert_equal @globalEmail, sg.host + assert_equal @global_email, sg.host end def test_with_global_eu_residency sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) sg.data_residency('eu') - assert_equal @euEmail, sg.host + assert_equal @eu_email, sg.host end + def test_with_global_eu_residency_in_constructor sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'eu') - assert_equal @euEmail, sg.host + assert_equal @eu_email, sg.host end def test_with_host_set_first_and_residency_set_second sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'eu') - sg.setHost("https://api.test.sendgrid.com") + sg.update_host("https://api.test.sendgrid.com") sg.data_residency("eu") - assert_equal @euEmail, sg.host + assert_equal @eu_email, sg.host end def test_with_residency_set_first_and_host_set_second sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'eu') sg.data_residency("eu") - sg.setHost("https://api.test.sendgrid.com") + sg.update_host("https://api.test.sendgrid.com") assert_equal "https://api.test.sendgrid.com", sg.host end From bb2d0b3df1a3ac7d3d365b68aec73c5b06e77c02 Mon Sep 17 00:00:00 2001 From: manisha1997 Date: Fri, 17 Nov 2023 16:30:00 +0530 Subject: [PATCH 05/18] feat: geolocation setter in sendgrid-ruby for GDPR compliance --- examples/dataresidency/setregion.rb | 37 ++++++++----------- lib/sendgrid/base_interface.rb | 5 +-- lib/sendgrid/sendgrid.rb | 4 +- .../helpers/mail/test_data_residency.rb | 1 - 4 files changed, 20 insertions(+), 27 deletions(-) diff --git a/examples/dataresidency/setregion.rb b/examples/dataresidency/setregion.rb index 2481de29..bbf0010c 100644 --- a/examples/dataresidency/setregion.rb +++ b/examples/dataresidency/setregion.rb @@ -1,13 +1,10 @@ -# frozen_string_literal: true -#require '/Users/manisingh/github/sendgrid/sendgrid-ruby' -require_relative '/Users/manisingh/github/sendgrid/sendgrid-ruby/lib/sendgrid-ruby.rb' -include SendGrid +require 'sendgrid-ruby' # Example 1 # Sending using "global" data residency -from = SendGrid::Email.new(email: 'manisingh@twilio.com') -to = SendGrid::Email.new(email: 'manisingh@twilio.com') +from = SendGrid::Email.new(email: 'example@abc.com') +to = SendGrid::Email.new(email: 'example@abc.com') subject = 'Sending with Twilio SendGrid is Fun' content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') mail = SendGrid::Mail.new(from, subject, to, content) @@ -22,12 +19,12 @@ # Example 2 # Sending using "eu" data residency -from = SendGrid::Email.new(email: 'sburman@twilio.com') -to = SendGrid::Email.new(email: 'sburman@twilio.com') +from = SendGrid::Email.new(email: 'example@abc.com') +to = SendGrid::Email.new(email: 'example@abc.com') subject = 'Sending with Twilio SendGrid is Fun' content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') mail = SendGrid::Mail.new(from, subject, to, content) -sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY_EU']) +sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) sg.data_residency('eu') puts sg.host response = sg.client.mail._('send').post(request_body: mail.to_json) @@ -38,8 +35,8 @@ # Example 3 # Sending using no data residency -from = SendGrid::Email.new(email: 'manisingh@twilio.com') -to = SendGrid::Email.new(email: 'manisingh@twilio.com') +from = SendGrid::Email.new(email: 'example@abc.com') +to = SendGrid::Email.new(email: 'example@abc.com') subject = 'Sending with Twilio SendGrid is Fun' content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') mail = SendGrid::Mail.new(from, subject, to, content) @@ -53,8 +50,8 @@ # Example 4 # Sending using "global" data residency in constructor -from = SendGrid::Email.new(email: 'manisingh@twilio.com') -to = SendGrid::Email.new(email: 'manisingh@twilio.com') +from = SendGrid::Email.new(email: 'example@abc.com') +to = SendGrid::Email.new(email: 'example@abc.com') subject = 'Sending with Twilio SendGrid is Fun' content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') mail = SendGrid::Mail.new(from, subject, to, content) @@ -68,26 +65,24 @@ # Example 5 # Sending using "eu" data residency in constructor -from = SendGrid::Email.new(email: 'sburman@twilio.com') -to = SendGrid::Email.new(email: 'sburman@twilio.com') +from = SendGrid::Email.new(email: 'example@abc.com') +to = SendGrid::Email.new(email: 'example@abc.com') subject = 'Sending with Twilio SendGrid is Fun' content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') mail = SendGrid::Mail.new(from, subject, to, content) -sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY_EU'],region: 'eu') +sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'eu') puts sg.host response = sg.client.mail._('send').post(request_body: mail.to_json) puts response.status_code puts response.body puts response.headers - - # Example 6 # Sending using "nil" data residency # This will throw exception because data residency cannot be null -from = SendGrid::Email.new(email: 'manisingh@twilio.com') -to = SendGrid::Email.new(email: 'manisingh@twilio.com') +from = SendGrid::Email.new(email: 'example@abc.com') +to = SendGrid::Email.new(email: 'example@abc.com') subject = 'Sending with Twilio SendGrid is Fun' content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') mail = SendGrid::Mail.new(from, subject, to, content) @@ -97,4 +92,4 @@ response = sg.client.mail._('send').post(request_body: mail.to_json) puts response.status_code puts response.body -puts response.headers \ No newline at end of file +puts response.headers diff --git a/lib/sendgrid/base_interface.rb b/lib/sendgrid/base_interface.rb index 657443a6..6555b18a 100644 --- a/lib/sendgrid/base_interface.rb +++ b/lib/sendgrid/base_interface.rb @@ -16,9 +16,9 @@ class BaseInterface # in the "On-Behalf-Of" header # - +http_options+ -> http options that you want to be globally applied to each request # - def initialize(auth:, host:, region:'global', request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) + def initialize(auth:, host:, region: 'global', request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) @auth = auth - if not host.nil? + if !host.nil? update_host(host) else data_residency(region) @@ -59,6 +59,5 @@ def data_residency(region) @client = SendGrid::Client.new(host: "#{@host}/#{@version}", request_headers: @request_headers, http_options: @http_options) - return region_host_dict[region] end end diff --git a/lib/sendgrid/sendgrid.rb b/lib/sendgrid/sendgrid.rb index ce882147..7d1c6fa7 100644 --- a/lib/sendgrid/sendgrid.rb +++ b/lib/sendgrid/sendgrid.rb @@ -12,9 +12,9 @@ class API < BaseInterface # - +http_options+ -> http options that you want to be globally applied to each request # - +region+ -> data residency. The values only be either 'eu' or 'global'. # - def initialize(api_key:, host: nil,region: 'global', request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) + def initialize(api_key:, host: nil, region: 'global', request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) auth = "Bearer #{api_key}" - super(auth: auth, host: host,region:region, request_headers: request_headers, version: version, impersonate_subuser: impersonate_subuser, http_options: http_options) + super(auth: auth, host: host, region:region, request_headers: request_headers, version: version, impersonate_subuser: impersonate_subuser, http_options: http_options) end end end diff --git a/test/sendgrid/helpers/mail/test_data_residency.rb b/test/sendgrid/helpers/mail/test_data_residency.rb index bcb2695c..b3820953 100644 --- a/test/sendgrid/helpers/mail/test_data_residency.rb +++ b/test/sendgrid/helpers/mail/test_data_residency.rb @@ -65,5 +65,4 @@ def test_with_global_empty_residency sg.data_residency("") end end - end From 6c8d6d5ae3a115a743b1c67b98c0848d0fc6e5d0 Mon Sep 17 00:00:00 2001 From: manisha1997 Date: Fri, 17 Nov 2023 16:35:29 +0530 Subject: [PATCH 06/18] feat: geolocation setter in sendgrid-ruby for GDPR compliance --- lib/sendgrid/base_interface.rb | 4 +--- lib/sendgrid/sendgrid.rb | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/sendgrid/base_interface.rb b/lib/sendgrid/base_interface.rb index 6555b18a..cec0dafa 100644 --- a/lib/sendgrid/base_interface.rb +++ b/lib/sendgrid/base_interface.rb @@ -51,9 +51,7 @@ def update_host(host) def data_residency(region) region_host_dict = { "eu" => 'https://api.eu.sendgrid.com', "global" => 'https://api.sendgrid.com' } - if region.nil? || !region_host_dict.key?(region) - raise ArgumentError, "region can only be \"eu\" or \"global\"" - end + raise ArgumentError, "region can only be \"eu\" or \"global\"" if region.nil? || !region_host_dict.key?(region) @host = region_host_dict[region] @client = SendGrid::Client.new(host: "#{@host}/#{@version}", diff --git a/lib/sendgrid/sendgrid.rb b/lib/sendgrid/sendgrid.rb index 7d1c6fa7..c1f59a20 100644 --- a/lib/sendgrid/sendgrid.rb +++ b/lib/sendgrid/sendgrid.rb @@ -14,7 +14,7 @@ class API < BaseInterface # def initialize(api_key:, host: nil, region: 'global', request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) auth = "Bearer #{api_key}" - super(auth: auth, host: host, region:region, request_headers: request_headers, version: version, impersonate_subuser: impersonate_subuser, http_options: http_options) + super(auth: auth, host: host, region: region, request_headers: request_headers, version: version, impersonate_subuser: impersonate_subuser, http_options: http_options) end end end From cf0165acf78c424c5f3a9120847600f5bdf78034 Mon Sep 17 00:00:00 2001 From: manisha1997 Date: Mon, 20 Nov 2023 14:55:59 +0530 Subject: [PATCH 07/18] feat: geolocation setter in sendgrid-ruby for GDPR compliance --- examples/dataresidency/setregion.rb | 25 +++++++++++++++---- lib/sendgrid/base_interface.rb | 15 +++++------ lib/sendgrid/sendgrid.rb | 2 +- .../helpers/mail/test_data_residency.rb | 23 ++++++++++------- 4 files changed, 43 insertions(+), 22 deletions(-) diff --git a/examples/dataresidency/setregion.rb b/examples/dataresidency/setregion.rb index bbf0010c..5f922a69 100644 --- a/examples/dataresidency/setregion.rb +++ b/examples/dataresidency/setregion.rb @@ -9,7 +9,6 @@ content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') mail = SendGrid::Mail.new(from, subject, to, content) sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) -sg.data_residency("global") puts sg.host response = sg.client.mail._('send').post(request_body: mail.to_json) puts response.status_code @@ -24,8 +23,8 @@ subject = 'Sending with Twilio SendGrid is Fun' content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') mail = SendGrid::Mail.new(from, subject, to, content) -sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) -sg.data_residency('eu') +sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY_EU']) +sg.data_residency(region: 'eu') puts sg.host response = sg.client.mail._('send').post(request_body: mail.to_json) puts response.status_code @@ -70,7 +69,7 @@ subject = 'Sending with Twilio SendGrid is Fun' content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') mail = SendGrid::Mail.new(from, subject, to, content) -sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'eu') +sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY_EU'], region: 'eu') puts sg.host response = sg.client.mail._('send').post(request_body: mail.to_json) puts response.status_code @@ -81,13 +80,29 @@ # Sending using "nil" data residency # This will throw exception because data residency cannot be null +from = SendGrid::Email.new(email: 'example@abc.com') +to = SendGrid::Email.new(email: 'example@abc.com') +subject = 'Sending with Twilio SendGrid is Fun' +content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') +mail = SendGrid::Mail.new(from, subject, to, content) +sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY_EU']) +sg.data_residency(region: nil) +puts sg.host +response = sg.client.mail._('send').post(request_body: mail.to_json) +puts response.status_code +puts response.body +puts response.headers + +# Example 7 +# First region, then host from = SendGrid::Email.new(email: 'example@abc.com') to = SendGrid::Email.new(email: 'example@abc.com') subject = 'Sending with Twilio SendGrid is Fun' content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') mail = SendGrid::Mail.new(from, subject, to, content) sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) -sg.data_residency(nil) +sg.data_residency(region: "eu") +sg.update_host(host: "https://api.sendgrid.com") puts sg.host response = sg.client.mail._('send').post(request_body: mail.to_json) puts response.status_code diff --git a/lib/sendgrid/base_interface.rb b/lib/sendgrid/base_interface.rb index cec0dafa..e318e8a7 100644 --- a/lib/sendgrid/base_interface.rb +++ b/lib/sendgrid/base_interface.rb @@ -16,12 +16,14 @@ class BaseInterface # in the "On-Behalf-Of" header # - +http_options+ -> http options that you want to be globally applied to each request # - def initialize(auth:, host:, region: 'global', request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) + def initialize(auth:, host: nil, region: nil, request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) @auth = auth - if !host.nil? - update_host(host) + if host.nil? && region.nil? + @host="https://api.sendgrid.com" + elsif !region.nil? + data_residency(region: region) else - data_residency(region) + update_host(host: host) end @version = version || 'v3' @impersonate_subuser = impersonate_subuser @@ -42,14 +44,13 @@ def initialize(auth:, host:, region: 'global', request_headers: nil, version: ni http_options: @http_options) end - def update_host(host) + def update_host(host:) @host = host @client = SendGrid::Client.new(host: "#{@host}/#{@version}", request_headers: @request_headers, http_options: @http_options) end - - def data_residency(region) + def data_residency(region:) region_host_dict = { "eu" => 'https://api.eu.sendgrid.com', "global" => 'https://api.sendgrid.com' } raise ArgumentError, "region can only be \"eu\" or \"global\"" if region.nil? || !region_host_dict.key?(region) diff --git a/lib/sendgrid/sendgrid.rb b/lib/sendgrid/sendgrid.rb index c1f59a20..70208d9a 100644 --- a/lib/sendgrid/sendgrid.rb +++ b/lib/sendgrid/sendgrid.rb @@ -12,7 +12,7 @@ class API < BaseInterface # - +http_options+ -> http options that you want to be globally applied to each request # - +region+ -> data residency. The values only be either 'eu' or 'global'. # - def initialize(api_key:, host: nil, region: 'global', request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) + def initialize(api_key:, host: nil, region: nil, request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) auth = "Bearer #{api_key}" super(auth: auth, host: host, region: region, request_headers: request_headers, version: version, impersonate_subuser: impersonate_subuser, http_options: http_options) end diff --git a/test/sendgrid/helpers/mail/test_data_residency.rb b/test/sendgrid/helpers/mail/test_data_residency.rb index b3820953..e5eaaf4f 100644 --- a/test/sendgrid/helpers/mail/test_data_residency.rb +++ b/test/sendgrid/helpers/mail/test_data_residency.rb @@ -11,7 +11,7 @@ def setup def test_with_global_data_residency sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) - sg.data_residency('global') + sg.data_residency(region: 'global') assert_equal @global_email, sg.host end @@ -22,7 +22,7 @@ def test_with_global_data_residency_in_constructor def test_with_global_eu_residency sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) - sg.data_residency('eu') + sg.data_residency(region: 'eu') assert_equal @eu_email, sg.host end @@ -33,36 +33,41 @@ def test_with_global_eu_residency_in_constructor def test_with_host_set_first_and_residency_set_second sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'eu') - sg.update_host("https://api.test.sendgrid.com") - sg.data_residency("eu") + sg.update_host(host: "https://api.test.sendgrid.com") + sg.data_residency(region: "eu") assert_equal @eu_email, sg.host end def test_with_residency_set_first_and_host_set_second sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'eu') - sg.data_residency("eu") - sg.update_host("https://api.test.sendgrid.com") + sg.data_residency(region: "eu") + sg.update_host(host: "https://api.test.sendgrid.com") assert_equal "https://api.test.sendgrid.com", sg.host end def test_with_global_nil_residency sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) assert_raises(ArgumentError) do - sg.data_residency(nil) + sg.data_residency(region: nil) end end def test_with_global_invalid_residency sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) assert_raises(ArgumentError) do - sg.data_residency("abc") + sg.data_residency(region: "abc") end end def test_with_global_empty_residency sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) assert_raises(ArgumentError) do - sg.data_residency("") + sg.data_residency(region: "") end end + + def test_host_with_both_host_and_region_in_constructor + sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'],host: "https://example.com",region: "eu") + assert_equal @eu_email, sg.host + end end From 66f8b23dcd42941aa43fd140fcd868cba90b9dd4 Mon Sep 17 00:00:00 2001 From: manisha1997 Date: Mon, 20 Nov 2023 15:06:25 +0530 Subject: [PATCH 08/18] feat: geolocation setter in sendgrid-ruby for GDPR compliance --- lib/sendgrid/base_interface.rb | 2 +- test/sendgrid/helpers/mail/test_data_residency.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/sendgrid/base_interface.rb b/lib/sendgrid/base_interface.rb index e318e8a7..0ef3f9ee 100644 --- a/lib/sendgrid/base_interface.rb +++ b/lib/sendgrid/base_interface.rb @@ -19,7 +19,7 @@ class BaseInterface def initialize(auth:, host: nil, region: nil, request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) @auth = auth if host.nil? && region.nil? - @host="https://api.sendgrid.com" + @host = "https://api.sendgrid.com" elsif !region.nil? data_residency(region: region) else diff --git a/test/sendgrid/helpers/mail/test_data_residency.rb b/test/sendgrid/helpers/mail/test_data_residency.rb index e5eaaf4f..55627d18 100644 --- a/test/sendgrid/helpers/mail/test_data_residency.rb +++ b/test/sendgrid/helpers/mail/test_data_residency.rb @@ -67,7 +67,7 @@ def test_with_global_empty_residency end def test_host_with_both_host_and_region_in_constructor - sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'],host: "https://example.com",region: "eu") + sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], host: "https://example.com", region: "eu") assert_equal @eu_email, sg.host end end From 7023f8547f6e7dd877a2e4fa46bc8f5115bf63c8 Mon Sep 17 00:00:00 2001 From: manisha1997 Date: Mon, 20 Nov 2023 15:07:40 +0530 Subject: [PATCH 09/18] feat: geolocation setter in sendgrid-ruby for GDPR compliance --- lib/sendgrid/base_interface.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/sendgrid/base_interface.rb b/lib/sendgrid/base_interface.rb index 0ef3f9ee..f6c51cca 100644 --- a/lib/sendgrid/base_interface.rb +++ b/lib/sendgrid/base_interface.rb @@ -50,6 +50,7 @@ def update_host(host:) request_headers: @request_headers, http_options: @http_options) end + def data_residency(region:) region_host_dict = { "eu" => 'https://api.eu.sendgrid.com', "global" => 'https://api.sendgrid.com' } raise ArgumentError, "region can only be \"eu\" or \"global\"" if region.nil? || !region_host_dict.key?(region) From 4f0bf595db05f06a839e02d0d71df2d0c822f72e Mon Sep 17 00:00:00 2001 From: manisha1997 Date: Tue, 21 Nov 2023 12:24:53 +0530 Subject: [PATCH 10/18] feat: geolocation setter in sendgrid-ruby for GDPR compliance --- lib/sendgrid/base_interface.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/sendgrid/base_interface.rb b/lib/sendgrid/base_interface.rb index f6c51cca..7ceedaed 100644 --- a/lib/sendgrid/base_interface.rb +++ b/lib/sendgrid/base_interface.rb @@ -16,14 +16,14 @@ class BaseInterface # in the "On-Behalf-Of" header # - +http_options+ -> http options that you want to be globally applied to each request # - def initialize(auth:, host: nil, region: nil, request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) + def initialize(auth:, host: , region: nil, request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) @auth = auth if host.nil? && region.nil? @host = "https://api.sendgrid.com" elsif !region.nil? data_residency(region: region) else - update_host(host: host) + @host = host end @version = version || 'v3' @impersonate_subuser = impersonate_subuser From ffe788e7cf5cafc246247b757f022edf940e2b82 Mon Sep 17 00:00:00 2001 From: manisha1997 Date: Tue, 21 Nov 2023 12:28:10 +0530 Subject: [PATCH 11/18] feat: geolocation setter in sendgrid-ruby for GDPR compliance --- lib/sendgrid/base_interface.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sendgrid/base_interface.rb b/lib/sendgrid/base_interface.rb index 7ceedaed..2708152f 100644 --- a/lib/sendgrid/base_interface.rb +++ b/lib/sendgrid/base_interface.rb @@ -16,7 +16,7 @@ class BaseInterface # in the "On-Behalf-Of" header # - +http_options+ -> http options that you want to be globally applied to each request # - def initialize(auth:, host: , region: nil, request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) + def initialize(auth:, host:, region: nil, request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) @auth = auth if host.nil? && region.nil? @host = "https://api.sendgrid.com" From d976973044dafed69389d56dacd33afebc41ffc7 Mon Sep 17 00:00:00 2001 From: manisha1997 Date: Wed, 22 Nov 2023 11:52:37 +0530 Subject: [PATCH 12/18] feat: geolocation setter in sendgrid-ruby for GDPR compliance --- lib/sendgrid/sendgrid.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/sendgrid/sendgrid.rb b/lib/sendgrid/sendgrid.rb index 70208d9a..ef08d6d2 100644 --- a/lib/sendgrid/sendgrid.rb +++ b/lib/sendgrid/sendgrid.rb @@ -14,6 +14,7 @@ class API < BaseInterface # def initialize(api_key:, host: nil, region: 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, region: region, request_headers: request_headers, version: version, impersonate_subuser: impersonate_subuser, http_options: http_options) end end From bc213ecd253f509a98f938884462e9a330933434 Mon Sep 17 00:00:00 2001 From: manisha1997 Date: Wed, 22 Nov 2023 11:53:50 +0530 Subject: [PATCH 13/18] feat: geolocation setter in sendgrid-ruby for GDPR compliance --- lib/sendgrid/base_interface.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/sendgrid/base_interface.rb b/lib/sendgrid/base_interface.rb index 2708152f..603ad6be 100644 --- a/lib/sendgrid/base_interface.rb +++ b/lib/sendgrid/base_interface.rb @@ -18,9 +18,7 @@ class BaseInterface # def initialize(auth:, host:, region: nil, request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) @auth = auth - if host.nil? && region.nil? - @host = "https://api.sendgrid.com" - elsif !region.nil? + if !region.nil? data_residency(region: region) else @host = host From 82be96b3cac8bcf2b69e36a9287de8dfe1288233 Mon Sep 17 00:00:00 2001 From: manisha1997 Date: Wed, 22 Nov 2023 13:54:46 +0530 Subject: [PATCH 14/18] feat: geolocation setter in sendgrid-ruby for GDPR compliance --- examples/dataresidency/setregion.rb | 66 +------------------ lib/sendgrid/base_interface.rb | 28 ++++---- lib/sendgrid/sendgrid.rb | 5 +- .../helpers/mail/test_data_residency.rb | 40 +++-------- 4 files changed, 25 insertions(+), 114 deletions(-) diff --git a/examples/dataresidency/setregion.rb b/examples/dataresidency/setregion.rb index 5f922a69..03ccfd8e 100644 --- a/examples/dataresidency/setregion.rb +++ b/examples/dataresidency/setregion.rb @@ -9,6 +9,7 @@ content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') mail = SendGrid::Mail.new(from, subject, to, content) sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) +sg.sendgrid_data_residency(region: "global") puts sg.host response = sg.client.mail._('send').post(request_body: mail.to_json) puts response.status_code @@ -24,7 +25,7 @@ content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') mail = SendGrid::Mail.new(from, subject, to, content) sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY_EU']) -sg.data_residency(region: 'eu') +sg.sendgrid_data_residency(region: 'eu') puts sg.host response = sg.client.mail._('send').post(request_body: mail.to_json) puts response.status_code @@ -45,66 +46,3 @@ puts response.status_code puts response.body puts response.headers - -# Example 4 -# Sending using "global" data residency in constructor - -from = SendGrid::Email.new(email: 'example@abc.com') -to = SendGrid::Email.new(email: 'example@abc.com') -subject = 'Sending with Twilio SendGrid is Fun' -content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') -mail = SendGrid::Mail.new(from, subject, to, content) -sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'global') -puts sg.host -response = sg.client.mail._('send').post(request_body: mail.to_json) -puts response.status_code -puts response.body -puts response.headers - -# Example 5 -# Sending using "eu" data residency in constructor - -from = SendGrid::Email.new(email: 'example@abc.com') -to = SendGrid::Email.new(email: 'example@abc.com') -subject = 'Sending with Twilio SendGrid is Fun' -content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') -mail = SendGrid::Mail.new(from, subject, to, content) -sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY_EU'], region: 'eu') -puts sg.host -response = sg.client.mail._('send').post(request_body: mail.to_json) -puts response.status_code -puts response.body -puts response.headers - -# Example 6 -# Sending using "nil" data residency -# This will throw exception because data residency cannot be null - -from = SendGrid::Email.new(email: 'example@abc.com') -to = SendGrid::Email.new(email: 'example@abc.com') -subject = 'Sending with Twilio SendGrid is Fun' -content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') -mail = SendGrid::Mail.new(from, subject, to, content) -sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY_EU']) -sg.data_residency(region: nil) -puts sg.host -response = sg.client.mail._('send').post(request_body: mail.to_json) -puts response.status_code -puts response.body -puts response.headers - -# Example 7 -# First region, then host -from = SendGrid::Email.new(email: 'example@abc.com') -to = SendGrid::Email.new(email: 'example@abc.com') -subject = 'Sending with Twilio SendGrid is Fun' -content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') -mail = SendGrid::Mail.new(from, subject, to, content) -sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) -sg.data_residency(region: "eu") -sg.update_host(host: "https://api.sendgrid.com") -puts sg.host -response = sg.client.mail._('send').post(request_body: mail.to_json) -puts response.status_code -puts response.body -puts response.headers diff --git a/lib/sendgrid/base_interface.rb b/lib/sendgrid/base_interface.rb index 603ad6be..253bc122 100644 --- a/lib/sendgrid/base_interface.rb +++ b/lib/sendgrid/base_interface.rb @@ -16,13 +16,9 @@ class BaseInterface # in the "On-Behalf-Of" header # - +http_options+ -> http options that you want to be globally applied to each request # - def initialize(auth:, host:, region: nil, request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) + def initialize(auth:, host:, request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) @auth = auth - if !region.nil? - data_residency(region: region) - else - @host = host - end + @host = host @version = version || 'v3' @impersonate_subuser = impersonate_subuser @user_agent = "sendgrid/#{SendGrid::VERSION};ruby" @@ -42,20 +38,20 @@ def initialize(auth:, host:, region: nil, request_headers: nil, version: nil, im http_options: @http_options) end - def update_host(host:) - @host = host - @client = SendGrid::Client.new(host: "#{@host}/#{@version}", - request_headers: @request_headers, - http_options: @http_options) - end - - def data_residency(region:) + # Client libraries contain setters for specifying region/edge. + # This supports global and eu regions only. This set will likely expand in the future. + # Global is the default residency (or region) + # Global region means the message will be sent through https://api.sendgrid.com + # EU region means the message will be sent through https://api.eu.sendgrid.com + # Parameters: + # - region(String) : specify the region. Currently supports "global" and "eu" + def sendgrid_data_residency(region:) region_host_dict = { "eu" => 'https://api.eu.sendgrid.com', "global" => 'https://api.sendgrid.com' } raise ArgumentError, "region can only be \"eu\" or \"global\"" if region.nil? || !region_host_dict.key?(region) @host = region_host_dict[region] @client = SendGrid::Client.new(host: "#{@host}/#{@version}", - request_headers: @request_headers, - http_options: @http_options) + request_headers: @request_headers, + http_options: @http_options) end end diff --git a/lib/sendgrid/sendgrid.rb b/lib/sendgrid/sendgrid.rb index ef08d6d2..e6d15ec0 100644 --- a/lib/sendgrid/sendgrid.rb +++ b/lib/sendgrid/sendgrid.rb @@ -10,12 +10,11 @@ class API < BaseInterface # - +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 - # - +region+ -> data residency. The values only be either 'eu' or 'global'. # - def initialize(api_key:, host: nil, region: nil, request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) + 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, region: region, request_headers: request_headers, version: version, impersonate_subuser: impersonate_subuser, http_options: http_options) + 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/helpers/mail/test_data_residency.rb b/test/sendgrid/helpers/mail/test_data_residency.rb index 55627d18..f6bb5281 100644 --- a/test/sendgrid/helpers/mail/test_data_residency.rb +++ b/test/sendgrid/helpers/mail/test_data_residency.rb @@ -11,63 +11,41 @@ def setup def test_with_global_data_residency sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) - sg.data_residency(region: 'global') - assert_equal @global_email, sg.host - end - - def test_with_global_data_residency_in_constructor - sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'global') + sg.sendgrid_data_residency(region: 'global') assert_equal @global_email, sg.host end def test_with_global_eu_residency sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) - sg.data_residency(region: 'eu') - assert_equal @eu_email, sg.host - end - - def test_with_global_eu_residency_in_constructor - sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'eu') - assert_equal @eu_email, sg.host - end - - def test_with_host_set_first_and_residency_set_second - sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'eu') - sg.update_host(host: "https://api.test.sendgrid.com") - sg.data_residency(region: "eu") + sg.sendgrid_data_residency(region: 'eu') assert_equal @eu_email, sg.host end - def test_with_residency_set_first_and_host_set_second - sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], region: 'eu') - sg.data_residency(region: "eu") - sg.update_host(host: "https://api.test.sendgrid.com") - assert_equal "https://api.test.sendgrid.com", sg.host - end - def test_with_global_nil_residency sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) assert_raises(ArgumentError) do - sg.data_residency(region: nil) + sg.sendgrid_data_residency(region: nil) end end def test_with_global_invalid_residency sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) assert_raises(ArgumentError) do - sg.data_residency(region: "abc") + sg.sendgrid_data_residency(region: "abc") end end def test_with_global_empty_residency sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) assert_raises(ArgumentError) do - sg.data_residency(region: "") + sg.sendgrid_data_residency(region: "") end end - def test_host_with_both_host_and_region_in_constructor - sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], host: "https://example.com", region: "eu") + def test_with_global_eu_residency + sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) + sg.sendgrid_data_residency(region: '') assert_equal @eu_email, sg.host end + end From a35c21eca31c9a924707891663ff39eec9cecd49 Mon Sep 17 00:00:00 2001 From: manisha1997 Date: Wed, 22 Nov 2023 13:57:50 +0530 Subject: [PATCH 15/18] feat: geolocation setter in sendgrid-ruby for GDPR compliance --- lib/sendgrid/base_interface.rb | 4 ++-- test/sendgrid/helpers/mail/test_data_residency.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/sendgrid/base_interface.rb b/lib/sendgrid/base_interface.rb index 253bc122..fa11ebc3 100644 --- a/lib/sendgrid/base_interface.rb +++ b/lib/sendgrid/base_interface.rb @@ -51,7 +51,7 @@ def sendgrid_data_residency(region:) @host = region_host_dict[region] @client = SendGrid::Client.new(host: "#{@host}/#{@version}", - request_headers: @request_headers, - http_options: @http_options) + request_headers: @request_headers, + http_options: @http_options) end end diff --git a/test/sendgrid/helpers/mail/test_data_residency.rb b/test/sendgrid/helpers/mail/test_data_residency.rb index f6bb5281..3f889cd5 100644 --- a/test/sendgrid/helpers/mail/test_data_residency.rb +++ b/test/sendgrid/helpers/mail/test_data_residency.rb @@ -42,7 +42,7 @@ def test_with_global_empty_residency end end - def test_with_global_eu_residency + def test_with_failure sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) sg.sendgrid_data_residency(region: '') assert_equal @eu_email, sg.host From 80862aa506ddcf09a31495ea7a843660792cecfc Mon Sep 17 00:00:00 2001 From: manisha1997 Date: Wed, 22 Nov 2023 13:59:30 +0530 Subject: [PATCH 16/18] feat: geolocation setter in sendgrid-ruby for GDPR compliance --- test/sendgrid/helpers/mail/test_data_residency.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/test/sendgrid/helpers/mail/test_data_residency.rb b/test/sendgrid/helpers/mail/test_data_residency.rb index 3f889cd5..71be0dd4 100644 --- a/test/sendgrid/helpers/mail/test_data_residency.rb +++ b/test/sendgrid/helpers/mail/test_data_residency.rb @@ -47,5 +47,4 @@ def test_with_failure sg.sendgrid_data_residency(region: '') assert_equal @eu_email, sg.host end - end From 3a14d2f3edc57393de55e9cab9d900ca5e81f186 Mon Sep 17 00:00:00 2001 From: manisha1997 Date: Wed, 22 Nov 2023 14:03:09 +0530 Subject: [PATCH 17/18] feat: geolocation setter in sendgrid-ruby for GDPR compliance --- test/sendgrid/helpers/mail/test_data_residency.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/sendgrid/helpers/mail/test_data_residency.rb b/test/sendgrid/helpers/mail/test_data_residency.rb index 71be0dd4..d1cf15b1 100644 --- a/test/sendgrid/helpers/mail/test_data_residency.rb +++ b/test/sendgrid/helpers/mail/test_data_residency.rb @@ -41,10 +41,4 @@ def test_with_global_empty_residency sg.sendgrid_data_residency(region: "") end end - - def test_with_failure - sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) - sg.sendgrid_data_residency(region: '') - assert_equal @eu_email, sg.host - end end From 176b7c49964d63250bd933f6d458fbfd8181409f Mon Sep 17 00:00:00 2001 From: manisha1997 Date: Wed, 22 Nov 2023 14:06:23 +0530 Subject: [PATCH 18/18] feat: geolocation setter in sendgrid-ruby for GDPR compliance --- lib/sendgrid/sendgrid.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/sendgrid/sendgrid.rb b/lib/sendgrid/sendgrid.rb index e6d15ec0..03876d5a 100644 --- a/lib/sendgrid/sendgrid.rb +++ b/lib/sendgrid/sendgrid.rb @@ -14,6 +14,7 @@ class API < BaseInterface 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, http_options: http_options) end end