-
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.
chore: created exceptions, restclient, httpclient, request and respon…
…se (#751) * created exceptions, restclient, httpclient, request and response
- Loading branch information
Showing
28 changed files
with
1,391 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
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,91 @@ | ||
package com.sendgrid; | ||
|
||
import com.sendgrid.constant.ErrorMessages; | ||
import com.sendgrid.exception.AuthenticationException; | ||
import com.sendgrid.http.ApiKeyRestClient; | ||
import lombok.Getter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class ApiKeySendGrid { | ||
private static String apiKey = System.getenv("SENDGRID_API_KEY"); | ||
private static String region = System.getenv("SENDGRID_REGION"); | ||
@Getter | ||
private static List<String> userAgentExtensions; | ||
private static volatile ApiKeyRestClient apiKeyRestClient; | ||
|
||
private ApiKeySendGrid() { | ||
} | ||
|
||
public static synchronized void init(final String apiKey) { | ||
if (apiKey == null || apiKey.isEmpty()) { | ||
throw new AuthenticationException(String.format(ErrorMessages.EMPTY_STRING, "API_KEY")); | ||
} | ||
ApiKeySendGrid.apiKey = apiKey; | ||
} | ||
|
||
|
||
// Explore Data Residency | ||
/** | ||
* Set the region. | ||
* | ||
* @param region region to make request | ||
* Global Region api.sendgrid.com | ||
* EU Region api.eu.sendgrid.com | ||
*/ | ||
public static synchronized void setRegion(final String region) { | ||
if (region == null || region.isEmpty()) { | ||
throw new AuthenticationException(String.format(ErrorMessages.EMPTY_STRING, "REGION")); | ||
} | ||
if (!Objects.equals(region, ApiKeySendGrid.region)) { | ||
ApiKeySendGrid.invalidate(); | ||
} | ||
ApiKeySendGrid.region = region; | ||
} | ||
|
||
public static synchronized void setUserAgentExtensions(final List<String> userAgentExtensions) { | ||
if (userAgentExtensions != null && !userAgentExtensions.isEmpty()) { | ||
ApiKeySendGrid.userAgentExtensions = new ArrayList<>(userAgentExtensions); | ||
} else { | ||
// In case a developer wants to reset userAgentExtensions | ||
ApiKeySendGrid.userAgentExtensions = null; | ||
} | ||
} | ||
|
||
|
||
public static ApiKeyRestClient getRestClient() { | ||
if (ApiKeySendGrid.apiKeyRestClient == null) { | ||
synchronized (ApiKeySendGrid.class) { | ||
if (ApiKeySendGrid.apiKeyRestClient == null) { | ||
ApiKeySendGrid.apiKeyRestClient = buildRestClient(); | ||
} | ||
} | ||
} | ||
|
||
return ApiKeySendGrid.apiKeyRestClient; | ||
} | ||
|
||
private static ApiKeyRestClient buildRestClient() { | ||
if (ApiKeySendGrid.apiKey == null) { | ||
throw new AuthenticationException( | ||
"Api Key is not initialized, please call ApiKeySendGrid.init()" | ||
); | ||
} | ||
ApiKeyRestClient.Builder builder = new ApiKeyRestClient.Builder(ApiKeySendGrid.apiKey); | ||
if (userAgentExtensions != null) { | ||
builder.userAgentExtensions(ApiKeySendGrid.userAgentExtensions); | ||
} | ||
// TODO: Check if it mandatory to fetch region from customer. | ||
builder.region(region); | ||
return builder.build(); | ||
} | ||
|
||
/** | ||
* Invalidates the volatile state held in the Sendgrid singleton. | ||
*/ | ||
private static void invalidate() { | ||
ApiKeySendGrid.apiKeyRestClient = null; | ||
} | ||
} |
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,17 @@ | ||
package com.sendgrid.base.apikey; | ||
|
||
import com.sendgrid.ApiKeySendGrid; | ||
import com.sendgrid.constant.ErrorMessages; | ||
import com.sendgrid.http.ApiResponse; | ||
import com.sendgrid.http.ApiKeyRestClient; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public abstract class ApiKeyBase { | ||
private static final Logger logger = LoggerFactory.getLogger(ApiKeyBase.class); | ||
public ApiResponse send() { | ||
logger.debug(String.format(ErrorMessages.DEFAULT_REST_CLIENT, "ApiKeyBase")); | ||
return send(ApiKeySendGrid.getRestClient()); | ||
} | ||
public abstract ApiResponse send(final ApiKeyRestClient client); | ||
} |
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,8 @@ | ||
package com.sendgrid.constant; | ||
|
||
public class Config { | ||
public static final String VERSION = "5.0.0-rc.0"; | ||
public static final String JAVA_VERSION = System.getProperty("java.version"); | ||
public static final String OS_NAME = System.getProperty("os.name"); | ||
public static final String OS_ARCH = System.getProperty("os.arch"); | ||
} |
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,15 @@ | ||
package com.sendgrid.constant; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
public class EnumConstants { | ||
@Getter | ||
@RequiredArgsConstructor | ||
public enum ContentType { | ||
JSON("application/json"), | ||
FORM_URLENCODED("application/x-www-form-urlencoded"); | ||
|
||
private final String value; | ||
} | ||
} |
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,10 @@ | ||
package com.sendgrid.constant; | ||
|
||
|
||
import lombok.experimental.UtilityClass; | ||
|
||
@UtilityClass | ||
public class ErrorMessages { | ||
public static final String EMPTY_STRING = "'%s' can not be null or empty"; | ||
public static final String DEFAULT_REST_CLIENT = "Sending API request using default '%s' RestClient"; | ||
} |
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,61 @@ | ||
package com.sendgrid.converter; | ||
|
||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Promoter { | ||
|
||
/** | ||
* Create a @see java.net.URI from a string | ||
* | ||
* @param url url to convert | ||
* @return built @see java.net.URI | ||
*/ | ||
public static URI uriFromString(final String url) { | ||
try { | ||
return new URI(url); | ||
} catch (URISyntaxException | NullPointerException e) { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Create a list from a single element. | ||
* | ||
* @param one the single element | ||
* @param <T> type of the element | ||
* @return List containing the single element | ||
*/ | ||
public static <T> List<T> listOfOne(final T one) { | ||
List<T> list = new ArrayList<>(); | ||
list.add(one); | ||
return list; | ||
} | ||
|
||
/** | ||
* Convert a string to a enum type. | ||
* | ||
* @param value string value | ||
* @param values enum values | ||
* @param <T> enum type | ||
* @return converted enum if able to convert; null otherwise | ||
*/ | ||
public static <T extends Enum<?>> T enumFromString(final String value, final T[] values) { | ||
if (value == null) { | ||
return null; | ||
} | ||
|
||
for (T v : values) { | ||
if (v.toString().equalsIgnoreCase(value)) { | ||
return v; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/sendgrid/exception/ApiConnectionException.java
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,15 @@ | ||
package com.sendgrid.exception; | ||
|
||
public class ApiConnectionException extends SendgridException { | ||
|
||
private static final long serialVersionUID = 6354388724599793830L; | ||
|
||
public ApiConnectionException(final String message) { | ||
super(message); | ||
} | ||
|
||
public ApiConnectionException(final String message, final Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/sendgrid/exception/ApiErrorResponse.java
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,29 @@ | ||
package com.sendgrid.exception; | ||
|
||
import com.sun.net.httpserver.Headers; | ||
import lombok.Getter; | ||
import org.apache.http.Header; | ||
|
||
import java.util.Map; | ||
|
||
public class ApiErrorResponse extends RuntimeException { | ||
@Getter | ||
private Integer statusCode; | ||
@Getter | ||
private String statusMessage; | ||
@Getter | ||
private Object error; | ||
@Getter | ||
private Header[] headers; | ||
|
||
public ApiErrorResponse(Integer statusCode, String statusMessage, Object error, Header[] headers) { | ||
super(statusMessage); | ||
this.statusCode = statusCode; | ||
this.statusMessage = statusMessage; | ||
this.error = error; | ||
this.headers = headers; | ||
} | ||
public ApiErrorResponse() { | ||
super(); | ||
} | ||
} |
Oops, something went wrong.