Skip to content

Commit 70876ae

Browse files
committed
Add setters and getters for all the variables in the request object, issue #13
1 parent 50c0dcb commit 70876ae

File tree

5 files changed

+128
-57
lines changed

5 files changed

+128
-57
lines changed

README.md

Lines changed: 6 additions & 6 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,9 +77,9 @@ try {
7777
`POST /your/api/{param}/call` with headers, query parameters and a request body.
7878

7979
```java
80-
request.getHeaders().put("Authorization", "Bearer YOUR_API_KEY");
81-
request.getQueryParams().put("limit", "100");
82-
request.getQueryParams().put("offset", "0");
80+
request.addHeader("Authorization", "Bearer YOUR_API_KEY");
81+
request.addQueryParam("limit", "100");
82+
request.addQueryParam("offset", "0");
8383
request.body ="{\"name\": \"My Request Body\"}";
8484
request.method = Method.POST;
8585
String param = "param";

examples/Example.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +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-
request.getHeaders().put("Authorization", "Bearer " + System.getenv("SENDGRID_API_KEY"));
20+
request.setBaseUri("api.sendgrid.com");
21+
request.addHeader("Authorization", "Bearer " + System.getenv("SENDGRID_API_KEY"));
2222

2323
Response response = new Response();
2424

2525
// GET Collection
26-
request.method = Method.GET;
27-
request.endpoint = "/v3/api_keys";
28-
request.getQueryParams().put("limit", "100");
29-
request.getQueryParams().put("offset", "0");
26+
request.setMethod(Method.GET);
27+
request.setEndpoint("/v3/api_keys");
28+
request.addQueryParam("limit", "100");
29+
request.addQueryParam("offset", "0");
3030
try {
3131
response = client.api(request);
3232
System.out.println(response.statusCode);
@@ -35,13 +35,13 @@ public static void main(String[] args) throws IOException {
3535
} catch (IOException ex) {
3636
throw ex;
3737
}
38-
request.getQueryParams().clear();
38+
request.clearQueryParams();
3939

4040
// POST
41-
request.method = Method.POST;
42-
request.endpoint = "/v3/api_keys";
43-
request.body =
44-
"{\"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+
4545
try {
4646
response = client.api(request);
4747
System.out.println(response.statusCode);
@@ -58,11 +58,11 @@ public static void main(String[] args) throws IOException {
5858
} catch (IOException ex) {
5959
throw ex;
6060
}
61-
request.body = "";
61+
request.clearBody();
6262

6363
// GET Single
64-
request.method = Method.GET;
65-
request.endpoint = "/v3/api_keys/" + apiKeyId;
64+
request.setMethod(Method.GET);
65+
request.setEndpoint("/v3/api_keys/" + apiKeyId);
6666
try {
6767
response = client.api(request);
6868
System.out.println(response.statusCode);
@@ -73,8 +73,8 @@ public static void main(String[] args) throws IOException {
7373
}
7474

7575
// PATCH
76-
request.method = Method.PATCH;
77-
request.body = "{\"name\": \"A New Hope\"}";
76+
request.setMethod(Method.PATCH);
77+
request.setBody("{\"name\": \"A New Ho}");
7878
try {
7979
response = client.api(request);
8080
System.out.println(response.statusCode);
@@ -83,12 +83,11 @@ public static void main(String[] args) throws IOException {
8383
} catch (IOException ex) {
8484
throw ex;
8585
}
86-
request.body = "";
86+
request.clearBody();
8787

8888
// PUT
89-
request.method = Method.PUT;
90-
request.body =
91-
"{\"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\"]}");
9291
try {
9392
response = client.api(request);
9493
System.out.println(response.statusCode);
@@ -97,10 +96,10 @@ public static void main(String[] args) throws IOException {
9796
} catch (IOException ex) {
9897
throw ex;
9998
}
100-
request.body = "";
99+
request.clearBody();
101100

102101
// DELETE
103-
request.method = Method.DELETE;
102+
request.setMethod(Method.DELETE);
104103
try {
105104
response = client.api(request);
106105
System.out.println(response.statusCode);

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

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

150150
try {
151-
uri = buildUri(request.baseUri, request.endpoint, request.getQueryParams());
151+
uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams());
152152
httpGet = new HttpGet(uri.toString());
153153
} catch (URISyntaxException ex) {
154154
throw ex;
@@ -171,7 +171,7 @@ public Response post(Request request) throws URISyntaxException, IOException {
171171
HttpPost httpPost = null;
172172

173173
try {
174-
uri = buildUri(request.baseUri, request.endpoint, request.getQueryParams());
174+
uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams());
175175
httpPost = new HttpPost(uri.toString());
176176
} catch (URISyntaxException ex) {
177177
throw ex;
@@ -183,8 +183,8 @@ public Response post(Request request) throws URISyntaxException, IOException {
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,7 +200,7 @@ public Response patch(Request request) throws URISyntaxException, IOException {
200200
HttpPatch httpPatch = null;
201201

202202
try {
203-
uri = buildUri(request.baseUri, request.endpoint, request.getQueryParams());
203+
uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams());
204204
httpPatch = new HttpPatch(uri.toString());
205205
} catch (URISyntaxException ex) {
206206
throw ex;
@@ -212,8 +212,8 @@ public Response patch(Request request) throws URISyntaxException, IOException {
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,7 +228,7 @@ public Response put(Request request) throws URISyntaxException, IOException {
228228
HttpPut httpPut = null;
229229

230230
try {
231-
uri = buildUri(request.baseUri, request.endpoint, request.getQueryParams());
231+
uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams());
232232
httpPut = new HttpPut(uri.toString());
233233
} catch (URISyntaxException ex) {
234234
throw ex;
@@ -240,8 +240,8 @@ public Response put(Request request) throws URISyntaxException, IOException {
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,7 +256,7 @@ public Response delete(Request request) throws URISyntaxException, IOException {
256256
HttpDeleteWithBody httpDelete = null;
257257

258258
try {
259-
uri = buildUri(request.baseUri, request.endpoint, request.getQueryParams());
259+
uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams());
260260
httpDelete = new HttpDeleteWithBody(uri.toString());
261261
} catch (URISyntaxException ex) {
262262
throw ex;
@@ -268,8 +268,8 @@ public Response delete(Request request) throws URISyntaxException, IOException {
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

@@ -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:

src/main/java/com/sendgrid/Request.java

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
* Class Response provides a standard interface to an API's HTTP request.
88
*/
99
public class Request {
10-
public Method method;
11-
public String baseUri;
12-
public String endpoint;
13-
public String body;
10+
private Method method;
11+
private String baseUri;
12+
private String endpoint;
13+
private String body;
1414
private final Map<String, String> headers;
1515
private final Map<String, String> queryParams;
1616

@@ -24,12 +24,44 @@ public Request() {
2424
* Place the object into an empty state.
2525
*/
2626
public void reset() {
27-
this.method = null;
28-
this.baseUri = "";
29-
this.endpoint = "";
30-
this.body = "";
31-
this.headers.clear();
32-
this.queryParams.clear();
27+
this.clearMethod();
28+
this.clearBaseUri();
29+
this.clearEndpoint();
30+
this.clearBody();
31+
this.clearHeaders();
32+
this.clearQueryParams();
33+
}
34+
35+
public void addQueryParam(String key, String value) {
36+
this.queryParams.put(key, value);
37+
}
38+
39+
public void addHeader(String key, String value) {
40+
this.headers.put(key, value);
41+
}
42+
43+
public String removeQueryParam(String key) {
44+
return this.queryParams.remove(key);
45+
}
46+
47+
public String removeHeader(String key) {
48+
return this.headers.remove(key);
49+
}
50+
51+
public void setMethod(Method method) {
52+
this.method = method;
53+
}
54+
55+
public void setBaseUri(String baseUri) {
56+
this.baseUri = baseUri;
57+
}
58+
59+
public void setEndpoint(String endpoint) {
60+
this.endpoint = endpoint;
61+
}
62+
63+
public void setBody(String body) {
64+
this.body = body;
3365
}
3466

3567
public Map<String, String> getHeaders() {
@@ -39,4 +71,44 @@ public Map<String, String> getHeaders() {
3971
public Map<String, String> getQueryParams() {
4072
return this.queryParams;
4173
}
74+
75+
public Method getMethod() {
76+
return this.method;
77+
}
78+
79+
public String getBaseUri() {
80+
return this.baseUri;
81+
}
82+
83+
public String getEndpoint() {
84+
return this.endpoint;
85+
}
86+
87+
public String getBody() {
88+
return this.body;
89+
}
90+
91+
public void clearMethod() {
92+
this.method = null;
93+
}
94+
95+
public void clearBaseUri() {
96+
this.baseUri = "";
97+
}
98+
99+
public void clearEndpoint() {
100+
this.endpoint = "";
101+
}
102+
103+
public void clearBody() {
104+
this.body = "";
105+
}
106+
107+
public void clearQueryParams() {
108+
this.queryParams.clear();
109+
}
110+
111+
public void clearHeaders() {
112+
this.headers.clear();
113+
}
42114
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ public void testMethod(Method method, int statusCode) {
124124
mockedHeaders = new Header[] { new BasicHeader("headerA", "valueA") };
125125
when(response.getAllHeaders()).thenReturn(mockedHeaders);
126126
when(httpClient.execute(Matchers.any(HttpGet.class))).thenReturn(response);
127-
request.method = method;
127+
request.setMethod(method);
128128
if ((method == Method.POST) || (method == Method.PATCH) || (method == Method.PUT)) {
129-
request.body = "{\"test\":\"testResult\"}";
129+
request.setBody("{\"test\":\"testResult\"}");
130130
}
131-
request.endpoint = "/test";
132-
request.getHeaders().put("Authorization", "Bearer XXXX");
131+
request.setEndpoint("/test");
132+
request.addHeader("Authorization", "Bearer XXXX");
133133
Client client = new Client(httpClient);
134134
testResponse = client.get(request);
135135
} catch (URISyntaxException | IOException ex) {

0 commit comments

Comments
 (0)