Skip to content

Commit 638d55c

Browse files
Changed Request and Response variables to be non-redundant
1 parent 3faeedd commit 638d55c

File tree

7 files changed

+118
-118
lines changed

7 files changed

+118
-118
lines changed

CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ source ./sendgrid.env
8989
```bash
9090
./gradelew build
9191
cd examples
92-
javac -classpath ./commons-logging-1.2.jar:./httpcore-4.4.4.jar:./httpclient-4.5.2.jar:./jackson-databind-2.7.3.jar:./jackson-annotations-2.7.0.jar:./jackson-core-2.7.3.jar:../target/http-1.0-SNAPSHOT.jar:. Example.java && java -classpath ./commons-logging-1.2.jar:./httpcore-4.4.4.jar:./httpclient-4.5.2.jar:./jackson-databind-2.7.3.jar:./jackson-annotations-2.7.0.jar:./jackson-core-2.7.3.jar:../target/http-1.0-SNAPSHOT.jar:. Example
92+
javac -classpath ./commons-logging-1.2.jar:./httpcore-4.4.4.jar:./httpclient-4.5.2.jar:./jackson-databind-2.7.3.jar:./jackson-annotations-2.7.0.jar:./jackson-core-2.7.3.jar:../repo/com/sendgrid/2.0.0/sendgrid-java-http-client-2.0.0-jar.jar:. Example.java && java -classpath ./commons-logging-1.2.jar:./httpcore-4.4.4.jar:./httpclient-4.5.2.jar:./jackson-databind-2.7.3.jar:./jackson-annotations-2.7.0.jar:./jackson-core-2.7.3.jar:../repo/com/sendgrid/2.0.0/sendgrid-java-http-client-2.0.0-jar.jar:. Example
9393
```
9494

