Skip to content

Commit 433326b

Browse files
Merge pull request #15 from belfazt/make-request-object-have-sensible-default-13
Update the request object with sensible defaults and access methods, #13
2 parents 84d7508 + e3ec8ae commit 433326b

File tree

5 files changed

+151
-79
lines changed

5 files changed

+151
-79
lines changed

README.md

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ Here is a quick example:
5959
Client client = new Client();
6060

6161
Request request = new Request();
62-
request.baseUri = "api.test.com";
63-
request.method = Method.GET;
62+
request.setBaseUri("api.test.com");
63+
request.setMethod(Method.GET);
6464
String param = "param";
65-
request.endpoint = "/your/api/" + param + "/call";
65+
request.setEndpoint("/your/api/" + param + "/call");
6666

6767
try {
6868
Response response = client.api(request);
@@ -77,17 +77,13 @@ try {
7777
`POST /your/api/{param}/call` with headers, query parameters and a request body.
7878

7979
```java
80-
Map<String,String> requestHeaders = new HashMap<String, String>();
81-
requestHeaders.put("Authorization", "Bearer YOUR_API_KEY");
82-
request.headers = requestHeaders;
83-
Map<String,String> queryParams = new HashMap<String, String>();
84-
queryParams.put("limit", "100");
85-
queryParams.put("offset", "0");
86-
request.queryParams = queryParams;
87-
request.body ="{\"name\": \"My Request Body\"}";
88-
request.method = Method.POST;
80+
request.addHeader("Authorization", "Bearer YOUR_API_KEY");
81+
request.addQueryParam("limit", "100");
82+
request.addQueryParam("offset", "0");
83+
request.setBody("{\"name\": \"My Request Body\"}");
84+
request.setMethod(Method.POST);
8985
String param = "param";
90-
request.endpoint = "/your/api/" + param + "/call";
86+
request.setEndpoint("/your/api/" + param + "/call");
9187

9288
try {
9389
Response response = client.api(request);

examples/Example.java

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,16 @@ public static void main(String[] args) throws IOException {
1717
Client client = new Client();
1818

1919
Request request = new Request();
20-
request.baseUri = "api.sendgrid.com";
21-
Map<String,String> requestHeaders = new HashMap<String, String>();
22-
requestHeaders.put("Authorization", "Bearer " + System.getenv("SENDGRID_API_KEY"));
23-
request.headers = requestHeaders;
20+
request.setBaseUri("api.sendgrid.com");
21+
request.addHeader("Authorization", "Bearer " + System.getenv("SENDGRID_API_KEY"));
2422

2523
Response response = new Response();
2624

2725
// GET Collection
28-
request.method = Method.GET;
29-
request.endpoint = "/v3/api_keys";
30-
Map<String,String> queryParams = new HashMap<String, String>();
31-
queryParams.put("limit", "100");
32-
queryParams.put("offset", "0");
33-
request.queryParams = queryParams;
26+
request.setMethod(Method.GET);
27+
request.setEndpoint("/v3/api_keys");
28+
request.addQueryParam("limit", "100");
29+
request.addQueryParam("offset", "0");
3430
try {
3531
response = client.api(request);
3632
System.out.println(response.statusCode);
@@ -39,13 +35,13 @@ public static void main(String[] args) throws IOException {
3935
} catch (IOException ex) {
4036
throw ex;
4137
}
42-
request.queryParams = null;
38+
request.clearQueryParams();
4339

4440
// POST
45-
request.method = Method.POST;
46-
request.endpoint = "/v3/api_keys";
47-
request.body =
48-
"{\"name\": \"My api Key\",\"scopes\": [\"mail.send\",\"alerts.create\",\"alerts.read\"]}";
41+
request.setMethod(Method.POST);
42+
request.setEndpoint("/v3/api_keys");
43+
request.setBody("{\"name\": \"My api Key\",\"scopes\": [\"mail.send\",\"alerts.create\",\"alerts.read\"]}");
44+
4945
try {
5046
response = client.api(request);
5147
System.out.println(response.statusCode);
@@ -62,11 +58,11 @@ public static void main(String[] args) throws IOException {
6258
} catch (IOException ex) {
6359
throw ex;
6460
}
65-
request.body = "";
61+
request.clearBody();
6662

6763
// GET Single
68-
request.method = Method.GET;
69-
request.endpoint = "/v3/api_keys/" + apiKeyId;
64+
request.setMethod(Method.GET);
65+
request.setEndpoint("/v3/api_keys/" + apiKeyId);
7066
try {
7167
response = client.api(request);
7268
System.out.println(response.statusCode);
@@ -77,8 +73,8 @@ public static void main(String[] args) throws IOException {
7773
}
7874

7975
// PATCH
80-
request.method = Method.PATCH;
81-
request.body = "{\"name\": \"A New Hope\"}";
76+
request.setMethod(Method.PATCH);
77+
request.setBody("{\"name\": \"A New Ho}");
8278
try {
8379
response = client.api(request);
8480
System.out.println(response.statusCode);
@@ -87,12 +83,11 @@ public static void main(String[] args) throws IOException {
8783
} catch (IOException ex) {
8884
throw ex;
8985
}
90-
request.body = "";
86+
request.clearBody();
9187

9288
// PUT
93-
request.method = Method.PUT;
94-
request.body =
95-
"{\"name\": \"A New Hope\",\"scopes\": [\"user.profile.read\",\"user.profile.update\"]}";
89+
request.setMethod(Method.PUT);
90+
request.setBody("{\"name\": \"A New Hope\",\"scopes\": [\"user.profile.read\",\"user.profile.update\"]}");
9691
try {
9792
response = client.api(request);
9893
System.out.println(response.statusCode);
@@ -101,10 +96,10 @@ public static void main(String[] args) throws IOException {
10196
} catch (IOException ex) {
10297
throw ex;
10398
}
104-
request.body = "";
99+
request.clearBody();
105100

106101
// DELETE
107-
request.method = Method.DELETE;
102+
request.setMethod(Method.DELETE);
108103
try {
109104
response = client.api(request);
110105
System.out.println(response.statusCode);

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

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,14 @@ public Response get(Request request) throws URISyntaxException, IOException {
148148
HttpGet httpGet = null;
149149

150150
try {
151-
uri = buildUri(request.baseUri, request.endpoint, request.queryParams);
151+
uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams());
152152
httpGet = new HttpGet(uri.toString());
153153
} catch (URISyntaxException ex) {
154154
throw ex;
155155
}
156156

157-
if (request.headers != null) {
158-
for (Map.Entry<String, String> entry : request.headers.entrySet()) {
157+
if (request.getHeaders() != null) {
158+
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
159159
httpGet.setHeader(entry.getKey(), entry.getValue());
160160
}
161161
}
@@ -171,20 +171,20 @@ public Response post(Request request) throws URISyntaxException, IOException {
171171
HttpPost httpPost = null;
172172

173173
try {
174-
uri = buildUri(request.baseUri, request.endpoint, request.queryParams);
174+
uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams());
175175
httpPost = new HttpPost(uri.toString());
176176
} catch (URISyntaxException ex) {
177177
throw ex;
178178
}
179179

180-
if (request.headers != null) {
181-
for (Map.Entry<String, String> entry : request.headers.entrySet()) {
180+
if (request.getHeaders() != null) {
181+
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
182182
httpPost.setHeader(entry.getKey(), entry.getValue());
183183
}
184184
}
185185

186-
httpPost.setEntity(new StringEntity(request.body, Charset.forName("UTF-8")));
187-
if (request.body != "") {
186+
httpPost.setEntity(new StringEntity(request.getBody(), Charset.forName("UTF-8")));
187+
if (request.getBody() != "") {
188188
httpPost.setHeader("Content-Type", "application/json");
189189
}
190190

@@ -200,20 +200,20 @@ public Response patch(Request request) throws URISyntaxException, IOException {
200200
HttpPatch httpPatch = null;
201201

202202
try {
203-
uri = buildUri(request.baseUri, request.endpoint, request.queryParams);
203+
uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams());
204204
httpPatch = new HttpPatch(uri.toString());
205205
} catch (URISyntaxException ex) {
206206
throw ex;
207207
}
208208

209-
if (request.headers != null) {
210-
for (Map.Entry<String, String> entry : request.headers.entrySet()) {
209+
if (request.getHeaders() != null) {
210+
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
211211
httpPatch.setHeader(entry.getKey(), entry.getValue());
212212
}
213213
}
214214

215-
httpPatch.setEntity(new StringEntity(request.body, Charset.forName("UTF-8")));
216-
if (request.body != "") {
215+
httpPatch.setEntity(new StringEntity(request.getBody(), Charset.forName("UTF-8")));
216+
if (request.getBody() != "") {
217217
httpPatch.setHeader("Content-Type", "application/json");
218218
}
219219
return executeApiCall(httpPatch);
@@ -228,20 +228,20 @@ public Response put(Request request) throws URISyntaxException, IOException {
228228
HttpPut httpPut = null;
229229

230230
try {
231-
uri = buildUri(request.baseUri, request.endpoint, request.queryParams);
231+
uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams());
232232
httpPut = new HttpPut(uri.toString());
233233
} catch (URISyntaxException ex) {
234234
throw ex;
235235
}
236236

237-
if (request.headers != null) {
238-
for (Map.Entry<String, String> entry : request.headers.entrySet()) {
237+
if (request.getHeaders() != null) {
238+
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
239239
httpPut.setHeader(entry.getKey(), entry.getValue());
240240
}
241241
}
242242

243-
httpPut.setEntity(new StringEntity(request.body, Charset.forName("UTF-8")));
244-
if (request.body != "") {
243+
httpPut.setEntity(new StringEntity(request.getBody(), Charset.forName("UTF-8")));
244+
if (request.getBody() != "") {
245245
httpPut.setHeader("Content-Type", "application/json");
246246
}
247247

@@ -256,20 +256,20 @@ public Response delete(Request request) throws URISyntaxException, IOException {
256256
HttpDeleteWithBody httpDelete = null;
257257

258258
try {
259-
uri = buildUri(request.baseUri, request.endpoint, request.queryParams);
259+
uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams());
260260
httpDelete = new HttpDeleteWithBody(uri.toString());
261261
} catch (URISyntaxException ex) {
262262
throw ex;
263263
}
264264

265-
if (request.headers != null) {
266-
for (Map.Entry<String, String> entry : request.headers.entrySet()) {
265+
if (request.getHeaders() != null) {
266+
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
267267
httpDelete.setHeader(entry.getKey(), entry.getValue());
268268
}
269269
}
270270

271-
httpDelete.setEntity(new StringEntity(request.body, Charset.forName("UTF-8")));
272-
if (request.body != "") {
271+
httpDelete.setEntity(new StringEntity(request.getBody(), Charset.forName("UTF-8")));
272+
if (request.getBody() != "") {
273273
httpDelete.setHeader("Content-Type", "application/json");
274274
}
275275

@@ -287,7 +287,7 @@ private Response executeApiCall(HttpRequestBase httpPost) throws IOException {
287287
//throwing IOException here to not break API behavior.
288288
throw new IOException("Request returned status Code "+statusLine.getStatusCode()+"Body:"+(response!=null?response.body:null));
289289
}
290-
290+
291291
} finally {
292292
if (serverResponse != null) {
293293
serverResponse.close();
@@ -301,10 +301,10 @@ private Response executeApiCall(HttpRequestBase httpPost) throws IOException {
301301
*/
302302
public Response api(Request request) throws IOException {
303303
try {
304-
if (request.method == null) {
304+
if (request.getMethod() == null) {
305305
throw new IOException("We only support GET, PUT, PATCH, POST and DELETE.");
306306
}
307-
switch (request.method) {
307+
switch (request.getMethod()) {
308308
case GET:
309309
return get(request);
310310
case POST:

0 commit comments

Comments
 (0)