From 36a576c4921fb79b1fe3ecb7278e588853c93491 Mon Sep 17 00:00:00 2001 From: shrutiburman <87537688+shrutiburman@users.noreply.github.com> Date: Wed, 1 Nov 2023 22:30:46 +0530 Subject: [PATCH] feat: Add data residency for eu and global regions Merging in feature branch --- src/main/java/com/sendgrid/BaseInterface.java | 25 ++++++++++ src/test/java/com/sendgrid/SendGridTest.java | 46 +++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/src/main/java/com/sendgrid/BaseInterface.java b/src/main/java/com/sendgrid/BaseInterface.java index 0a812c19..bef1814f 100644 --- a/src/main/java/com/sendgrid/BaseInterface.java +++ b/src/main/java/com/sendgrid/BaseInterface.java @@ -17,6 +17,13 @@ public abstract class BaseInterface implements SendGridAPI { private static final int RATE_LIMIT_RESPONSE_CODE = 429; private static final int THREAD_POOL_SIZE = 8; + private static final Map allowedRegionsHostMap; + static { + //todo: change this to Map.of() when we've moved on from Java 8 + allowedRegionsHostMap = new HashMap<>(); + allowedRegionsHostMap.put("eu", "api.eu.sendgrid.com"); + allowedRegionsHostMap.put("global", "api.sendgrid.com"); + } private ExecutorService pool; /** @@ -336,4 +343,22 @@ public void run() { } }); } + + /* + * Client libraries contain setters for specifying region/edge. + * This allows support global and eu regions only. This set will likely expand in the future. + * Global should be the default + * Global region means the message should be sent through: + * HTTP: api.sendgrid.com + * EU region means the message should be sent through: + * HTTP: api.eu.sendgrid.com +*/ + public void setDataResidency(String region){ + if (allowedRegionsHostMap.containsKey(region)){ + this.host = allowedRegionsHostMap.get(region); + } + else{ + throw new IllegalArgumentException("region can only be \"eu\" or \"global\""); + } + } } diff --git a/src/test/java/com/sendgrid/SendGridTest.java b/src/test/java/com/sendgrid/SendGridTest.java index 2ac11fdb..28f55697 100644 --- a/src/test/java/com/sendgrid/SendGridTest.java +++ b/src/test/java/com/sendgrid/SendGridTest.java @@ -3242,4 +3242,50 @@ public void test_request_headers_override_sendgrid_object_headers() throws IOExc sg.api(request); verify(client).api(argThat((Request req) -> req.getHeaders().get("set-on-both").equals("456"))); } + + @Test + public void testSetResidency_happy_path_eu() { + SendGrid sg = new SendGrid(SENDGRID_API_KEY); + sg.setDataResidency("eu"); + Assert.assertEquals(sg.getHost(), "api.eu.sendgrid.com"); + } + @Test + public void testSetResidency_happy_path_global() { + SendGrid sg = new SendGrid(SENDGRID_API_KEY); + sg.setDataResidency("global"); + Assert.assertEquals(sg.getHost(), "api.eu.sendgrid.com"); + } + + + @Test + public void testSetResidency_override_host() { + SendGrid sg = new SendGrid(SENDGRID_API_KEY); + sg.setHost("api.new.com"); + sg.setDataResidency("eu"); + Assert.assertEquals(sg.getHost(), "api.eu.sendgrid.com"); + } + + @Test + public void testsetResidency_override_data_residency() { + SendGrid sg = new SendGrid(SENDGRID_API_KEY); + sg.setDataResidency("eu"); + sg.setHost("api.new.com"); + Assert.assertEquals(sg.getHost(), "api.new.com"); + } + + @Test (expected = IllegalArgumentException.class) + public void testsetResidency_incorrect_region() { + SendGrid sg = new SendGrid(SENDGRID_API_KEY); + sg.setDataResidency("foo"); + } + @Test (expected = IllegalArgumentException.class) + public void testsetResidency_null_region(){ + SendGrid sg = new SendGrid(SENDGRID_API_KEY); + sg.setDataResidency(""); + } + @Test + public void testsetResidency_default_region() { + SendGrid sg = new SendGrid(SENDGRID_API_KEY); + Assert.assertEquals(sg.getHost(), "api.sendgrid.com"); + } }