Skip to content

Commit

Permalink
Add helper for get unassigned IPs
Browse files Browse the repository at this point in the history
  • Loading branch information
pushkyn committed Oct 1, 2018
1 parent 96655f3 commit 51179ee
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 0 deletions.
120 changes: 120 additions & 0 deletions src/main/java/com/sendgrid/helpers/ips/IPAddress.java
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;
}
}
47 changes: 47 additions & 0 deletions src/main/java/com/sendgrid/helpers/ips/IPsHelper.java
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;
}

}

0 comments on commit 51179ee

Please sign in to comment.