Skip to content

Commit

Permalink
Tidied up a little.
Browse files Browse the repository at this point in the history
- (Mostly) automatically reformatted code using IntelliJ IDEA to match the Google Style Guide (as documented in CONTRIBUTING.md).
  - 2 space indent, where it wasn't already.
  - Line wrap at 100 characters, where it wasn't already.
  - Wildcard imports were replaced with explicit imports.
  - Unused imports were removed.
  - Annotations were put onto their own line, and additional lines added for readability where necessary.
  - Braces were added to if-statements that didn't already have them.
- Also made some minor edits, e.g. as suggested by IntelliJ IDEA (e.g. public --> private).

This will help with getting the code into shape before style conformance is required as per issue sendgrid#472 and pull request sendgrid#496.

See also:
- https://github.com/google/google-java-format
- https://plugins.jetbrains.com/plugin/8527-google-java-format
- https://github.com/google/styleguide/blob/gh-pages/intellij-java-google-style.xml
  • Loading branch information
RohanTalip committed Oct 31, 2018
1 parent 7560a8d commit 58bf3e6
Show file tree
Hide file tree
Showing 23 changed files with 1,029 additions and 652 deletions.
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.
*/
public void error(Exception ex);

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

/**
* Callback method in case of a valid response.
*
* @param response the valid response.
*/
public 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;
}
}
51 changes: 33 additions & 18 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 SendGrid allows for quick and easy access to the SendGrid API.
*/
* The SendGrid class allows for quick and easy access to the 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 SendGrid API wrapper.
*
* @param apiKey is your 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 SendGrid API wrapper.
*
* @param apiKey is your 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 SendGrid API wrapper.
*
* @param apiKey is your 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 @@ -90,14 +94,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 @@ -106,6 +112,7 @@ public String getVersion() {

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

/**
* Obtain the request headers.
*
* @return the request headers.
*/
public Map<String, String> getRequestHeaders() {
Expand All @@ -122,6 +130,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 @@ -133,6 +142,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 @@ -143,6 +153,7 @@ public Map<String, String> removeRequestHeader(String key) {

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

/**
* Set the SendGrid host.
*
* @param host the new 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 @@ -168,25 +180,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 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
* 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 @@ -195,6 +208,7 @@ public void setRateLimitSleep(int rateLimitSleep) {

/**
* Makes the call to the 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 @@ -205,6 +219,7 @@ public Response makeCall(Request request) throws IOException {

/**
* Class api sets up the request to the 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 @@ -226,9 +241,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 @@ -243,10 +258,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
22 changes: 11 additions & 11 deletions src/main/java/com/sendgrid/SendGridAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ public interface SendGridAPI {

/**
* Initializes SendGrid
*
*
* @param apiKey is your SendGrid API Key: https://app.sendgrid.com/settings/api_keys
*/
public void initializeSendGrid(String apiKey);

/**
* Returns the library version
*
*
* @return the library version.
*/
public String getLibraryVersion();
Expand All @@ -28,20 +28,21 @@ public interface SendGridAPI {

/**
* Sets the version.
*
*
* @param version the SendGrid version.
*/
public void setVersion(String version);

/**
* Gets the request headers.
*
* @return returns a map of request headers.
*/
public Map<String, String> getRequestHeaders();

/**
* Adds a request headers.
*
*
* @param key the key
* @param value the value
* @return returns a map of request headers.
Expand All @@ -50,30 +51,29 @@ public interface SendGridAPI {

/**
* Removes a request headers.
*
*
* @param key the key
* @return returns a map of request headers.
*/
public Map<String, String> removeRequestHeader(String key);

/**
* Gets the host.
*
*
* @return returns the host.
*/
public String getHost();

/**
* Sets the host.
*
*
* @param host the host to set
*/
public void setHost(String host);

/**
* Class makeCall makes the call to the SendGrid API, override this method for
* testing.
*
* Class makeCall makes the call to the SendGrid API, override this method for testing.
*
* @param request the request
* @return returns a response.
* @throws IOException in case of network or marshal error.
Expand All @@ -82,7 +82,7 @@ public interface SendGridAPI {

/**
* Class api sets up the request to the SendGrid API, this is main interface.
*
*
* @param request the request
* @return returns a response.
* @throws IOException in case of network or marshal error.
Expand Down
Loading

0 comments on commit 58bf3e6

Please sign in to comment.