Skip to content

Commit 4176482

Browse files
Merge pull request #73 from huytranrjc/master
Fixes #71
2 parents d717011 + 2f2df21 commit 4176482

File tree

1 file changed

+52
-26
lines changed

1 file changed

+52
-26
lines changed

examples/Example.java

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,10 @@
1313
import java.util.Map;
1414

1515
public class Example {
16-
public static void main(String[] args) throws IOException {
17-
Client client = new Client();
18-
19-
Request request = new Request();
20-
request.setBaseUri("api.sendgrid.com");
21-
request.addHeader("Authorization", "Bearer " + System.getenv("SENDGRID_API_KEY"));
22-
23-
Response response = new Response();
24-
25-
// GET Collection
16+
17+
private static String apiKeyId = "";
18+
19+
private static void getCollection(Client client, Request request) throws IOException {
2620
request.setMethod(Method.GET);
2721
request.setEndpoint("/v3/api_keys");
2822
request.addQueryParam("limit", "100");
@@ -33,8 +27,9 @@ public static void main(String[] args) throws IOException {
3327
throw ex;
3428
}
3529
request.clearQueryParams();
36-
37-
// POST
30+
}
31+
32+
private static void post(Client client, Request request) throws IOException {
3833
request.setMethod(Method.POST);
3934
request.setEndpoint("/v3/api_keys");
4035
request.setBody("{\"name\": \"My api Key\",\"scopes\": [\"mail.send\",\"alerts.create\",\"alerts.read\"]}");
@@ -52,45 +47,76 @@ public static void main(String[] args) throws IOException {
5247
} catch (IOException ex) {
5348
throw ex;
5449
}
55-
request.clearBody();
56-
57-
// GET Single
50+
request.clearBody();
51+
}
52+
53+
private static void getSingle(Client client, Request request) throws IOException {
5854
request.setMethod(Method.GET);
5955
request.setEndpoint("/v3/api_keys/" + apiKeyId);
6056
try {
6157
processResponse();
6258
} catch (IOException ex) {
6359
throw ex;
64-
}
65-
66-
// PATCH
60+
}
61+
}
62+
63+
private static void patch(Client client, Request request) throws IOException {
6764
request.setMethod(Method.PATCH);
6865
request.setBody("{\"name\": \"A New Ho}");
6966
try {
7067
processResponse();
7168
} catch (IOException ex) {
7269
throw ex;
7370
}
74-
request.clearBody();
75-
76-
// PUT
71+
request.clearBody();
72+
}
73+
74+
private static void put(Client client, Request request) throws IOException {
7775
request.setMethod(Method.PUT);
7876
request.setBody("{\"name\": \"A New Hope\",\"scopes\": [\"user.profile.read\",\"user.profile.update\"]}");
7977
try {
8078
processResponse();
79+
} catch (IOException ex) {
8180
throw ex;
8281
}
83-
request.clearBody();
84-
85-
// DELETE
82+
request.clearBody();
83+
}
84+
85+
private static void delete(Client client, Request request) throws IOException {
8686
request.setMethod(Method.DELETE);
8787
try {
88-
response = client.api(request);
88+
Response response = client.api(request);
8989
System.out.println(response.getStatusCode());
9090
System.out.println(response.getHeaders());
9191
} catch (IOException ex) {
9292
throw ex;
93-
}
93+
}
94+
}
95+
96+
public static void main(String[] args) throws IOException {
97+
Client client = new Client();
98+
99+
Request request = new Request();
100+
request.setBaseUri("api.sendgrid.com");
101+
request.addHeader("Authorization", "Bearer " + System.getenv("SENDGRID_API_KEY"));
102+
103+
// GET Collection
104+
getCollection(client, request);
105+
106+
// POST
107+
post(client, request);
108+
109+
// GET Single
110+
getSingle(client, request);
111+
112+
// PATCH
113+
patch(client, request);
114+
115+
// PUT
116+
put(client, request);
117+
118+
// DELETE
119+
delete(client, request);
94120
}
95121

96122
//Refactor method

0 commit comments

Comments
 (0)