Skip to content

Commit 461315c

Browse files
authored
feat: Add helper for get unassigned IPs (#292)
1 parent e0454aa commit 461315c

2 files changed

Lines changed: 167 additions & 0 deletions

File tree

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package com.sendgrid.helpers.ips;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
import java.util.List;
6+
7+
public class IPAddress {
8+
9+
/**
10+
* An IP address.
11+
*/
12+
@JsonProperty("ip")
13+
private String ip;
14+
15+
/**
16+
* The subusers that are able to send email from this IP.
17+
*/
18+
@JsonProperty("subusers")
19+
private List<String> subUsers;
20+
21+
/**
22+
* The reverse DNS record for this IP address.
23+
*/
24+
@JsonProperty("rdns")
25+
private String rdns;
26+
27+
/**
28+
* The IP pools that this IP has been added to.
29+
*/
30+
@JsonProperty("pools")
31+
private List<String> pools;
32+
33+
/**
34+
* Indicates if this IP address is currently warming up.
35+
*/
36+
@JsonProperty("warmup")
37+
private boolean warmup;
38+
39+
/**
40+
* The date that the IP address was entered into warmup.
41+
*/
42+
@JsonProperty("start_date")
43+
private long startDate;
44+
45+
/**
46+
* Indicates if this IP address has been whitelabeled.
47+
*/
48+
@JsonProperty("whitelabeled")
49+
private boolean whitelabeled;
50+
51+
/**
52+
* The date that the IP address was assigned to the user.
53+
*/
54+
@JsonProperty("assigned_at")
55+
private long assignedAt;
56+
57+
public String getIp() {
58+
return ip;
59+
}
60+
61+
public void setIp(String ip) {
62+
this.ip = ip;
63+
}
64+
65+
public List<String> getSubUsers() {
66+
return subUsers;
67+
}
68+
69+
public void setSubUsers(List<String> subUsers) {
70+
this.subUsers = subUsers;
71+
}
72+
73+
public String getRdns() {
74+
return rdns;
75+
}
76+
77+
public void setRdns(String rdns) {
78+
this.rdns = rdns;
79+
}
80+
81+
public List<String> getPools() {
82+
return pools;
83+
}
84+
85+
public void setPools(List<String> pools) {
86+
this.pools = pools;
87+
}
88+
89+
public boolean isWarmup() {
90+
return warmup;
91+
}
92+
93+
public void setWarmup(boolean warmup) {
94+
this.warmup = warmup;
95+
}
96+
97+
public long getStartDate() {
98+
return startDate;
99+
}
100+
101+
public void setStartDate(long startDate) {
102+
this.startDate = startDate;
103+
}
104+
105+
public boolean isWhitelabeled() {
106+
return whitelabeled;
107+
}
108+
109+
public void setWhitelabeled(boolean whitelabeled) {
110+
this.whitelabeled = whitelabeled;
111+
}
112+
113+
public long getAssignedAt() {
114+
return assignedAt;
115+
}
116+
117+
public void setAssignedAt(long assignedAt) {
118+
this.assignedAt = assignedAt;
119+
}
120+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.sendgrid.helpers.ips;
2+
3+
import com.fasterxml.jackson.core.type.TypeReference;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.sendgrid.Method;
6+
import com.sendgrid.Request;
7+
import com.sendgrid.Response;
8+
import com.sendgrid.SendGrid;
9+
import org.apache.http.HttpStatus;
10+
11+
import java.io.IOException;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
/**
16+
* Helper class for quick and easy access to the SendGrid IP Addresses API.
17+
*/
18+
public class IPsHelper {
19+
20+
private static final String ALL_IPS_ENDPOINT = "ips";
21+
22+
/**
23+
* Get a list of unassigned IP addresses.
24+
* @param sendGrid a SendGrid client.
25+
* @return a list of unassigned ip addresses if response status is ok (200), otherwise - null
26+
* @throws IOException in case of a network error or json parse error.
27+
*/
28+
public static List<String> getAllUnassignedIPs(SendGrid sendGrid) throws IOException {
29+
Request request = new Request();
30+
request.setMethod(Method.GET);
31+
request.setEndpoint(ALL_IPS_ENDPOINT);
32+
33+
Response response = sendGrid.api(request);
34+
if (response != null && response.getStatusCode() == HttpStatus.SC_OK) {
35+
List<String> unassignedIPs = new ArrayList<>();
36+
List<IPAddress> ipAddressList = new ObjectMapper().readValue(response.getBody(), new TypeReference<List<IPAddress>>() {});
37+
for (IPAddress ipAddress : ipAddressList) {
38+
if (ipAddress.getSubUsers() == null || ipAddress.getSubUsers().size() == 0) {
39+
unassignedIPs.add(ipAddress.getIp());
40+
}
41+
}
42+
return unassignedIPs;
43+
}
44+
return null;
45+
}
46+
47+
}

0 commit comments

Comments
 (0)