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: Modify request #759

Merged
merged 3 commits into from
Jun 27, 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ examples/Example.java
sendgrid-java.jar
dependency-reduced-pom.xml
prism*
**/.openapi-generator*
**/TestSG.java
6 changes: 3 additions & 3 deletions src/main/java/com/sendgrid/exception/RestException.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.Map;

/**
* Twilio Exceptions.
* Sendgrid Exceptions.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class RestException {
Expand All @@ -24,11 +24,11 @@ public class RestException {
private final Map<String, Object> details;

/**
* Initialize a Twilio Rest Exception.
* Initialize a Sendgrid Rest Exception.
*
* @param status HTTP status code
* @param message message of exception
* @param code Twilio status code
* @param code Sendgrid status code
* @param moreInfo further information, if there is any
*/
@JsonCreator
Expand Down
39 changes: 20 additions & 19 deletions src/main/java/com/sendgrid/http/ApiKeyRestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ApiKeyRestClient {
private final String apiKey;
Expand Down Expand Up @@ -41,6 +42,7 @@ public Response request(final Request request) {
if (userAgentExtensions != null && !userAgentExtensions.isEmpty()) {
request.setUserAgentExtensions(userAgentExtensions);
}
request.buildUrl();
logRequest(request);

Response response = httpClient.makeRequest(request);
Expand Down Expand Up @@ -87,24 +89,23 @@ public ApiKeyRestClient build() {


public void logRequest(final Request request) {
// if (logger.isDebugEnabled()) {
// logger.debug("-- BEGIN Twilio API Request --");
// logger.debug("request method: " + request.getMethod());
// // TODO: URL Encode query params before logging.
// logger.debug("request URL: " + request.getUrl().toString());
//
// final Map<String, String> headerParams = request.getHeaders();
//
// if (headerParams != null && !headerParams.isEmpty()) {
// logger.debug("header parameters: ");
// for (String key : headerParams.keySet()) {
// if (!key.toLowerCase().contains("authorization")) {
// logger.debug(key + ": " + headerParams.get(key));
// }
// }
// }
//
// logger.debug("-- END Twilio API Request --");
// }
if (logger.isDebugEnabled()) {
logger.debug("-- BEGIN Sendgrid API Request --");
logger.debug("request method: " + request.getMethod());
logger.debug("request URL: " + request.getUrl().toString());

final Map<String, String> headerParams = request.getHeaders();

if (headerParams != null && !headerParams.isEmpty()) {
logger.debug("header parameters: ");
for (String key : headerParams.keySet()) {
if (!key.toLowerCase().contains("authorization")) {
logger.debug(key + ": " + headerParams.get(key));
}
}
}

logger.debug("-- END Sendgrid API Request --");
}
}
}
101 changes: 33 additions & 68 deletions src/main/java/com/sendgrid/http/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,100 +4,65 @@
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

public class Request {
@Getter
private final HttpMethod method;
@Getter
private final String url;
private final String endPoint;
@Getter
private final String domain;
@Getter
private String body;
@Getter
private Map<String, String> headers;
private Map<String, String> headers = new HashMap<>();
@Getter
private Map<String, String> queryParams = new HashMap<>();
@Getter
private Map<String, String> pathParams = new HashMap<>();
@Getter
@Setter
private String region;
@Getter
@Setter
private List<String> userAgentExtensions;

@Getter
private String url;
public Request(HttpMethod method, String endPoint, String domain) {
this.method = method;
this.endPoint = endPoint;
this.domain = domain;
}

public void addHeader(String key, String value) {
if (value == null || value.equals("null") || key == null) return;
public void addPathParam(String key, String value) {
pathParams.put(key, value);
}

public void addHeaderParam(String key, String value) {
headers.put(key, 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 = Utility.buildBaseUrl(domain, region, builder.endPoint);
baseUrl = Utility.buildWithPathParams(baseUrl, builder.pathParams);
baseUrl = Utility.buildWithQueryParams(baseUrl, builder.queryParams);
public void addQueryParam(String key, String value) {
queryParams.put(key, value);
}

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

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

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

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

public Builder addPathParam(String key, String value) {
this.pathParams.put(key, value);
return this;
}

public Builder addHeaderParam(String key, String value) {
headers.put(key, value);
return this;
}

/*
* Sendgrid follows Resource Query Language(RQL) for Query parameters instead of following standard query parameters.
* 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 and pass it as a query parameter in the query field.
*/
public Builder addQueryParam(String key, String 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);
}
public void buildUrl(final String url) {
this.url = url;
}


}
2 changes: 1 addition & 1 deletion src/main/java/com/sendgrid/http/auth/TokenStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class TokenStrategy implements AuthStrategy {
private final String token;
@Override
public void applyAuth(Request request) {
request.addHeader("Authorization", "Bearer " + token);
request.addHeaderParam("Authorization", "Bearer " + token);
}
}
Loading