Skip to content

Commit 02034bb

Browse files
Diego Andres Camargodiegoc-am
authored andcommitted
Made Response class variables private, issue #12
1 parent 84d7508 commit 02034bb

File tree

5 files changed

+59
-36
lines changed

5 files changed

+59
-36
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ request.endpoint = "/your/api/" + param + "/call";
6666

6767
try {
6868
Response response = client.api(request);
69-
System.out.println(response.statusCode);
70-
System.out.println(response.body);
71-
System.out.println(response.headers);
69+
System.out.println(response.getStatusCode());
70+
System.out.println(response.getBody());
71+
System.out.println(response.getHeaders());
7272
} catch (IOException ex) {
7373
throw ex;
7474
}
@@ -91,9 +91,9 @@ request.endpoint = "/your/api/" + param + "/call";
9191

9292
try {
9393
Response response = client.api(request);
94-
System.out.println(response.statusCode);
95-
System.out.println(response.body);
96-
System.out.println(response.headers);
94+
System.out.println(response.getStatusCode());
95+
System.out.println(response.getBody());
96+
System.out.println(response.getHeaders());
9797
} catch (IOException ex) {
9898
throw ex;
9999
}

examples/Example.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public static void main(String[] args) throws IOException {
3333
request.queryParams = queryParams;
3434
try {
3535
response = client.api(request);
36-
System.out.println(response.statusCode);
37-
System.out.println(response.body);
38-
System.out.println(response.headers);
36+
System.out.println(response.getStatusCode());
37+
System.out.println(response.getBody());
38+
System.out.println(response.getHeaders());
3939
} catch (IOException ex) {
4040
throw ex;
4141
}
@@ -48,16 +48,16 @@ public static void main(String[] args) throws IOException {
4848
"{\"name\": \"My api Key\",\"scopes\": [\"mail.send\",\"alerts.create\",\"alerts.read\"]}";
4949
try {
5050
response = client.api(request);
51-
System.out.println(response.statusCode);
52-
System.out.println(response.body);
53-
System.out.println(response.headers);
51+
System.out.println(response.getStatusCode());
52+
System.out.println(response.getBody());
53+
System.out.println(response.getHeaders());
5454
} catch (IOException ex) {
5555
throw ex;
5656
}
5757
String apiKeyId = "";
5858
try {
5959
ObjectMapper mapper = new ObjectMapper();
60-
JsonNode json = mapper.readTree(response.body);
60+
JsonNode json = mapper.readTree(response.getBody());
6161
apiKeyId = json.path("api_key_id").asText();
6262
} catch (IOException ex) {
6363
throw ex;
@@ -69,9 +69,9 @@ public static void main(String[] args) throws IOException {
6969
request.endpoint = "/v3/api_keys/" + apiKeyId;
7070
try {
7171
response = client.api(request);
72-
System.out.println(response.statusCode);
73-
System.out.println(response.body);
74-
System.out.println(response.headers);
72+
System.out.println(response.getStatusCode());
73+
System.out.println(response.getBody());
74+
System.out.println(response.getHeaders());
7575
} catch (IOException ex) {
7676
throw ex;
7777
}
@@ -81,9 +81,9 @@ public static void main(String[] args) throws IOException {
8181
request.body = "{\"name\": \"A New Hope\"}";
8282
try {
8383
response = client.api(request);
84-
System.out.println(response.statusCode);
85-
System.out.println(response.body);
86-
System.out.println(response.headers);
84+
System.out.println(response.getStatusCode());
85+
System.out.println(response.getBody());
86+
System.out.println(response.getHeaders());
8787
} catch (IOException ex) {
8888
throw ex;
8989
}
@@ -95,9 +95,9 @@ public static void main(String[] args) throws IOException {
9595
"{\"name\": \"A New Hope\",\"scopes\": [\"user.profile.read\",\"user.profile.update\"]}";
9696
try {
9797
response = client.api(request);
98-
System.out.println(response.statusCode);
99-
System.out.println(response.body);
100-
System.out.println(response.headers);
98+
System.out.println(response.getStatusCode());
99+
System.out.println(response.getBody());
100+
System.out.println(response.getHeaders());
101101
} catch (IOException ex) {
102102
throw ex;
103103
}
@@ -107,8 +107,8 @@ public static void main(String[] args) throws IOException {
107107
request.method = Method.DELETE;
108108
try {
109109
response = client.api(request);
110-
System.out.println(response.statusCode);
111-
System.out.println(response.headers);
110+
System.out.println(response.getStatusCode());
111+
System.out.println(response.getHeaders());
112112
} catch (IOException ex) {
113113
throw ex;
114114
}

src/main/java/com/sendgrid/Client.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,9 @@ private Response executeApiCall(HttpRequestBase httpPost) throws IOException {
285285
final StatusLine statusLine = serverResponse.getStatusLine();
286286
if(statusLine.getStatusCode()>=300){
287287
//throwing IOException here to not break API behavior.
288-
throw new IOException("Request returned status Code "+statusLine.getStatusCode()+"Body:"+(response!=null?response.body:null));
288+
throw new IOException("Request returned status Code "+statusLine.getStatusCode()+"Body:"+(response!=null?response.getBody():null));
289289
}
290-
290+
291291
} finally {
292292
if (serverResponse != null) {
293293
serverResponse.close();

src/main/java/com/sendgrid/Response.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
* Class Response provides a standard interface to an API's response.
77
*/
88
public class Response {
9-
public int statusCode;
10-
public String body;
11-
public Map<String,String> headers;
9+
private int statusCode;
10+
private String body;
11+
private Map<String, String> headers;
1212

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

35+
public int getStatusCode() {
36+
return this.statusCode;
37+
}
38+
39+
public String getBody() {
40+
return this.body;
41+
}
42+
43+
public Map<String, String> getHeaders() {
44+
return this.headers;
45+
}
46+
47+
public void setStatusCode(int statusCode) {
48+
this.statusCode = statusCode;
49+
}
50+
51+
public void setBody(String body) {
52+
this.body = body;
53+
}
54+
55+
public void setHeaders(Map<String, String> headers) {
56+
this.headers = headers;
57+
}
3558
}

src/test/java/com/sendgrid/ClientTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ public void testGetResponse() {
101101
Assert.assertTrue(errors.toString(), false);
102102
}
103103

104-
Assert.assertTrue(testResponse.statusCode == 200);
105-
Assert.assertEquals(testResponse.body, "{\"message\":\"success\"}");
104+
Assert.assertTrue(testResponse.getStatusCode() == 200);
105+
Assert.assertEquals(testResponse.getBody(), "{\"message\":\"success\"}");
106106
Map<String,String> headers = new HashMap<String,String>();
107107
for (Header h:mockedHeaders) {
108108
headers.put(h.getName(), h.getValue());
109109
}
110-
Assert.assertEquals(testResponse.headers, headers);
110+
Assert.assertEquals(testResponse.getHeaders(), headers);
111111
}
112112

113113
public void testMethod(Method method, int statusCode) {
@@ -140,16 +140,16 @@ public void testMethod(Method method, int statusCode) {
140140
Assert.assertTrue(errors.toString(), false);
141141
}
142142

143-
Assert.assertTrue(testResponse.statusCode == statusCode);
143+
Assert.assertTrue(testResponse.getStatusCode() == statusCode);
144144
if (method != Method.DELETE) {
145-
Assert.assertEquals(testResponse.body, "{\"message\":\"success\"}");
145+
Assert.assertEquals(testResponse.getBody(), "{\"message\":\"success\"}");
146146
}
147-
Assert.assertEquals(testResponse.body, "{\"message\":\"success\"}");
147+
Assert.assertEquals(testResponse.getBody(), "{\"message\":\"success\"}");
148148
Map<String,String> headers = new HashMap<String,String>();
149149
for (Header h:mockedHeaders) {
150150
headers.put(h.getName(), h.getValue());
151151
}
152-
Assert.assertEquals(testResponse.headers, headers);
152+
Assert.assertEquals(testResponse.getHeaders(), headers);
153153
}
154154

155155
@Test

0 commit comments

Comments
 (0)