Skip to content

Commit 662d24d

Browse files
authored
chore: created exceptions, restclient, httpclient, request and response (#751)
* created exceptions, restclient, httpclient, request and response
1 parent 472b4df commit 662d24d

28 files changed

Lines changed: 1391 additions & 0 deletions

pom.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,17 @@
159159
<artifactId>maven-scm-provider-gitexe</artifactId>
160160
<version>1.8.1</version>
161161
</dependency>
162+
<dependency>
163+
<groupId>org.projectlombok</groupId>
164+
<artifactId>lombok</artifactId>
165+
<version>1.18.30</version>
166+
<scope>compile</scope>
167+
</dependency>
168+
<dependency>
169+
<groupId>org.slf4j</groupId>
170+
<artifactId>slf4j-api</artifactId>
171+
<version>1.7.30</version>
172+
</dependency>
162173
</dependencies>
163174
</plugin>
164175
<plugin>
@@ -245,10 +256,12 @@
245256
<artifactId>spotbugs</artifactId>
246257
<version>4.0.4</version>
247258
</dependency>
259+
248260
</dependencies>
249261
</plugin>
250262
</plugins>
251263
</build>
264+
252265
<developers>
253266
<developer>
254267
<id>thinkingserious</id>
@@ -264,6 +277,11 @@
264277
<artifactId>java-http-client</artifactId>
265278
<version>4.5.0</version>
266279
</dependency>
280+
<dependency>
281+
<groupId>com.fasterxml.jackson.datatype</groupId>
282+
<artifactId>jackson-datatype-jsr310</artifactId>
283+
<version>${jackson.version}</version>
284+
</dependency>
267285
<dependency>
268286
<groupId>com.fasterxml.jackson.core</groupId>
269287
<artifactId>jackson-core</artifactId>
@@ -296,6 +314,17 @@
296314
<artifactId>bcprov-jdk18on</artifactId>
297315
<version>1.76</version>
298316
</dependency>
317+
<dependency>
318+
<groupId>org.projectlombok</groupId>
319+
<artifactId>lombok</artifactId>
320+
<version>1.18.30</version>
321+
<scope>compile</scope>
322+
</dependency>
323+
<dependency>
324+
<groupId>org.slf4j</groupId>
325+
<artifactId>slf4j-api</artifactId>
326+
<version>1.7.30</version>
327+
</dependency>
299328
</dependencies>
300329
<reporting>
301330
<plugins>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.sendgrid;
2+
3+
import com.sendgrid.constant.ErrorMessages;
4+
import com.sendgrid.exception.AuthenticationException;
5+
import com.sendgrid.http.ApiKeyRestClient;
6+
import lombok.Getter;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.Objects;
11+
12+
public class ApiKeySendGrid {
13+
private static String apiKey = System.getenv("SENDGRID_API_KEY");
14+
private static String region = System.getenv("SENDGRID_REGION");
15+
@Getter
16+
private static List<String> userAgentExtensions;
17+
private static volatile ApiKeyRestClient apiKeyRestClient;
18+
19+
private ApiKeySendGrid() {
20+
}
21+
22+
public static synchronized void init(final String apiKey) {
23+
if (apiKey == null || apiKey.isEmpty()) {
24+
throw new AuthenticationException(String.format(ErrorMessages.EMPTY_STRING, "API_KEY"));
25+
}
26+
ApiKeySendGrid.apiKey = apiKey;
27+
}
28+
29+
30+
// Explore Data Residency
31+
/**
32+
* Set the region.
33+
*
34+
* @param region region to make request
35+
* Global Region api.sendgrid.com
36+
* EU Region api.eu.sendgrid.com
37+
*/
38+
public static synchronized void setRegion(final String region) {
39+
if (region == null || region.isEmpty()) {
40+
throw new AuthenticationException(String.format(ErrorMessages.EMPTY_STRING, "REGION"));
41+
}
42+
if (!Objects.equals(region, ApiKeySendGrid.region)) {
43+
ApiKeySendGrid.invalidate();
44+
}
45+
ApiKeySendGrid.region = region;
46+
}
47+
48+
public static synchronized void setUserAgentExtensions(final List<String> userAgentExtensions) {
49+
if (userAgentExtensions != null && !userAgentExtensions.isEmpty()) {
50+
ApiKeySendGrid.userAgentExtensions = new ArrayList<>(userAgentExtensions);
51+
} else {
52+
// In case a developer wants to reset userAgentExtensions
53+
ApiKeySendGrid.userAgentExtensions = null;
54+
}
55+
}
56+
57+
58+
public static ApiKeyRestClient getRestClient() {
59+
if (ApiKeySendGrid.apiKeyRestClient == null) {
60+
synchronized (ApiKeySendGrid.class) {
61+
if (ApiKeySendGrid.apiKeyRestClient == null) {
62+
ApiKeySendGrid.apiKeyRestClient = buildRestClient();
63+
}
64+
}
65+
}
66+
67+
return ApiKeySendGrid.apiKeyRestClient;
68+
}
69+
70+
private static ApiKeyRestClient buildRestClient() {
71+
if (ApiKeySendGrid.apiKey == null) {
72+
throw new AuthenticationException(
73+
"Api Key is not initialized, please call ApiKeySendGrid.init()"
74+
);
75+
}
76+
ApiKeyRestClient.Builder builder = new ApiKeyRestClient.Builder(ApiKeySendGrid.apiKey);
77+
if (userAgentExtensions != null) {
78+
builder.userAgentExtensions(ApiKeySendGrid.userAgentExtensions);
79+
}
80+
// TODO: Check if it mandatory to fetch region from customer.
81+
builder.region(region);
82+
return builder.build();
83+
}
84+
85+
/**
86+
* Invalidates the volatile state held in the Sendgrid singleton.
87+
*/
88+
private static void invalidate() {
89+
ApiKeySendGrid.apiKeyRestClient = null;
90+
}
91+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.sendgrid.base.apikey;
2+
3+
import com.sendgrid.ApiKeySendGrid;
4+
import com.sendgrid.constant.ErrorMessages;
5+
import com.sendgrid.http.ApiResponse;
6+
import com.sendgrid.http.ApiKeyRestClient;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
10+
public abstract class ApiKeyBase {
11+
private static final Logger logger = LoggerFactory.getLogger(ApiKeyBase.class);
12+
public ApiResponse send() {
13+
logger.debug(String.format(ErrorMessages.DEFAULT_REST_CLIENT, "ApiKeyBase"));
14+
return send(ApiKeySendGrid.getRestClient());
15+
}
16+
public abstract ApiResponse send(final ApiKeyRestClient client);
17+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.sendgrid.constant;
2+
3+
public class Config {
4+
public static final String VERSION = "5.0.0-rc.0";
5+
public static final String JAVA_VERSION = System.getProperty("java.version");
6+
public static final String OS_NAME = System.getProperty("os.name");
7+
public static final String OS_ARCH = System.getProperty("os.arch");
8+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.sendgrid.constant;
2+
3+
import lombok.Getter;
4+
import lombok.RequiredArgsConstructor;
5+
6+
public class EnumConstants {
7+
@Getter
8+
@RequiredArgsConstructor
9+
public enum ContentType {
10+
JSON("application/json"),
11+
FORM_URLENCODED("application/x-www-form-urlencoded");
12+
13+
private final String value;
14+
}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.sendgrid.constant;
2+
3+
4+
import lombok.experimental.UtilityClass;
5+
6+
@UtilityClass
7+
public class ErrorMessages {
8+
public static final String EMPTY_STRING = "'%s' can not be null or empty";
9+
public static final String DEFAULT_REST_CLIENT = "Sending API request using default '%s' RestClient";
10+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.sendgrid.converter;
2+
3+
4+
import java.net.URI;
5+
import java.net.URISyntaxException;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class Promoter {
10+
11+
/**
12+
* Create a @see java.net.URI from a string
13+
*
14+
* @param url url to convert
15+
* @return built @see java.net.URI
16+
*/
17+
public static URI uriFromString(final String url) {
18+
try {
19+
return new URI(url);
20+
} catch (URISyntaxException | NullPointerException e) {
21+
return null;
22+
}
23+
}
24+
25+
/**
26+
* Create a list from a single element.
27+
*
28+
* @param one the single element
29+
* @param <T> type of the element
30+
* @return List containing the single element
31+
*/
32+
public static <T> List<T> listOfOne(final T one) {
33+
List<T> list = new ArrayList<>();
34+
list.add(one);
35+
return list;
36+
}
37+
38+
/**
39+
* Convert a string to a enum type.
40+
*
41+
* @param value string value
42+
* @param values enum values
43+
* @param <T> enum type
44+
* @return converted enum if able to convert; null otherwise
45+
*/
46+
public static <T extends Enum<?>> T enumFromString(final String value, final T[] values) {
47+
if (value == null) {
48+
return null;
49+
}
50+
51+
for (T v : values) {
52+
if (v.toString().equalsIgnoreCase(value)) {
53+
return v;
54+
}
55+
}
56+
57+
return null;
58+
}
59+
60+
61+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.sendgrid.exception;
2+
3+
public class ApiConnectionException extends SendgridException {
4+
5+
private static final long serialVersionUID = 6354388724599793830L;
6+
7+
public ApiConnectionException(final String message) {
8+
super(message);
9+
}
10+
11+
public ApiConnectionException(final String message, final Throwable cause) {
12+
super(message, cause);
13+
}
14+
15+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.sendgrid.exception;
2+
3+
import com.sun.net.httpserver.Headers;
4+
import lombok.Getter;
5+
import org.apache.http.Header;
6+
7+
import java.util.Map;
8+
9+
public class ApiErrorResponse extends RuntimeException {
10+
@Getter
11+
private Integer statusCode;
12+
@Getter
13+
private String statusMessage;
14+
@Getter
15+
private Object error;
16+
@Getter
17+
private Header[] headers;
18+
19+
public ApiErrorResponse(Integer statusCode, String statusMessage, Object error, Header[] headers) {
20+
super(statusMessage);
21+
this.statusCode = statusCode;
22+
this.statusMessage = statusMessage;
23+
this.error = error;
24+
this.headers = headers;
25+
}
26+
public ApiErrorResponse() {
27+
super();
28+
}
29+
}

0 commit comments

Comments
 (0)