Skip to content

Commit

Permalink
created exceptions, restclient, httpclient, request and response
Browse files Browse the repository at this point in the history
  • Loading branch information
sbansla committed Jun 15, 2024
1 parent 472b4df commit e0d6793
Show file tree
Hide file tree
Showing 29 changed files with 1,443 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,22 @@
<artifactId>maven-scm-provider-gitexe</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.6</version>
</dependency>
</dependencies>
</plugin>
<plugin>
Expand Down Expand Up @@ -245,10 +261,12 @@
<artifactId>spotbugs</artifactId>
<version>4.0.4</version>
</dependency>

</dependencies>
</plugin>
</plugins>
</build>

<developers>
<developer>
<id>thinkingserious</id>
Expand All @@ -264,6 +282,11 @@
<artifactId>java-http-client</artifactId>
<version>4.5.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
Expand Down Expand Up @@ -296,6 +319,12 @@
<artifactId>bcprov-jdk18on</artifactId>
<version>1.76</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>compile</scope>
</dependency>
</dependencies>
<reporting>
<plugins>
Expand Down
91 changes: 91 additions & 0 deletions src/main/java/com/sendgrid/ApiKeySendGrid.java
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;
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/sendgrid/base/apikey/ApiKeyBase.java
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 com.sun.org.slf4j.internal.Logger;
import com.sun.org.slf4j.internal.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);
}
8 changes: 8 additions & 0 deletions src/main/java/com/sendgrid/constant/Config.java
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");
}
15 changes: 15 additions & 0 deletions src/main/java/com/sendgrid/constant/EnumConstants.java
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;
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/sendgrid/constant/ErrorMessages.java
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";
}
61 changes: 61 additions & 0 deletions src/main/java/com/sendgrid/converter/Promoter.java
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 src/main/java/com/sendgrid/exception/ApiConnectionException.java
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 src/main/java/com/sendgrid/exception/ApiErrorResponse.java
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();
}
}
Loading

0 comments on commit e0d6793

Please sign in to comment.