-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package com.sendgrid.helpers.ips; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.util.List; | ||
|
||
public class IPAddress { | ||
|
||
/** | ||
* An IP address. | ||
*/ | ||
@JsonProperty("ip") | ||
private String ip; | ||
|
||
/** | ||
* The subusers that are able to send email from this IP. | ||
*/ | ||
@JsonProperty("subusers") | ||
private List<String> subUsers; | ||
|
||
/** | ||
* The reverse DNS record for this IP address. | ||
*/ | ||
@JsonProperty("rdns") | ||
private String rdns; | ||
|
||
/** | ||
* The IP pools that this IP has been added to. | ||
*/ | ||
@JsonProperty("pools") | ||
private List<String> pools; | ||
|
||
/** | ||
* Indicates if this IP address is currently warming up. | ||
*/ | ||
@JsonProperty("warmup") | ||
private boolean warmup; | ||
|
||
/** | ||
* The date that the IP address was entered into warmup. | ||
*/ | ||
@JsonProperty("start_date") | ||
private long startDate; | ||
|
||
/** | ||
* Indicates if this IP address has been whitelabeled. | ||
*/ | ||
@JsonProperty("whitelabeled") | ||
private boolean whitelabeled; | ||
|
||
/** | ||
* The date that the IP address was assigned to the user. | ||
*/ | ||
@JsonProperty("assigned_at") | ||
private long assignedAt; | ||
|
||
public String getIp() { | ||
return ip; | ||
} | ||
|
||
public void setIp(String ip) { | ||
this.ip = ip; | ||
} | ||
|
||
public List<String> getSubUsers() { | ||
return subUsers; | ||
} | ||
|
||
public void setSubUsers(List<String> subUsers) { | ||
this.subUsers = subUsers; | ||
} | ||
|
||
public String getRdns() { | ||
return rdns; | ||
} | ||
|
||
public void setRdns(String rdns) { | ||
this.rdns = rdns; | ||
} | ||
|
||
public List<String> getPools() { | ||
return pools; | ||
} | ||
|
||
public void setPools(List<String> pools) { | ||
this.pools = pools; | ||
} | ||
|
||
public boolean isWarmup() { | ||
return warmup; | ||
} | ||
|
||
public void setWarmup(boolean warmup) { | ||
this.warmup = warmup; | ||
} | ||
|
||
public long getStartDate() { | ||
return startDate; | ||
} | ||
|
||
public void setStartDate(long startDate) { | ||
this.startDate = startDate; | ||
} | ||
|
||
public boolean isWhitelabeled() { | ||
return whitelabeled; | ||
} | ||
|
||
public void setWhitelabeled(boolean whitelabeled) { | ||
this.whitelabeled = whitelabeled; | ||
} | ||
|
||
public long getAssignedAt() { | ||
return assignedAt; | ||
} | ||
|
||
public void setAssignedAt(long assignedAt) { | ||
this.assignedAt = assignedAt; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.sendgrid.helpers.ips; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.sendgrid.Method; | ||
import com.sendgrid.Request; | ||
import com.sendgrid.Response; | ||
import com.sendgrid.SendGrid; | ||
import org.apache.http.HttpStatus; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Helper class for quick and easy access to the SendGrid IP Addresses API. | ||
*/ | ||
public class IPsHelper { | ||
|
||
private static final String ALL_IPS_ENDPOINT = "ips"; | ||
|
||
/** | ||
* Get a list of unassigned IP addresses. | ||
* @param sendGrid a SendGrid client. | ||
* @return a list of unassigned ip addresses if response status is ok (200), otherwise - null | ||
* @throws IOException in case of a network error or json parse error. | ||
*/ | ||
public static List<String> getAllUnassignedIPs(SendGrid sendGrid) throws IOException { | ||
Request request = new Request(); | ||
request.setMethod(Method.GET); | ||
request.setEndpoint(ALL_IPS_ENDPOINT); | ||
|
||
Response response = sendGrid.api(request); | ||
if (response != null && response.getStatusCode() == HttpStatus.SC_OK) { | ||
List<String> unassignedIPs = new ArrayList<>(); | ||
List<IPAddress> ipAddressList = new ObjectMapper().readValue(response.getBody(), new TypeReference<List<IPAddress>>() {}); | ||
for (IPAddress ipAddress : ipAddressList) { | ||
if (ipAddress.getSubUsers() == null || ipAddress.getSubUsers().size() == 0) { | ||
unassignedIPs.add(ipAddress.getIp()); | ||
} | ||
} | ||
return unassignedIPs; | ||
} | ||
return null; | ||
} | ||
|
||
} |