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: Region set and encode query params #755

Merged
merged 2 commits into from
Jun 25, 2024
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
10 changes: 8 additions & 2 deletions src/main/java/com/sendgrid/ApiKeySendGrid.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sendgrid;

import com.sendgrid.constant.EnumConstants;
import com.sendgrid.constant.ErrorMessages;
import com.sendgrid.exception.AuthenticationException;
import com.sendgrid.http.ApiKeyRestClient;
Expand Down Expand Up @@ -39,6 +40,9 @@ public static synchronized void setRegion(final String region) {
if (region == null || region.isEmpty()) {
throw new AuthenticationException(String.format(ErrorMessages.EMPTY_STRING, "REGION"));
}
if (!EnumConstants.Region.getValues().contains(region)) {
throw new AuthenticationException(String.format(ErrorMessages.INVALID_STRING, "REGION"));
}
if (!Objects.equals(region, ApiKeySendGrid.region)) {
ApiKeySendGrid.invalidate();
}
Expand Down Expand Up @@ -77,8 +81,10 @@ private static ApiKeyRestClient buildRestClient() {
if (userAgentExtensions != null) {
builder.userAgentExtensions(ApiKeySendGrid.userAgentExtensions);
}
// TODO: Check if it mandatory to fetch region from customer.
builder.region(region);
if (region == null)
builder.region(EnumConstants.Region.GLOBAL.getValue());
else
builder.region(region);
return builder.build();
}

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/sendgrid/constant/Domains.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.sendgrid.constant;

public enum Domains {
API("api");
private final String value;

private Domains(final String value) {
this.value = value;
}

public String toString() {
return value;
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/sendgrid/constant/EnumConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class EnumConstants {
@Getter
@RequiredArgsConstructor
Expand All @@ -12,4 +16,19 @@ public enum ContentType {

private final String value;
}

@Getter
@RequiredArgsConstructor
public enum Region {
GLOBAL("global"),
EU("eu");

private final String value;

public static List<String> getValues() {
return Arrays.stream(Region.values())
.map(Region::getValue)
.collect(Collectors.toList());
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/sendgrid/constant/ErrorMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
@UtilityClass
public class ErrorMessages {
public static final String EMPTY_STRING = "'%s' can not be null or empty";

public static final String INVALID_STRING = "'%s' is invalid";
public static final String DEFAULT_REST_CLIENT = "Sending API request using default '%s' RestClient";
}
5 changes: 2 additions & 3 deletions src/main/java/com/sendgrid/exception/ApiErrorResponse.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
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
Expand All @@ -17,7 +16,7 @@ public class ApiErrorResponse extends RuntimeException {
private Header[] headers;

public ApiErrorResponse(Integer statusCode, String statusMessage, Object error, Header[] headers) {
super(statusMessage);
super("An error has occurred");
this.statusCode = statusCode;
this.statusMessage = statusMessage;
this.error = error;
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/com/sendgrid/exception/GenericApiError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.sendgrid.exception;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.List;
import java.util.StringJoiner;

@ToString
public class GenericApiError {
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("errors")
@Getter
@Setter
private List<GenericError> errors;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("id")
@Getter
@Setter
private String id;

public GenericApiError() {
}

private GenericApiError(GenericApiError.Builder builder) {
this.errors = builder.errors;
this.id = builder.id;
}

// Builder class for constructing object
public static class Builder {
private List<GenericError> errors;
private String id;

public Builder() {
}

public GenericApiError.Builder errors(List<GenericError> errors) {
this.errors = errors;
return this;
}

public GenericApiError.Builder id(String id) {
this.id = id;
return this;
}

public GenericApiError build() {
return new GenericApiError(this);
}

}

@Override
public String toString() {
StringJoiner joiner = new StringJoiner(", ", GenericApiError.class.getSimpleName() + "(", ")");
if (errors != null) joiner.add("errors=" + errors);
if (id != null) joiner.add("id=" + id);
return joiner.toString();
}

}
78 changes: 78 additions & 0 deletions src/main/java/com/sendgrid/exception/GenericError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.sendgrid.exception;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.StringJoiner;

@ToString
public class GenericError {
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("message")
@Getter
@Setter
private String message;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("field")
@Getter
@Setter
private String field;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("help")
@Getter
@Setter
private Object help;

public GenericError() {
}

private GenericError(GenericError.Builder builder) {
this.message = builder.message;
this.field = builder.field;
this.help = builder.help;
}

// Builder class for constructing object
public static class Builder {
private String message;
private String field;
private Object help;

public Builder() {
}

public GenericError.Builder message(String message) {
this.message = message;
return this;
}

public GenericError.Builder field(String field) {
this.field = field;
return this;
}

public GenericError.Builder help(Object help) {
this.help = help;
return this;
}

public GenericError build() {
return new GenericError(this);
}

}

@Override
public String toString() {
StringJoiner joiner = new StringJoiner(", ", GenericError.class.getSimpleName() + "(", ")");
if (message != null) joiner.add("message=" + message);
if (field != null) joiner.add("field=" + field);
if (help != null) joiner.add("help=" + help);
return joiner.toString();
}

}

14 changes: 13 additions & 1 deletion src/main/java/com/sendgrid/http/ApiKeyRestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

public class ApiKeyRestClient {
private final String apiKey;
@Getter
private final String region;
@Getter
private final ObjectMapper objectMapper;
Expand Down Expand Up @@ -42,7 +43,18 @@ public Response request(final Request request) {
}
logRequest(request);

return null;
Response response = httpClient.makeRequest(request);

if (logger.isDebugEnabled()) {
logger.debug("status code: {}", response.getStatusCode());
org.apache.http.Header[] responseHeaders = response.getHeaders();
logger.debug("response headers:");
for (int i = 0; i < responseHeaders.length; i++) {
logger.debug("responseHeader: {}", responseHeaders[i]);
}
}

return response;
}


Expand Down
31 changes: 21 additions & 10 deletions src/main/java/com/sendgrid/http/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class Request {
@Getter
private final String url;
@Getter
private final String domain;
@Getter
private String body;
@Getter
private Map<String, String> headers;
Expand All @@ -33,26 +35,31 @@ public void addHeader(String key, String value) {

private Request(Builder builder) {
this.method = builder.method;
this.domain = builder.domain;
this.region = builder.region;
this.body = builder.body;
this.headers = builder.headers;

String baseUrl = builder.url;
String baseUrl = Utility.buildBaseUrl(domain, region, builder.endPoint);
baseUrl = Utility.buildWithPathParams(baseUrl, builder.pathParams);
baseUrl = Utility.buildWithQueryParams(baseUrl, builder.queryParams);
this.url = baseUrl;
}

public static class Builder {
private String url;
private String endPoint;
private HttpMethod method;
private String domain;
private String region;
private Map<String, String> headers = new HashMap<>();
private Map<String, List<String>> queryParams = new HashMap<>();
private Map<String, String> queryParams = new HashMap<>();
private Map<String, String> pathParams = new HashMap<>();
private String body;

public Builder(HttpMethod method, String url) {
public Builder(HttpMethod method, String endPoint, String domain) {
this.method = method;
this.url = url;
this.endPoint = endPoint;
this.domain = domain;
}

public Builder addPathParam(String key, String value) {
Expand All @@ -70,23 +77,27 @@ public Builder addHeaderParam(String key, String value) {
* limit: If limit occurs as query param in open api spec
* offset: If offset occurs as query param in open api spec
* query: If there is query parameter apart from limit and offset.
* It will be the responsibility of the client to build a compound query, encode it and pass it as a query parameter in the query field.
* It will be the responsibility of the client to build a compound query and pass it as a query parameter in the query field.
*/
public Builder addQueryParam(String key, String value) {
if (!queryParams.containsKey(key)) {
queryParams.put(key, new ArrayList<String>());
}
queryParams.get(key).add(value);
queryParams.put(key, value);
return this;
}

public Builder addBody(String body) {
this.body = body;
return this;
}

public Builder region(String region) {
this.region = region;
return this;
}

public Request build() {
return new Request(this);
}
}


}
Loading
Loading