From 780e7a0d1fc5e35116d1b4462588a91bfee18a34 Mon Sep 17 00:00:00 2001 From: Manisha Singh Date: Thu, 23 Nov 2023 16:35:54 +0530 Subject: [PATCH] feat: geolocation setter in sendgrid-ruby for GDPR compliance (#496) * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance * feat: geolocation setter in sendgrid-ruby for GDPR compliance --- examples/dataresidency/setregion.rb | 48 +++++++++++++++++++ lib/sendgrid/base_interface.rb | 17 +++++++ .../helpers/mail/test_data_residency.rb | 44 +++++++++++++++++ 3 files changed, 109 insertions(+) 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..03ccfd8e --- /dev/null +++ b/examples/dataresidency/setregion.rb @@ -0,0 +1,48 @@ +require 'sendgrid-ruby' + +# 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.sendgrid_data_residency(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 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_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 +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 diff --git a/lib/sendgrid/base_interface.rb b/lib/sendgrid/base_interface.rb index 0d5615a4..fa11ebc3 100644 --- a/lib/sendgrid/base_interface.rb +++ b/lib/sendgrid/base_interface.rb @@ -37,4 +37,21 @@ def initialize(auth:, host:, request_headers: nil, version: nil, impersonate_sub request_headers: @request_headers, http_options: @http_options) end + + # 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) + 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..d1cf15b1 --- /dev/null +++ b/test/sendgrid/helpers/mail/test_data_residency.rb @@ -0,0 +1,44 @@ +require_relative '../../../../lib/sendgrid-ruby' +require 'minitest/autorun' + +class TestDataResidency < Minitest::Test + include SendGrid + + def setup + @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.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.sendgrid_data_residency(region: 'eu') + assert_equal @eu_email, sg.host + end + + def test_with_global_nil_residency + sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) + assert_raises(ArgumentError) do + 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.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.sendgrid_data_residency(region: "") + end + end +end