Skip to content

Commit 040d916

Browse files
authored
feat: Add data residency for eu and global regions (#743)
1 parent 146bfee commit 040d916

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import com.fasterxml.jackson.databind.JsonNode;
2+
import com.fasterxml.jackson.databind.ObjectMapper;
3+
4+
import com.sendgrid.*;
5+
6+
import java.io.IOException;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
//////////////////////////////////////////////////////////////////
11+
// Set data residency to navigate to a region/edge.
12+
// Currently supported: "global", "eu"
13+
14+
15+
public class Example {
16+
public static void main(String[] args) throws IOException {
17+
try {
18+
19+
final Mail helloWorld = buildHelloEmail();
20+
21+
Request request = new Request();
22+
request.setEndpoint("mail/send");
23+
request.setBody(helloWorld.build());
24+
request.setMethod(Method.POST);
25+
26+
// sending to global data residency
27+
Sendgrid sg = buildSendgridObj("global");
28+
Response response = sg.api(request);
29+
System.out.println("Sending to hostname: " + sg.getHost());
30+
System.out.println(response.getStatusCode());
31+
System.out.println(response.getBody());
32+
System.out.println(response.getHeaders());
33+
34+
// sending to EU data residency
35+
Sendgrid sg = buildSendgridObj("eu");
36+
Response response = sg.api(request);
37+
System.out.println("Sending to hostname: " + sg.getHost());
38+
System.out.println(response.getStatusCode());
39+
System.out.println(response.getBody());
40+
System.out.println(response.getHeaders());
41+
42+
// not configuring any region defaults to global
43+
Sendgrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
44+
Response response = sg.api(request);
45+
System.out.println("Sending to hostname: " + sg.getHost());
46+
System.out.println(response.getStatusCode());
47+
System.out.println(response.getBody());
48+
System.out.println(response.getHeaders());
49+
50+
} catch (IOException ex) {
51+
throw ex;
52+
}
53+
}
54+
55+
public static Mail buildHelloEmail() {
56+
Email from = new Email("test@example.com");
57+
String subject = "Hello World from the Twilio SendGrid Java Library";
58+
Email to = new Email("test@example.com");
59+
Content content = new Content("text/plain", "some text here");
60+
// Note that when you use this constructor an initial personalization object
61+
// is created for you. It can be accessed via
62+
// mail.personalization.get(0) as it is a List object
63+
Mail mail = new Mail(from, subject, to, content);
64+
Email email = new Email("test2@example.com");
65+
mail.personalization.get(0).addTo(email);
66+
67+
return mail;
68+
}
69+
70+
public static Sendgrid buildSendgridObj(String region){
71+
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
72+
sg.setDataResidency(region);
73+
return sg;
74+
75+
}
76+
}
77+

src/main/java/com/sendgrid/BaseInterface.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ public abstract class BaseInterface implements SendGridAPI {
1717
private static final int RATE_LIMIT_RESPONSE_CODE = 429;
1818
private static final int THREAD_POOL_SIZE = 8;
1919

20+
private static final Map<String, String> allowedRegionsHostMap;
21+
static {
22+
//todo: change this to Map.of() when we've moved on from Java 8
23+
allowedRegionsHostMap = new HashMap<>();
24+
allowedRegionsHostMap.put("eu", "api.eu.sendgrid.com");
25+
allowedRegionsHostMap.put("global", "api.sendgrid.com");
26+
}
2027
private ExecutorService pool;
2128

2229
/**
@@ -336,4 +343,22 @@ public void run() {
336343
}
337344
});
338345
}
346+
347+
/*
348+
* Client libraries contain setters for specifying region/edge.
349+
* This allows support global and eu regions only. This set will likely expand in the future.
350+
* Global should be the default
351+
* Global region means the message should be sent through:
352+
* HTTP: api.sendgrid.com
353+
* EU region means the message should be sent through:
354+
* HTTP: api.eu.sendgrid.com
355+
*/
356+
public void setDataResidency(String region){
357+
if (allowedRegionsHostMap.containsKey(region)){
358+
this.host = allowedRegionsHostMap.get(region);
359+
}
360+
else{
361+
throw new IllegalArgumentException("region can only be \"eu\" or \"global\"");
362+
}
363+
}
339364
}

src/test/java/com/sendgrid/SendGridTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3242,4 +3242,50 @@ public void test_request_headers_override_sendgrid_object_headers() throws IOExc
32423242
sg.api(request);
32433243
verify(client).api(argThat((Request req) -> req.getHeaders().get("set-on-both").equals("456")));
32443244
}
3245+
3246+
@Test
3247+
public void testSetResidency_happy_path_eu() {
3248+
SendGrid sg = new SendGrid(SENDGRID_API_KEY);
3249+
sg.setDataResidency("eu");
3250+
Assert.assertEquals(sg.getHost(), "api.eu.sendgrid.com");
3251+
}
3252+
@Test
3253+
public void testSetResidency_happy_path_global() {
3254+
SendGrid sg = new SendGrid(SENDGRID_API_KEY);
3255+
sg.setDataResidency("global");
3256+
Assert.assertEquals(sg.getHost(), "api.sendgrid.com");
3257+
}
3258+
3259+
3260+
@Test
3261+
public void testSetResidency_override_host() {
3262+
SendGrid sg = new SendGrid(SENDGRID_API_KEY);
3263+
sg.setHost("api.new.com");
3264+
sg.setDataResidency("eu");
3265+
Assert.assertEquals(sg.getHost(), "api.eu.sendgrid.com");
3266+
}
3267+
3268+
@Test
3269+
public void testsetResidency_override_data_residency() {
3270+
SendGrid sg = new SendGrid(SENDGRID_API_KEY);
3271+
sg.setDataResidency("eu");
3272+
sg.setHost("api.new.com");
3273+
Assert.assertEquals(sg.getHost(), "api.new.com");
3274+
}
3275+
3276+
@Test (expected = IllegalArgumentException.class)
3277+
public void testsetResidency_incorrect_region() {
3278+
SendGrid sg = new SendGrid(SENDGRID_API_KEY);
3279+
sg.setDataResidency("foo");
3280+
}
3281+
@Test (expected = IllegalArgumentException.class)
3282+
public void testsetResidency_null_region(){
3283+
SendGrid sg = new SendGrid(SENDGRID_API_KEY);
3284+
sg.setDataResidency("");
3285+
}
3286+
@Test
3287+
public void testsetResidency_default_region() {
3288+
SendGrid sg = new SendGrid(SENDGRID_API_KEY);
3289+
Assert.assertEquals(sg.getHost(), "api.sendgrid.com");
3290+
}
32453291
}

0 commit comments

Comments
 (0)