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
116 lines (105 loc) · 3.32 KB
/
Copy pathExample.java
File metadata and controls
116 lines (105 loc) · 3.32 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
112
113
114
115
116
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.baseUri = "api.sendgrid.com";
Map<String,String> requestHeaders = new HashMap<String, String>();
requestHeaders.put("Authorization", "Bearer " + System.getenv("SENDGRID_API_KEY"));
request.headers = requestHeaders;
Response response = new Response();
// GET Collection
request.method = Method.GET;
request.endpoint = "/v3/api_keys";
Map<String,String> queryParams = new HashMap<String, String>();
queryParams.put("limit", "100");
queryParams.put("offset", "0");
request.queryParams = queryParams;
try {
response = client.api(request);
System.out.println(response.statusCode);
System.out.println(response.body);
System.out.println(response.headers);
} catch (IOException ex) {
throw ex;
}
request.queryParams = null;
// POST
request.method = Method.POST;
request.endpoint = "/v3/api_keys";
request.body =
"{\"name\": \"My api Key\",\"scopes\": [\"mail.send\",\"alerts.create\",\"alerts.read\"]}";
try {
response = client.api(request);
System.out.println(response.statusCode);
System.out.println(response.body);
System.out.println(response.headers);
} catch (IOException ex) {
throw ex;
}
String apiKeyId = "";
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode json = mapper.readTree(response.body);
apiKeyId = json.path("api_key_id").asText();
} catch (IOException ex) {
throw ex;
}
request.body = "";
// GET Single
request.method = Method.GET;
request.endpoint = "/v3/api_keys/" + apiKeyId;
try {
response = client.api(request);
System.out.println(response.statusCode);
System.out.println(response.body);
System.out.println(response.headers);
} catch (IOException ex) {
throw ex;
}
// PATCH
request.method = Method.PATCH;
request.body = "{\"name\": \"A New Hope\"}";
try {
response = client.api(request);
System.out.println(response.statusCode);
System.out.println(response.body);
System.out.println(response.headers);
} catch (IOException ex) {
throw ex;
}
request.body = "";
// PUT
request.method = Method.PUT;
request.body =
"{\"name\": \"A New Hope\",\"scopes\": [\"user.profile.read\",\"user.profile.update\"]}";
try {
response = client.api(request);
System.out.println(response.statusCode);
System.out.println(response.body);
System.out.println(response.headers);
} catch (IOException ex) {
throw ex;
}
request.body = "";
// DELETE
request.method = Method.DELETE;
try {
response = client.api(request);
System.out.println(response.statusCode);
System.out.println(response.headers);
} catch (IOException ex) {
throw ex;
}
}
}