com.revinate.sendgrid.resource.EntityResource Maven / Gradle / Ivy
The newest version!
package com.revinate.sendgrid.resource;
import com.revinate.sendgrid.exception.InvalidRequestException;
import com.revinate.sendgrid.exception.SendGridException;
import com.revinate.sendgrid.model.SendGridEntity;
import com.revinate.sendgrid.model.SendGridModel;
import com.revinate.sendgrid.net.SendGridHttpClient;
import com.revinate.sendgrid.net.SendGridHttpClient.RequestType;
import com.revinate.sendgrid.net.auth.Credential;
import java.util.Map;
public abstract class EntityResource extends SendGridResource {
protected final Class entityType;
protected final String id;
public EntityResource(String baseUrl, SendGridHttpClient client, Credential credential, Class entityType, T entity) {
this(baseUrl, client, credential, entityType, entity.getEntityId());
}
public EntityResource(String baseUrl, SendGridHttpClient client, Credential credential, Class entityType, String id) {
super(baseUrl, client, credential);
this.entityType = entityType;
this.id = id;
}
public String getId() {
return id;
}
public T retrieve() throws SendGridException {
return client.get(getUrl(), entityType, credential);
}
public T update(T entity) throws SendGridException {
return client.put(getUrl(), entityType, credential, entity, RequestType.JSON);
}
public T partialUpdate(Map requestObject) throws SendGridException {
return client.patch(getUrl(), entityType, credential, requestObject, RequestType.JSON);
}
public void delete() throws SendGridException {
client.delete(getUrl(), credential);
}
protected String getUrl() throws InvalidRequestException {
if (id == null) {
throw new InvalidRequestException("Missing entity identifier");
}
return String.format("%s/%s", baseUrl, id);
}
}