forked from sendgrid/java-http-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.java
More file actions
111 lines (99 loc) · 3.22 KB
/
Example.java
File metadata and controls
111 lines (99 loc) · 3.22 KB
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
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 {
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"));
Response response = new Response();
// GET Collection
request.setMethod(Method.GET);
request.setEndpoint("/v3/api_keys");
request.addQueryParam("limit", "100");
request.addQueryParam("offset", "0");
try {
response = client.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
request.clearQueryParams();
// POST
request.setMethod(Method.POST);
request.setEndpoint("/v3/api_keys");
request.setBody("{\"name\": \"My api Key\",\"scopes\": [\"mail.send\",\"alerts.create\",\"alerts.read\"]}");
try {
response = client.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} 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();
// GET Single
request.setMethod(Method.GET);
request.setEndpoint("/v3/api_keys/" + apiKeyId);
try {
response = client.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
// PATCH
request.setMethod(Method.PATCH);
request.setBody("{\"name\": \"A New Ho}");
try {
response = client.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
request.clearBody();
// PUT
request.setMethod(Method.PUT);
request.setBody("{\"name\": \"A New Hope\",\"scopes\": [\"user.profile.read\",\"user.profile.update\"]}");
try {
response = client.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
request.clearBody();
// DELETE
request.setMethod(Method.DELETE);
try {
response = client.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
}
}