9595
<a name="understanding_the_codebase"></a>

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ request.endpoint = "/your/api/" + param + "/call";
6161
try {
6262
Response response = client.api(request);
6363
System.out.println(response.statusCode);
64-
System.out.println(response.responseBody);
65-
System.out.println(response.responseHeaders);
64+
System.out.println(response.body);
65+
System.out.println(response.headers);
6666
} catch (IOException ex) {
6767
throw ex;
6868
}
@@ -74,21 +74,21 @@ try {
7474
Map<String,String> requestHeaders = new HashMap<String, String>();
7575
requestHeaders.put("Authorization", "Bearer " + System.getenv("SENDGRID_API_KEY"));
7676
requestHeaders.put("Content-Type", "application/json");
77-
request.requestHeaders = requestHeaders;
77+
request.headers = requestHeaders;
7878
Map<String,String> queryParams = new HashMap<String, String>();
7979
queryParams.put("limit", "100");
8080
queryParams.put("offset", "0");
8181
request.queryParams = queryParams;
82-
request.requestBody ="{\"name\": \"My Request Body\"}";
82+
request.body ="{\"name\": \"My Request Body\"}";
8383
request.method = Method.POST;
8484
String param = "param";
8585
request.endpoint = "/your/api/" + param + "/call";
8686

8787
try {
8888
Response response = client.api(request);
8989
System.out.println(response.statusCode);
90-
System.out.println(response.responseBody);
91-
System.out.println(response.responseHeaders);
90+
System.out.println(response.body);
91+
System.out.println(response.headers);
9292
} catch (IOException ex) {
9393
throw ex;
9494
}
@@ -111,7 +111,7 @@ source ./sendgrid.env
111111
```bash
112112
mvn package
113113
cd examples
114-
javac -classpath ./jackson-databind-2.7.3.jar:./jackson-annotations-2.7.0.jar:./jackson-core-2.7.3.jar:/{path_to}/java-http-client-1.0.0-jar.jar:. Example.java && java -classpath ./jackson-databind-2.7.3.jar:./jackson-annotations-2.7.0.jar:./jackson-core-2.7.3.jar:/{path_to}/java-http-client-1.0.0-jar.jar:. Example
114+
javac -classpath ./jackson-databind-2.7.3.jar:./jackson-annotations-2.7.0.jar:./jackson-core-2.7.3.jar:/{path_to}/java-http-client-2.0.0-jar.jar:. Example.java && java -classpath ./jackson-databind-2.7.3.jar:./jackson-annotations-2.7.0.jar:./jackson-core-2.7.3.jar:/{path_to}/java-http-client-2.0.0-jar.jar:. Example
115115
```
116116

117117
## Roadmap

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ apply plugin: 'maven'
1717
apply plugin: 'signing'
1818

1919
group = 'com.sendgrid'
20-
version = '1.0.0'
20+
version = '2.0.0'
2121
ext.packaging = 'jar'
2222

2323
allprojects {

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

+69-69
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@
3939
public class Client {
4040

4141
private CloseableHttpClient httpClient;
42-
42+
4343
/**
4444
* Constructor for using the default CloseableHttpClient.
4545
*/
4646
public Client() {
4747
this.httpClient = HttpClients.createDefault();
4848
}
49-
49+
5050
/**
5151
* Constructor for passing in an httpClient for mocking.
5252
*
@@ -55,7 +55,7 @@ public Client() {
5555
public Client(CloseableHttpClient httpClient) {
5656
this.httpClient = httpClient;
5757
}
58-
58+
5959
/**
6060
* Add query parameters to a URL.
6161
*
@@ -67,22 +67,22 @@ public URI buildUri(String baseUri, String endpoint, Map<String,String> queryPar
6767
throws URISyntaxException {
6868
URIBuilder builder = new URIBuilder();
6969
URI uri;
70-
70+
7171
builder.setScheme("https");
72-
builder.setHost(baseUri);
73-
builder.setPath(endpoint);
72+
builder.setHost(baseUri);
73+
builder.setPath(endpoint);
7474
if (queryParams != null) {
7575
for (Map.Entry<String, String> entry : queryParams.entrySet()) {
7676
builder.setParameter(entry.getKey(), entry.getValue());
77-
}
77+
}
7878
}
79-
79+
8080
try {
8181
uri = builder.build();
8282
} catch (URISyntaxException ex) {
8383
throw ex;
8484
}
85-
85+
8686
return uri;
8787
}
8888

@@ -94,24 +94,24 @@ public URI buildUri(String baseUri, String endpoint, Map<String,String> queryPar
9494
public Response getResponse(CloseableHttpResponse response) throws IOException {
9595
ResponseHandler<String> handler = new BasicResponseHandler();
9696
String responseBody = "";
97-
97+
9898
int statusCode = response.getStatusLine().getStatusCode();
99-
99+
100100
try {
101101
responseBody = handler.handleResponse(response);
102102
} catch (IOException ex) {
103103
throw ex;
104104
}
105-
105+
106106
Header[] headers = response.getAllHeaders();
107107
Map<String,String> responseHeaders = new HashMap<String,String>();
108108
for (Header h:headers) {
109109
responseHeaders.put(h.getName(), h.getValue());
110110
}
111-
111+
112112
return new Response(statusCode, responseBody, responseHeaders);
113113
}
114-
114+
115115
/**
116116
* Make a GET request and provide the status code, response body and response headers.
117117
*/
@@ -120,21 +120,21 @@ public Response get(Request request) throws URISyntaxException, IOException {
120120
Response response = new Response();
121121
URI uri = null;
122122
HttpGet httpGet = null;
123-
123+
124124
try {
125125
uri = buildUri(request.baseUri, request.endpoint, request.queryParams);
126-
httpGet = new HttpGet(uri.toString());
126+
httpGet = new HttpGet(uri.toString());
127127
} catch (URISyntaxException ex) {
128128
throw ex;
129129
}
130-
131-
if (request.requestHeaders != null) {
132-
for (Map.Entry<String, String> entry : request.requestHeaders.entrySet()) {
130+
131+
if (request.headers != null) {
132+
for (Map.Entry<String, String> entry : request.headers.entrySet()) {
133133
httpGet.setHeader(entry.getKey(), entry.getValue());
134-
}
134+
}
135135
}
136-
137-
136+
137+
138138
try {
139139
serverResponse = httpClient.execute(httpGet);
140140
response = getResponse(serverResponse);
@@ -145,10 +145,10 @@ public Response get(Request request) throws URISyntaxException, IOException {
145145
serverResponse.close();
146146
}
147147
}
148-
148+
149149
return response;
150150
}
151-
151+
152152
/**
153153
* Make a POST request and provide the status code, response body and response headers.
154154
*/
@@ -157,26 +157,26 @@ public Response post(Request request) throws URISyntaxException, IOException {
157157
Response response = new Response();
158158
URI uri = null;
159159
HttpPost httpPost = null;
160-
160+
161161
try {
162162
uri = buildUri(request.baseUri, request.endpoint, request.queryParams);
163-
httpPost = new HttpPost(uri.toString());
163+
httpPost = new HttpPost(uri.toString());
164164
} catch (URISyntaxException ex) {
165165
throw ex;
166166
}
167-
168-
if (request.requestHeaders != null) {
169-
for (Map.Entry<String, String> entry : request.requestHeaders.entrySet()) {
167+
168+
if (request.headers != null) {
169+
for (Map.Entry<String, String> entry : request.headers.entrySet()) {
170170
httpPost.setHeader(entry.getKey(), entry.getValue());
171-
}
171+
}
172172
}
173-
173+
174174
try {
175-
httpPost.setEntity(new StringEntity(request.requestBody));
175+
httpPost.setEntity(new StringEntity(request.body));
176176
} catch (IOException ex) {
177177
throw ex;
178178
}
179-
179+
180180
try {
181181
serverResponse = httpClient.execute(httpPost);
182182
response = getResponse(serverResponse);
@@ -188,38 +188,38 @@ public Response post(Request request) throws URISyntaxException, IOException {
188188
serverResponse.close();
189189
}
190190
}
191-
191+
192192
return response;
193193
}
194194

195195
/**
196196
* Make a PATCH request and provide the status code, response body and response headers.
197-
*/
197+
*/
198198
public Response patch(Request request) throws URISyntaxException, IOException {
199199
CloseableHttpResponse serverResponse = null;
200200
Response response = new Response();
201201
URI uri = null;
202202
HttpPatch httpPatch = null;
203-
203+
204204
try {
205205
uri = buildUri(request.baseUri, request.endpoint, request.queryParams);
206-
httpPatch = new HttpPatch(uri.toString());
206+
httpPatch = new HttpPatch(uri.toString());
207207
} catch (URISyntaxException ex) {
208208
throw ex;
209209
}
210-
211-
if (request.requestHeaders != null) {
212-
for (Map.Entry<String, String> entry : request.requestHeaders.entrySet()) {
210+
211+
if (request.headers != null) {
212+
for (Map.Entry<String, String> entry : request.headers.entrySet()) {
213213
httpPatch.setHeader(entry.getKey(), entry.getValue());
214-
}
214+
}
215215
}
216-
216+
217217
try {
218-
httpPatch.setEntity(new StringEntity(request.requestBody));
218+
httpPatch.setEntity(new StringEntity(request.body));
219219
} catch (IOException ex) {
220220
throw ex;
221221
}
222-
222+
223223
try {
224224
serverResponse = httpClient.execute(httpPatch);
225225
response = getResponse(serverResponse);
@@ -231,7 +231,7 @@ public Response patch(Request request) throws URISyntaxException, IOException {
231231
serverResponse.close();
232232
}
233233
}
234-
234+
235235
return response;
236236
}
237237

@@ -243,27 +243,27 @@ public Response put(Request request) throws URISyntaxException, IOException {
243243
Response response = new Response();
244244
URI uri = null;
245245
HttpPut httpPut = null;
246-
246+
247247
try {
248248
uri = buildUri(request.baseUri, request.endpoint, request.queryParams);
249-
httpPut = new HttpPut(uri.toString());
249+
httpPut = new HttpPut(uri.toString());
250250
} catch (URISyntaxException ex) {
251251
throw ex;
252252
}
253-
254-
if (request.requestHeaders != null) {
255-
for (Map.Entry<String, String> entry : request.requestHeaders.entrySet()) {
253+
254+
if (request.headers != null) {
255+
for (Map.Entry<String, String> entry : request.headers.entrySet()) {
256256
httpPut.setHeader(entry.getKey(), entry.getValue());
257-
}
257+
}
258258
}
259-
260-
259+
260+
261261
try {
262-
httpPut.setEntity(new StringEntity(request.requestBody));
262+
httpPut.setEntity(new StringEntity(request.body));
263263
} catch (IOException ex) {
264264
throw ex;
265265
}
266-
266+
267267
try {
268268
serverResponse = httpClient.execute(httpPut);
269269
response = getResponse(serverResponse);
@@ -275,7 +275,7 @@ public Response put(Request request) throws URISyntaxException, IOException {
275275
serverResponse.close();
276276
}
277277
}
278-
278+
279279
return response;
280280
}
281281

@@ -287,20 +287,20 @@ public Response delete(Request request) throws URISyntaxException, IOException {
287287
Response response = new Response();
288288
URI uri = null;
289289
HttpDelete httpDelete = null;
290-
290+
291291
try {
292292
uri = buildUri(request.baseUri, request.endpoint, request.queryParams);
293-
httpDelete = new HttpDelete(uri.toString());
293+
httpDelete = new HttpDelete(uri.toString());
294294
} catch (URISyntaxException ex) {
295295
throw ex;
296296
}
297-
298-
if (request.requestHeaders != null) {
299-
for (Map.Entry<String, String> entry : request.requestHeaders.entrySet()) {
297+
298+
if (request.headers != null) {
299+
for (Map.Entry<String, String> entry : request.headers.entrySet()) {
300300
httpDelete.setHeader(entry.getKey(), entry.getValue());
301-
}
301+
}
302302
}
303-
303+
304304
try {
305305
serverResponse = httpClient.execute(httpDelete);
306306
response = getResponse(serverResponse);
@@ -312,10 +312,10 @@ public Response delete(Request request) throws URISyntaxException, IOException {
312312
serverResponse.close();
313313
}
314314
}
315-
315+
316316
return response;
317317
}
318-
318+
319319
/**
320320
* A thin wrapper around the HTTP methods.
321321
*/
@@ -325,15 +325,15 @@ public Response api(Request request) throws IOException {
325325
throw new IOException("We only support GET, PUT, PATCH, POST and DELETE.");
326326
}
327327
switch (request.method) {
328-
case GET:
328+
case GET:
329329
return get(request);
330-
case POST:
330+
case POST:
331331
return post(request);
332-
case PUT:
332+
case PUT:
333333
return put(request);
334334
case PATCH:
335335
return patch(request);
336-
case DELETE:
336+
case DELETE:
337337
return delete(request);
338338
default:
339339
throw new IOException("We only support GET, PUT, PATCH, POST and DELETE.");

0 commit comments

Comments
 (0)