Skip to content

Update the request object with sensible defaults and access methods, #13 #15

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

Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,9 @@ try {
`POST /your/api/{param}/call` with headers, query parameters and a request body.

```java
Map<String,String> requestHeaders = new HashMap<String, String>();
requestHeaders.put("Authorization", "Bearer YOUR_API_KEY");
request.headers = requestHeaders;
Map<String,String> queryParams = new HashMap<String, String>();
queryParams.put("limit", "100");
queryParams.put("offset", "0");
request.queryParams = queryParams;
request.getHeaders().put("Authorization", "Bearer YOUR_API_KEY");
request.getQueryParams().put("limit", "100");
request.getQueryParams().put("offset", "0");
request.body ="{\"name\": \"My Request Body\"}";
request.method = Method.POST;
String param = "param";
Expand Down
12 changes: 4 additions & 8 deletions examples/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,15 @@ public static void main(String[] args) throws IOException {

Request request = new Request();
request.baseUri = "api.sendgrid.com";
Map<String,String> requestHeaders = new HashMap<String, String>();
requestHeaders.put("Authorization", "Bearer " + System.getenv("SENDGRID_API_KEY"));
request.headers = requestHeaders;
request.getHeaders().put("Authorization", "Bearer " + System.getenv("SENDGRID_API_KEY"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a setter here instead?

e.g. request.addHeader("Authorization", "Bearer " + System.getenv("SENDGRID_API_KEY"));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


Response response = new Response();

// GET Collection
request.method = Method.GET;
request.endpoint = "/v3/api_keys";
Map<String,String> queryParams = new HashMap<String, String>();
queryParams.put("limit", "100");
queryParams.put("offset", "0");
request.queryParams = queryParams;
request.getQueryParams().put("limit", "100");
request.getQueryParams().put("offset", "0");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a setter here instead?

e.g. request.addQueryParam("offset", "0");

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

try {
response = client.api(request);
System.out.println(response.statusCode);
Expand All @@ -39,7 +35,7 @@ public static void main(String[] args) throws IOException {
} catch (IOException ex) {
throw ex;
}
request.queryParams = null;
request.getQueryParams().clear();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about request.clearQueryParams();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


// POST
request.method = Method.POST;
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/com/sendgrid/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ public Response get(Request request) throws URISyntaxException, IOException {
HttpGet httpGet = null;

try {
uri = buildUri(request.baseUri, request.endpoint, request.queryParams);
uri = buildUri(request.baseUri, request.endpoint, request.getQueryParams());
httpGet = new HttpGet(uri.toString());
} catch (URISyntaxException ex) {
throw ex;
}

if (request.headers != null) {
for (Map.Entry<String, String> entry : request.headers.entrySet()) {
if (request.getHeaders() != null) {
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
httpGet.setHeader(entry.getKey(), entry.getValue());
}
}
Expand All @@ -171,14 +171,14 @@ public Response post(Request request) throws URISyntaxException, IOException {
HttpPost httpPost = null;

try {
uri = buildUri(request.baseUri, request.endpoint, request.queryParams);
uri = buildUri(request.baseUri, request.endpoint, request.getQueryParams());
httpPost = new HttpPost(uri.toString());
} catch (URISyntaxException ex) {
throw ex;
}

if (request.headers != null) {
for (Map.Entry<String, String> entry : request.headers.entrySet()) {
if (request.getHeaders() != null) {
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
}
Expand All @@ -200,14 +200,14 @@ public Response patch(Request request) throws URISyntaxException, IOException {
HttpPatch httpPatch = null;

try {
uri = buildUri(request.baseUri, request.endpoint, request.queryParams);
uri = buildUri(request.baseUri, request.endpoint, request.getQueryParams());
httpPatch = new HttpPatch(uri.toString());
} catch (URISyntaxException ex) {
throw ex;
}

if (request.headers != null) {
for (Map.Entry<String, String> entry : request.headers.entrySet()) {
if (request.getHeaders() != null) {
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
httpPatch.setHeader(entry.getKey(), entry.getValue());
}
}
Expand All @@ -228,14 +228,14 @@ public Response put(Request request) throws URISyntaxException, IOException {
HttpPut httpPut = null;

try {
uri = buildUri(request.baseUri, request.endpoint, request.queryParams);
uri = buildUri(request.baseUri, request.endpoint, request.getQueryParams());
httpPut = new HttpPut(uri.toString());
} catch (URISyntaxException ex) {
throw ex;
}

if (request.headers != null) {
for (Map.Entry<String, String> entry : request.headers.entrySet()) {
if (request.getHeaders() != null) {
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
httpPut.setHeader(entry.getKey(), entry.getValue());
}
}
Expand All @@ -256,14 +256,14 @@ public Response delete(Request request) throws URISyntaxException, IOException {
HttpDeleteWithBody httpDelete = null;

try {
uri = buildUri(request.baseUri, request.endpoint, request.queryParams);
uri = buildUri(request.baseUri, request.endpoint, request.getQueryParams());
httpDelete = new HttpDeleteWithBody(uri.toString());
} catch (URISyntaxException ex) {
throw ex;
}

if (request.headers != null) {
for (Map.Entry<String, String> entry : request.headers.entrySet()) {
if (request.getHeaders() != null) {
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
httpDelete.setHeader(entry.getKey(), entry.getValue());
}
}
Expand All @@ -287,7 +287,7 @@ private Response executeApiCall(HttpRequestBase httpPost) throws IOException {
//throwing IOException here to not break API behavior.
throw new IOException("Request returned status Code "+statusLine.getStatusCode()+"Body:"+(response!=null?response.body:null));
}

} finally {
if (serverResponse != null) {
serverResponse.close();
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/com/sendgrid/Request.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sendgrid;

import java.util.Map;
import java.util.HashMap;

/**
* Class Response provides a standard interface to an API's HTTP request.
Expand All @@ -10,10 +11,12 @@ public class Request {
public String baseUri;
public String endpoint;
public String body;
public Map<String,String> headers;
public Map<String,String> queryParams;
private final Map<String, String> headers;
private final Map<String, String> queryParams;

public Request() {
this.headers = new HashMap<String, String>();
this.queryParams = new HashMap<String, String>();
this.reset();
}

Expand All @@ -25,7 +28,15 @@ public void reset() {
this.baseUri = "";
this.endpoint = "";
this.body = "";
this.headers = null;
this.queryParams = null;
this.headers.clear();
this.queryParams.clear();
}

public Map<String, String> getHeaders() {
return this.headers;
}

public Map<String, String> getQueryParams() {
return this.queryParams;
}
}
4 changes: 1 addition & 3 deletions src/test/java/com/sendgrid/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ public void testMethod(Method method, int statusCode) {
request.body = "{\"test\":\"testResult\"}";
}
request.endpoint = "/test";
Map<String,String> requestHeaders = new HashMap<String, String>();
requestHeaders.put("Authorization", "Bearer XXXX");
request.headers = requestHeaders;
request.getHeaders().put("Authorization", "Bearer XXXX");
Client client = new Client(httpClient);
testResponse = client.get(request);
} catch (URISyntaxException | IOException ex) {
Expand Down