Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: tidied up a little #539

Merged
merged 1 commit into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions src/main/java/com/sendgrid/APICallback.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package com.sendgrid;

/**
* An interface describing a callback mechanism for the
* asynchronous, rate limit aware API connection.
* An interface describing a callback mechanism for the asynchronous, rate limit aware API
* connection.
*/
public interface APICallback {
/**
* Callback method in case of an error.
* @param ex the error that was thrown.
*/
void error(Exception ex);

/**
* Callback method in case of a valid response.
* @param response the valid response.
*/
void response(Response response);
/**
* Callback method in case of an error.
*
* @param ex the error that was thrown.
*/
void error(Exception ex);

/**
* Callback method in case of a valid response.
*
* @param response the valid response.
*/
void response(Response response);
}
58 changes: 31 additions & 27 deletions src/main/java/com/sendgrid/RateLimitException.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
package com.sendgrid;

/**
* An exception thrown when the maximum number of retries
* have occurred, and the API calls are still rate limited.
* An exception thrown when the maximum number of retries have occurred, and the API calls are still
* rate limited.
*/
public class RateLimitException extends Exception {
private final Request request;
private final int retryCount;

/**
* Construct a new exception.
* @param request the originating request object.
* @param retryCount the number of times a retry was attempted.
*/
public RateLimitException(Request request, int retryCount) {
this.request = request;
this.retryCount = retryCount;
}
private final Request request;
private final int retryCount;

/**
* Get the originating request object.
* @return the request object.
*/
public Request getRequest() {
return this.request;
}
/**
* Construct a new exception.
*
* @param request the originating request object.
* @param retryCount the number of times a retry was attempted.
*/
public RateLimitException(Request request, int retryCount) {
this.request = request;
this.retryCount = retryCount;
}

/**
* Get the number of times the action was attemted.
* @return the retry count.
*/
public int getRetryCount() {
return this.retryCount;
}
/**
* Get the originating request object.
*
* @return the request object.
*/
public Request getRequest() {
return this.request;
}

/**
* Get the number of times the action was attemted.
*
* @return the retry count.
*/
public int getRetryCount() {
return this.retryCount;
}
}
55 changes: 36 additions & 19 deletions src/main/java/com/sendgrid/SendGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* Class Twilio SendGrid allows for quick and easy access to the Twilio SendGrid API.
*/
* The Twilio SendGrid class allows for quick and easy access to the Twilio SendGrid API.
*/
public class SendGrid implements SendGridAPI {

private static final String VERSION = "3.0.0";
Expand Down Expand Up @@ -43,6 +43,7 @@ public class SendGrid implements SendGridAPI {

/**
* Construct a new Twilio SendGrid API wrapper.
*
* @param apiKey is your Twilio SendGrid API Key: https://app.sendgrid.com/settings/api_keys
*/
public SendGrid(String apiKey) {
Expand All @@ -52,6 +53,7 @@ public SendGrid(String apiKey) {

/**
* Construct a new Twilio SendGrid API wrapper.
*
* @param apiKey is your Twilio SendGrid API Key: https://app.sendgrid.com/settings/api_keys
* @param test is true if you are unit testing
*/
Expand All @@ -62,6 +64,7 @@ public SendGrid(String apiKey, Boolean test) {

/**
* Construct a new Twilio SendGrid API wrapper.
*
* @param apiKey is your Twilio SendGrid API Key: https://app.sendgrid.com/settings/api_keys
* @param client the Client to use (allows to customize its configuration)
*/
Expand All @@ -72,6 +75,7 @@ public SendGrid(String apiKey, Client client) {

/**
* Initialize the client.
*
* @param apiKey the user's API key.
*/
public void initializeSendGrid(String apiKey) {
Expand All @@ -89,14 +93,16 @@ public void initializeSendGrid(String apiKey) {

/**
* Retrieve the current library version.
*
* @return the current version.
*/
public String getLibraryVersion() {
return this.VERSION;
return VERSION;
}

/**
* Get the API version.
*
* @return the current API versioin (v3 by default).
*/
public String getVersion() {
Expand All @@ -105,6 +111,7 @@ public String getVersion() {

/**
* Set the API version.
*
* @param version the new version.
*/
public void setVersion(String version) {
Expand All @@ -113,6 +120,7 @@ public void setVersion(String version) {

/**
* Obtain the request headers.
*
* @return the request headers.
*/
public Map<String, String> getRequestHeaders() {
Expand All @@ -121,6 +129,7 @@ public Map<String, String> getRequestHeaders() {

/**
* Add a new request header.
*
* @param key the header key.
* @param value the header value.
* @return the new set of request headers.
Expand All @@ -132,6 +141,7 @@ public Map<String, String> addRequestHeader(String key, String value) {

/**
* Remove a request header.
*
* @param key the header key to remove.
* @return the new set of request headers.
*/
Expand All @@ -142,6 +152,7 @@ public Map<String, String> removeRequestHeader(String key) {

/**
* Get the Twilio SendGrid host (api.sendgrid.com by default).
*
* @return the Twilio SendGrid host.
*/
public String getHost() {
Expand All @@ -150,15 +161,16 @@ public String getHost() {

/**
* Set the Twilio SendGrid host.
*
* @param host the new Twilio SendGrid host.
*/
public void setHost(String host) {
this.host = host;
}

/**
* Get the maximum number of retries on a rate limit response.
* The default is 5.
* Get the maximum number of retries on a rate limit response. The default is 5.
*
* @return the number of retries on a rate limit.
*/
public int getRateLimitRetry() {
Expand All @@ -167,25 +179,26 @@ public int getRateLimitRetry() {

/**
* Set the maximum number of retries on a rate limit response.
*
* @param rateLimitRetry the maximum retry count.
*/
public void setRateLimitRetry(int rateLimitRetry) {
this.rateLimitRetry = rateLimitRetry;
}

/**
* Get the duration of time (in milliseconds) to sleep between
* consecutive rate limit retries. The Twilio SendGrid API enforces
* the rate limit to the second. The default value is 1.1 seconds.
* Get the duration of time (in milliseconds) to sleep between consecutive rate limit retries. The
* Twilio SendGrid API enforces the rate limit to the second. The default value is 1.1 seconds.
*
* @return the sleep duration.
*/
public int getRateLimitSleep() {
return this.rateLimitSleep;
}

/**
* Set the duration of time (in milliseconds) to sleep between
* consecutive rate limit retries.
* Set the duration of time (in milliseconds) to sleep between consecutive rate limit retries.
*
* @param rateLimitSleep the sleep duration.
*/
public void setRateLimitSleep(int rateLimitSleep) {
Expand All @@ -194,6 +207,7 @@ public void setRateLimitSleep(int rateLimitSleep) {

/**
* Impersonate subuser for subsequent requests
*
* @param subuser the subuser to be impersonated
*/
public void addImpersonateSubuser(String subuser) {
Expand All @@ -211,14 +225,16 @@ public void removeImpersonateSubuser() {

/**
* Get the impersonated subuser or null if empty
*
* @return the impersonated subuser
*/
public String getImpersonateSubuser() {
return this.subuser;
return this.subuser;
}

/**
* Makes the call to the Twilio SendGrid API, override this method for testing.
*
* @param request the request to make.
* @return the response object.
* @throws IOException in case of a network error.
Expand All @@ -229,6 +245,7 @@ public Response makeCall(Request request) throws IOException {

/**
* Class api sets up the request to the Twilio SendGrid API, this is main interface.
*
* @param request the request object.
* @return the response object.
* @throws IOException in case of a network error.
Expand All @@ -250,9 +267,9 @@ public Response api(Request request) throws IOException {
}

/**
* Attempt an API call. This method executes the API call asynchronously
* on an internal thread pool. If the call is rate limited, the thread
* will retry up to the maximum configured time.
* Attempt an API call. This method executes the API call asynchronously on an internal thread
* pool. If the call is rate limited, the thread will retry up to the maximum configured time.
*
* @param request the API request.
*/
public void attempt(Request request) {
Expand All @@ -267,10 +284,10 @@ public void response(Response r) {
}

/**
* Attempt an API call. This method executes the API call asynchronously
* on an internal thread pool. If the call is rate limited, the thread
* will retry up to the maximum configured time. The supplied callback
* will be called in the event of an error, or a successful response.
* Attempt an API call. This method executes the API call asynchronously on an internal thread
* pool. If the call is rate limited, the thread will retry up to the maximum configured time. The
* supplied callback will be called in the event of an error, or a successful response.
*
* @param request the API request.
* @param callback the callback.
*/
Expand Down
Loading