Skip to content

Make response have private variables 12 #14

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 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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ request.endpoint = "/your/api/" + param + "/call";

try {
Response response = client.api(request);
System.out.println(response.statusCode);
System.out.println(response.body);
System.out.println(response.headers);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
Expand All @@ -91,9 +91,9 @@ request.endpoint = "/your/api/" + param + "/call";

try {
Response response = client.api(request);
System.out.println(response.statusCode);
System.out.println(response.body);
System.out.println(response.headers);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
Expand Down
36 changes: 18 additions & 18 deletions examples/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public static void main(String[] args) throws IOException {
request.queryParams = queryParams;
try {
response = client.api(request);
System.out.println(response.statusCode);
System.out.println(response.body);
System.out.println(response.headers);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
Expand All @@ -48,16 +48,16 @@ public static void main(String[] args) throws IOException {
"{\"name\": \"My api Key\",\"scopes\": [\"mail.send\",\"alerts.create\",\"alerts.read\"]}";
try {
response = client.api(request);
System.out.println(response.statusCode);
System.out.println(response.body);
System.out.println(response.headers);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
String apiKeyId = "";
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode json = mapper.readTree(response.body);
JsonNode json = mapper.readTree(response.getBody());
apiKeyId = json.path("api_key_id").asText();
} catch (IOException ex) {
throw ex;
Expand All @@ -69,9 +69,9 @@ public static void main(String[] args) throws IOException {
request.endpoint = "/v3/api_keys/" + apiKeyId;
try {
response = client.api(request);
System.out.println(response.statusCode);
System.out.println(response.body);
System.out.println(response.headers);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
Expand All @@ -81,9 +81,9 @@ public static void main(String[] args) throws IOException {
request.body = "{\"name\": \"A New Hope\"}";
try {
response = client.api(request);
System.out.println(response.statusCode);
System.out.println(response.body);
System.out.println(response.headers);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
Expand All @@ -95,9 +95,9 @@ public static void main(String[] args) throws IOException {
"{\"name\": \"A New Hope\",\"scopes\": [\"user.profile.read\",\"user.profile.update\"]}";
try {
response = client.api(request);
System.out.println(response.statusCode);
System.out.println(response.body);
System.out.println(response.headers);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
Expand All @@ -107,8 +107,8 @@ public static void main(String[] args) throws IOException {
request.method = Method.DELETE;
try {
response = client.api(request);
System.out.println(response.statusCode);
System.out.println(response.headers);
System.out.println(response.getStatusCode());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/sendgrid/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,9 @@ private Response executeApiCall(HttpRequestBase httpPost) throws IOException {
final StatusLine statusLine = serverResponse.getStatusLine();
if(statusLine.getStatusCode()>=300){
//throwing IOException here to not break API behavior.
throw new IOException("Request returned status Code "+statusLine.getStatusCode()+"Body:"+(response!=null?response.body:null));
throw new IOException("Request returned status Code "+statusLine.getStatusCode()+"Body:"+(response!=null?response.getBody():null));
}

} finally {
if (serverResponse != null) {
serverResponse.close();
Expand Down
29 changes: 26 additions & 3 deletions src/main/java/com/sendgrid/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* Class Response provides a standard interface to an API's response.
*/
public class Response {
public int statusCode;
public String body;
public Map<String,String> headers;
private int statusCode;
private String body;
private Map<String, String> headers;

/**
* Set the API's response.
Expand All @@ -32,4 +32,27 @@ public void reset() {
this.headers = null;
}

public int getStatusCode() {
return this.statusCode;
}

public String getBody() {
return this.body;
}

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

public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}

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

public void setHeaders(Map<String, String> headers) {
this.headers = headers;
}
}
14 changes: 7 additions & 7 deletions src/test/java/com/sendgrid/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ public void testGetResponse() {
Assert.assertTrue(errors.toString(), false);
}

Assert.assertTrue(testResponse.statusCode == 200);
Assert.assertEquals(testResponse.body, "{\"message\":\"success\"}");
Assert.assertTrue(testResponse.getStatusCode() == 200);
Assert.assertEquals(testResponse.getBody(), "{\"message\":\"success\"}");
Map<String,String> headers = new HashMap<String,String>();
for (Header h:mockedHeaders) {
headers.put(h.getName(), h.getValue());
}
Assert.assertEquals(testResponse.headers, headers);
Assert.assertEquals(testResponse.getHeaders(), headers);
}

public void testMethod(Method method, int statusCode) {
Expand Down Expand Up @@ -140,16 +140,16 @@ public void testMethod(Method method, int statusCode) {
Assert.assertTrue(errors.toString(), false);
}

Assert.assertTrue(testResponse.statusCode == statusCode);
Assert.assertTrue(testResponse.getStatusCode() == statusCode);
if (method != Method.DELETE) {
Assert.assertEquals(testResponse.body, "{\"message\":\"success\"}");
Assert.assertEquals(testResponse.getBody(), "{\"message\":\"success\"}");
}
Assert.assertEquals(testResponse.body, "{\"message\":\"success\"}");
Assert.assertEquals(testResponse.getBody(), "{\"message\":\"success\"}");
Map<String,String> headers = new HashMap<String,String>();
for (Header h:mockedHeaders) {
headers.put(h.getName(), h.getValue());
}
Assert.assertEquals(testResponse.headers, headers);
Assert.assertEquals(testResponse.getHeaders(), headers);
}

@Test
Expand Down