Skip to content

Commit bca0cef

Browse files
Merge pull request #14 from belfazt/make-response-have-private-variables-12
Make response have private variables 12
2 parents 66cf550 + 8a731bf commit bca0cef

File tree

5 files changed

+58
-35
lines changed

5 files changed

+58
-35
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ request.setEndpoint("/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
}
@@ -87,9 +87,9 @@ request.setEndpoint("/your/api/" + param + "/call");
8787

8888
try {
8989
Response response = client.api(request);
90-
System.out.println(response.statusCode);
91-
System.out.println(response.body);
92-
System.out.println(response.headers);
90+
System.out.println(response.getStatusCode());
91+
System.out.println(response.getBody());
92+
System.out.println(response.getHeaders());
9393
} catch (IOException ex) {
9494
throw ex;
9595
}

examples/Example.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public static void main(String[] args) throws IOException {
2929
request.addQueryParam("offset", "0");
3030
try {
3131
response = client.api(request);
32-
System.out.println(response.statusCode);
33-
System.out.println(response.body);
34-
System.out.println(response.headers);
32+
System.out.println(response.getStatusCode());
33+
System.out.println(response.getBody());
34+
System.out.println(response.getHeaders());
3535
} catch (IOException ex) {
3636
throw ex;
3737
}
@@ -44,16 +44,16 @@ public static void main(String[] args) throws IOException {
4444

4545
try {
4646
response = client.api(request);
47-
System.out.println(response.statusCode);
48-
System.out.println(response.body);
49-
System.out.println(response.headers);
47+
System.out.println(response.getStatusCode());
48+
System.out.println(response.getBody());
49+
System.out.println(response.getHeaders());
5050
} catch (IOException ex) {
5151
throw ex;
5252
}
5353
String apiKeyId = "";
5454
try {
5555
ObjectMapper mapper = new ObjectMapper();
56-
JsonNode json = mapper.readTree(response.body);
56+
JsonNode json = mapper.readTree(response.getBody());
5757
apiKeyId = json.path("api_key_id").asText();
5858
} catch (IOException ex) {
5959
throw ex;
@@ -65,9 +65,9 @@ public static void main(String[] args) throws IOException {
6565
request.setEndpoint("/v3/api_keys/" + apiKeyId);
6666
try {
6767
response = client.api(request);
68-
System.out.println(response.statusCode);
69-
System.out.println(response.body);
70-
System.out.println(response.headers);
68+
System.out.println(response.getStatusCode());
69+
System.out.println(response.getBody());
70+
System.out.println(response.getHeaders());
7171
} catch (IOException ex) {
7272
throw ex;
7373
}
@@ -77,9 +77,9 @@ public static void main(String[] args) throws IOException {
7777
request.setBody("{\"name\": \"A New Ho}");
7878
try {
7979
response = client.api(request);
80-
System.out.println(response.statusCode);
81-
System.out.println(response.body);
82-
System.out.println(response.headers);
80+
System.out.println(response.getStatusCode());
81+
System.out.println(response.getBody());
82+
System.out.println(response.getHeaders());
8383
} catch (IOException ex) {
8484
throw ex;
8585
}
@@ -90,9 +90,9 @@ public static void main(String[] args) throws IOException {
9090
request.setBody("{\"name\": \"A New Hope\",\"scopes\": [\"user.profile.read\",\"user.profile.update\"]}");
9191
try {
9292
response = client.api(request);
93-
System.out.println(response.statusCode);
94-
System.out.println(response.body);
95-
System.out.println(response.headers);
93+
System.out.println(response.getStatusCode());
94+
System.out.println(response.getBody());
95+
System.out.println(response.getHeaders());
9696
} catch (IOException ex) {
9797
throw ex;
9898
}
@@ -102,8 +102,8 @@ public static void main(String[] args) throws IOException {
102102
request.setMethod(Method.DELETE);
103103
try {
104104
response = client.api(request);
105-
System.out.println(response.statusCode);
106-
System.out.println(response.headers);
105+
System.out.println(response.getStatusCode());
106+
System.out.println(response.getHeaders());
107107
} catch (IOException ex) {
108108
throw ex;
109109
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ 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
}
290290

291291
} finally {

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) {
@@ -138,16 +138,16 @@ public void testMethod(Method method, int statusCode) {
138138
Assert.assertTrue(errors.toString(), false);
139139
}
140140

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

153153
@Test

0 commit comments

Comments
 (0)