-
Notifications
You must be signed in to change notification settings - Fork 78
/
Example.java
129 lines (109 loc) · 3.4 KB
/
Example.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sendgrid.Client;
import com.sendgrid.Method;
import com.sendgrid.Request;
import com.sendgrid.Response;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
public class Example {
private static String apiKeyId = "";
private static void getCollection(Client client, Request request) throws IOException {
request.setMethod(Method.GET);
request.setEndpoint("/v3/api_keys");
request.addQueryParam("limit", "100");
request.addQueryParam("offset", "0");
try {
processResponse();
} catch (IOException ex) {
throw ex;
}
request.clearQueryParams();
}
private static void post(Client client, Request request) throws IOException {
request.setMethod(Method.POST);
request.setEndpoint("/v3/api_keys");
request.setBody("{\"name\": \"My api Key\",\"scopes\": [\"mail.send\",\"alerts.create\",\"alerts.read\"]}");
try {
processResponse();
} catch (IOException ex) {
throw ex;
}
String apiKeyId = "";
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode json = mapper.readTree(response.getBody());
apiKeyId = json.path("api_key_id").asText();
} catch (IOException ex) {
throw ex;
}
request.clearBody();
}
private static void getSingle(Client client, Request request) throws IOException {
request.setMethod(Method.GET);
request.setEndpoint("/v3/api_keys/" + apiKeyId);
try {
processResponse();
} catch (IOException ex) {
throw ex;
}
}
private static void patch(Client client, Request request) throws IOException {
request.setMethod(Method.PATCH);
request.setBody("{\"name\": \"A New Ho}");
try {
processResponse();
} catch (IOException ex) {
throw ex;
}
request.clearBody();
}
private static void put(Client client, Request request) throws IOException {
request.setMethod(Method.PUT);
request.setBody("{\"name\": \"A New Hope\",\"scopes\": [\"user.profile.read\",\"user.profile.update\"]}");
try {
processResponse();
} catch (IOException ex) {
throw ex;
}
request.clearBody();
}
private static void delete(Client client, Request request) throws IOException {
request.setMethod(Method.DELETE);
try {
Response response = client.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
}
public static void main(String[] args) throws IOException {
Client client = new Client();
Request request = new Request();
request.setBaseUri("api.sendgrid.com");
request.addHeader("Authorization", "Bearer " + System.getenv("SENDGRID_API_KEY"));
// GET Collection
getCollection(client, request);
// POST
post(client, request);
// GET Single
getSingle(client, request);
// PATCH
patch(client, request);
// PUT
put(client, request);
// DELETE
delete(client, request);
}
//Refactor method
private void processResponse(){
response = client.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
}
}