Skip to content

Commit c50d75c

Browse files
author
Matt Bernier
authored
Merge pull request sendgrid#361 from bhargodevarya/333
Fix similar-code issue in examples/accesssettings/accesssettings.java
2 parents fb8fe56 + 06b381e commit c50d75c

9 files changed

+223
-142
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import com.sendgrid.Method;
2+
import com.sendgrid.Request;
3+
import com.sendgrid.Response;
4+
import com.sendgrid.SendGrid;
5+
6+
import java.io.IOException;
7+
8+
/*Add one or more IPs to the whitelist
9+
POST /access_settings/whitelist
10+
*/
11+
12+
public class CreateAccessSettings extends Example {
13+
14+
private void run() throws IOException {
15+
try {
16+
String endPoint = "access_settings/whitelist";
17+
String body = "{\"ips\":[{\"ip\":\"192.168.1.1\"},{\"ip\":\"192.*.*.*\"},{\"ip\":\"192.168.1.3/32\"}]}";
18+
Request request = createRequest(Method.POST, endPoint, body);
19+
Response response = execute(request);
20+
printResponseInfo(response);
21+
} catch (IOException ex) {
22+
throw ex;
23+
}
24+
}
25+
26+
public static void main(String[] args) throws IOException {
27+
CreateAccessSettings createAccessSettings = new CreateAccessSettings();
28+
createAccessSettings.run();
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import com.sendgrid.Method;
2+
import com.sendgrid.Request;
3+
import com.sendgrid.Response;
4+
import com.sendgrid.SendGrid;
5+
6+
import java.io.IOException;
7+
8+
/* Remove one or more IPs from the whitelist
9+
DELETE /access_settings/whitelist
10+
*/
11+
12+
public class DeleteAccessSettings extends Example {
13+
14+
private void run() throws IOException {
15+
try {
16+
String endPoint = "access_settings/whitelist";
17+
String body = "{\"ids\":[1,2,3]}";
18+
Request request = createRequest(Method.DELETE, endPoint, body);
19+
Response response = execute(request);
20+
printResponseInfo(response);
21+
} catch (IOException ex) {
22+
throw ex;
23+
}
24+
}
25+
26+
public static void main(String[] args) throws IOException {
27+
DeleteAccessSettings deleteAccessSettings = new DeleteAccessSettings();
28+
deleteAccessSettings.run();
29+
}
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import com.sendgrid.Method;
2+
import com.sendgrid.Request;
3+
import com.sendgrid.Response;
4+
import com.sendgrid.SendGrid;
5+
6+
import java.io.IOException;
7+
8+
/*Remove a specific IP from the whitelist
9+
DELETE /access_settings/whitelist/{rule_id}
10+
*/
11+
12+
public class DeleteIPFromAccessSettings extends Example {
13+
14+
private void run() throws IOException {
15+
try {
16+
String endPoint = "access_settings/whitelist";
17+
String body = "{\"ids\":[1,2,3]}";
18+
Request request = createRequest(Method.DELETE, endPoint, body);
19+
Response response = execute(request);
20+
printResponseInfo(response);
21+
} catch (IOException ex) {
22+
throw ex;
23+
}
24+
}
25+
26+
public static void main(String[] args) throws IOException {
27+
DeleteIPFromAccessSettings deleteIPFromAccessSettings = new DeleteIPFromAccessSettings();
28+
deleteIPFromAccessSettings.run();
29+
}

examples/accesssettings/Example.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import com.sendgrid.Method;
2+
import com.sendgrid.Request;
3+
import com.sendgrid.Response;
4+
import com.sendgrid.SendGrid;
5+
6+
import java.io.IOException;
7+
8+
public class Example {
9+
10+
protected Request createRequest(Method method, String endPoint, String requestBody) {
11+
Request request = new Request();
12+
request.setMethod(method);
13+
request.setEndpoint(endPoint);
14+
if (requestBody != null) {
15+
request.setBody(requestBody);
16+
}
17+
return request;
18+
}
19+
20+
protected Response execute(Request request) throws IOException {
21+
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
22+
Response response = sg.api(request);
23+
return response;
24+
}
25+
26+
protected void printResonseInfo(Response response) {
27+
System.out.println(response.getStatusCode());
28+
System.out.println(response.getBody());
29+
System.out.println(response.getHeaders());
30+
}
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import com.sendgrid.Method;
2+
import com.sendgrid.Request;
3+
import com.sendgrid.Response;
4+
import com.sendgrid.SendGrid;
5+
6+
import java.io.IOException;
7+
8+
/*Retrieve a list of currently whitelisted IPs
9+
GET /access_settings/whitelist
10+
*/
11+
12+
public class GetAccessSettings extends Example{
13+
14+
private void run() throws IOException {
15+
try {
16+
String endPoint = "access_settings/whitelist";
17+
String body = "{\"ids\":[1,2,3]}";
18+
Request request = createRequest(Method.DELETE, endPoint, body);
19+
Response response = execute(request);
20+
printResponseInfo(response);
21+
} catch (IOException ex) {
22+
throw ex;
23+
}
24+
}
25+
26+
public static void main(String[] args) throws IOException {
27+
GetAccessSettings getAccessSettings = new GetAccessSettings();
28+
getAccessSettings.run();
29+
}
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import com.sendgrid.Method;
2+
import com.sendgrid.Request;
3+
import com.sendgrid.Response;
4+
import com.sendgrid.SendGrid;
5+
6+
import java.io.IOException;
7+
8+
/*Retrieve all recent access attempts
9+
GET /access_settings/activity
10+
*/
11+
12+
public class GetAccessSettingsActivity extends Example {
13+
14+
private void run() throws IOException {
15+
try {
16+
String endPoint = "access_settings/whitelist";
17+
String body = "{\"ids\":[1,2,3]}";
18+
Request request = createRequest(Method.DELETE, endPoint, body);
19+
request.addQueryParam("limit", "1");
20+
Response response = execute(request);
21+
printResponseInfo(response);
22+
} catch (IOException ex) {
23+
throw ex;
24+
}
25+
}
26+
27+
public static void main(String[] args) throws IOException {
28+
GetAccessSettingsActivity getAccessSettingsActivity = new GetAccessSettingsActivity();
29+
getAccessSettingsActivity.run();
30+
}
31+
}
32+
33+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import com.sendgrid.Method;
2+
import com.sendgrid.Request;
3+
import com.sendgrid.Response;
4+
import com.sendgrid.SendGrid;
5+
6+
import java.io.IOException;
7+
8+
/* Retrieve a specific whitelisted IP
9+
GET /access_settings/whitelist/{rule_id}
10+
*/
11+
12+
public class GetIPFromAccessSettings extends Example {
13+
14+
private void run() throws IOException {
15+
try {
16+
String endPoint = "access_settings/whitelist";
17+
String body = "{\"ids\":[1,2,3]}";
18+
Request request = createRequest(Method.DELETE, endPoint, body);
19+
Response response = execute(request);
20+
printResponseInfo(response);
21+
} catch (IOException ex) {
22+
throw ex;
23+
}
24+
}
25+
26+
public static void main(String[] args) throws IOException {
27+
GetIPFromAccessSettings getIPFromAccessSettings = new GetIPFromAccessSettings();
28+
getIPFromAccessSettings.run();
29+
}
30+
}

examples/accesssettings/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
![SendGrid Logo](https://uiux.s3.amazonaws.com/2016-logos/email-logo%402x.png)
2+
+
3+
+This folder contains various examples on using the ACCESS_SETTINGS endpoint of SendGrid with Java:
4+
+
5+
+* [Retrieve a list of currently whitelisted IPs (GET /access_settings/whitelist)](GetAccessSettings.java)
6+
+* [Retrieve a specific whitelisted IP (GET /access_settings/whitelist/{rule_id})](GetIPFromAccessSettings.java)
7+
+* [Retrieve a list of currently whitelisted IPs (GET /access_settings/whitelist)](GetAccessSettingsActivity.java)
8+
+* [Remove a specific IP from the whitelist (DELETE /access_settings/whitelist/{rule_id}](DeleteIPFromAccessSettings.java)
9+
+* [Remove one or more IPs from the whitelist (DELETE /access_settings/whitelist)](DeleteAccessSettings.java)
10+
+* [Add one or more IPs to the whitelist (POST /access_settings/whitelist)](CreateAccessSettings.java)

examples/accesssettings/accesssettings.java

Lines changed: 0 additions & 142 deletions
This file was deleted.

0 commit comments

Comments
 (0)