From 040d916d6b8cb2adf106ab6b910a1e00a85112af Mon Sep 17 00:00:00 2001 From: shrutiburman <87537688+shrutiburman@users.noreply.github.com> Date: Fri, 3 Nov 2023 00:22:24 +0530 Subject: [PATCH 1/3] feat: Add data residency for eu and global regions (#743) --- examples/dataresidency/setregion.java | 77 +++++++++++++++++++ src/main/java/com/sendgrid/BaseInterface.java | 25 ++++++ src/test/java/com/sendgrid/SendGridTest.java | 46 +++++++++++ 3 files changed, 148 insertions(+) create mode 100644 examples/dataresidency/setregion.java diff --git a/examples/dataresidency/setregion.java b/examples/dataresidency/setregion.java new file mode 100644 index 00000000..5dc42150 --- /dev/null +++ b/examples/dataresidency/setregion.java @@ -0,0 +1,77 @@ +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +import com.sendgrid.*; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +////////////////////////////////////////////////////////////////// +// Set data residency to navigate to a region/edge. +// Currently supported: "global", "eu" + + +public class Example { + public static void main(String[] args) throws IOException { + try { + + final Mail helloWorld = buildHelloEmail(); + + Request request = new Request(); + request.setEndpoint("mail/send"); + request.setBody(helloWorld.build()); + request.setMethod(Method.POST); + + // sending to global data residency + Sendgrid sg = buildSendgridObj("global"); + Response response = sg.api(request); + System.out.println("Sending to hostname: " + sg.getHost()); + System.out.println(response.getStatusCode()); + System.out.println(response.getBody()); + System.out.println(response.getHeaders()); + + // sending to EU data residency + Sendgrid sg = buildSendgridObj("eu"); + Response response = sg.api(request); + System.out.println("Sending to hostname: " + sg.getHost()); + System.out.println(response.getStatusCode()); + System.out.println(response.getBody()); + System.out.println(response.getHeaders()); + + // not configuring any region defaults to global + Sendgrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY")); + Response response = sg.api(request); + System.out.println("Sending to hostname: " + sg.getHost()); + System.out.println(response.getStatusCode()); + System.out.println(response.getBody()); + System.out.println(response.getHeaders()); + + } catch (IOException ex) { + throw ex; + } + } + + public static Mail buildHelloEmail() { + Email from = new Email("test@example.com"); + String subject = "Hello World from the Twilio SendGrid Java Library"; + Email to = new Email("test@example.com"); + Content content = new Content("text/plain", "some text here"); + // Note that when you use this constructor an initial personalization object + // is created for you. It can be accessed via + // mail.personalization.get(0) as it is a List object + Mail mail = new Mail(from, subject, to, content); + Email email = new Email("test2@example.com"); + mail.personalization.get(0).addTo(email); + + return mail; + } + + public static Sendgrid buildSendgridObj(String region){ + SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY")); + sg.setDataResidency(region); + return sg; + + } +} + 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..00f66799 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.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"); + } } From c81da35c271eab19f2cf63d7d8560926dc045a10 Mon Sep 17 00:00:00 2001 From: Twilio Date: Mon, 6 Nov 2023 14:44:09 +0000 Subject: [PATCH 2/3] [Librarian] Version Bump --- CHANGELOG.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a8840b0..a7293f35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,15 @@ # Change Log All notable changes to this project will be documented in this file. +[2023-11-06] Version 4.10.0 +--------------------------- +**Library - Feature** +- [PR #743](https://github.com/sendgrid/sendgrid-java/pull/743): Add data residency for eu and global regions. Thanks to [@shrutiburman](https://github.com/shrutiburman)! + +**Library - Test** +- [PR #735](https://github.com/sendgrid/sendgrid-java/pull/735): Adding misc as PR type. Thanks to [@rakatyal](https://github.com/rakatyal)! + + [2022-06-29] Version 4.9.3 -------------------------- **Library - Chore** From 1ed873276731f12a71d0011dcf88804dca97e8ea Mon Sep 17 00:00:00 2001 From: Twilio Date: Mon, 6 Nov 2023 14:44:09 +0000 Subject: [PATCH 3/3] Release 4.10.0 --- CONTRIBUTING.md | 2 +- README.md | 4 ++-- pom.xml | 4 ++-- src/main/java/com/sendgrid/BaseInterface.java | 2 +- src/test/java/com/sendgrid/SendGridTest.java | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 139b3656..77fc2389 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -69,7 +69,7 @@ touch Example.java Add the example you want to test to Example.java, including the headers at the top of the file. ``` bash -javac -classpath ../repo/com/sendgrid/4.9.3/sendgrid-4.9.3-jar.jar:. Example.java && java -classpath ../repo/com/sendgrid/4.9.3/sendgrid-4.9.3-jar.jar:. Example +javac -classpath ../repo/com/sendgrid/4.10.0/sendgrid-4.10.0-jar.jar:. Example.java && java -classpath ../repo/com/sendgrid/4.10.0/sendgrid-4.10.0-jar.jar:. Example ``` diff --git a/README.md b/README.md index fc60fb70..64a8aee5 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ Add the following to your build.gradle file in the root of your project. ... dependencies { ... - implementation 'com.sendgrid:sendgrid-java:4.9.3' + implementation 'com.sendgrid:sendgrid-java:4.10.0' } repositories { @@ -81,7 +81,7 @@ mvn install You can just drop the jar file in. It's a fat jar - it has all the dependencies built in. -[sendgrid-java.jar](https://github.com/sendgrid/sendgrid-java/releases/download/4.9.3/sendgrid-java.jar) +[sendgrid-java.jar](https://github.com/sendgrid/sendgrid-java/releases/download/4.10.0/sendgrid-java.jar) ## Dependencies diff --git a/pom.xml b/pom.xml index ef5d65c2..bb404261 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ com.sendgrid sendgrid-java Twilio SendGrid Java helper library - 4.9.3 + 4.10.0 This Java module allows you to quickly and easily send emails through Twilio SendGrid using Java. https://github.com/sendgrid/sendgrid-java @@ -26,7 +26,7 @@ https://github.com/sendgrid/sendgrid-java scm:git:git@github.com:sendgrid/sendgrid-java.git scm:git:git@github.com:sendgrid/sendgrid-java.git - 4.9.3 + 4.10.0 diff --git a/src/main/java/com/sendgrid/BaseInterface.java b/src/main/java/com/sendgrid/BaseInterface.java index bef1814f..163490a9 100644 --- a/src/main/java/com/sendgrid/BaseInterface.java +++ b/src/main/java/com/sendgrid/BaseInterface.java @@ -11,7 +11,7 @@ */ public abstract class BaseInterface implements SendGridAPI { - private static final String VERSION = "4.9.3"; + private static final String VERSION = "4.10.0"; private static final String USER_AGENT = "sendgrid/" + VERSION + ";java"; private static final int RATE_LIMIT_RESPONSE_CODE = 429; diff --git a/src/test/java/com/sendgrid/SendGridTest.java b/src/test/java/com/sendgrid/SendGridTest.java index 00f66799..6e137eb8 100644 --- a/src/test/java/com/sendgrid/SendGridTest.java +++ b/src/test/java/com/sendgrid/SendGridTest.java @@ -43,7 +43,7 @@ public void testConstructWithClient() throws IOException { @Test public void testLibraryVersion() { SendGrid sg = new SendGrid(SENDGRID_API_KEY); - Assert.assertEquals(sg.getLibraryVersion(), "4.9.3"); + Assert.assertEquals(sg.getLibraryVersion(), "4.10.0"); } @Test