13
13
import java .util .Map ;
14
14
15
15
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 {
26
20
request .setMethod (Method .GET );
27
21
request .setEndpoint ("/v3/api_keys" );
28
22
request .addQueryParam ("limit" , "100" );
@@ -33,8 +27,9 @@ public static void main(String[] args) throws IOException {
33
27
throw ex ;
34
28
}
35
29
request .clearQueryParams ();
36
-
37
- // POST
30
+ }
31
+
32
+ private static void post (Client client , Request request ) throws IOException {
38
33
request .setMethod (Method .POST );
39
34
request .setEndpoint ("/v3/api_keys" );
40
35
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 {
52
47
} catch (IOException ex ) {
53
48
throw ex ;
54
49
}
55
- request .clearBody ();
56
-
57
- // GET Single
50
+ request .clearBody ();
51
+ }
52
+
53
+ private static void getSingle (Client client , Request request ) throws IOException {
58
54
request .setMethod (Method .GET );
59
55
request .setEndpoint ("/v3/api_keys/" + apiKeyId );
60
56
try {
61
57
processResponse ();
62
58
} catch (IOException ex ) {
63
59
throw ex ;
64
- }
65
-
66
- // PATCH
60
+ }
61
+ }
62
+
63
+ private static void patch (Client client , Request request ) throws IOException {
67
64
request .setMethod (Method .PATCH );
68
65
request .setBody ("{\" name\" : \" A New Ho}" );
69
66
try {
70
67
processResponse ();
71
68
} catch (IOException ex ) {
72
69
throw ex ;
73
70
}
74
- request .clearBody ();
75
-
76
- // PUT
71
+ request .clearBody ();
72
+ }
73
+
74
+ private static void put (Client client , Request request ) throws IOException {
77
75
request .setMethod (Method .PUT );
78
76
request .setBody ("{\" name\" : \" A New Hope\" ,\" scopes\" : [\" user.profile.read\" ,\" user.profile.update\" ]}" );
79
77
try {
80
78
processResponse ();
79
+ } catch (IOException ex ) {
81
80
throw ex ;
82
81
}
83
- request .clearBody ();
84
-
85
- // DELETE
82
+ request .clearBody ();
83
+ }
84
+
85
+ private static void delete (Client client , Request request ) throws IOException {
86
86
request .setMethod (Method .DELETE );
87
87
try {
88
- response = client .api (request );
88
+ Response response = client .api (request );
89
89
System .out .println (response .getStatusCode ());
90
90
System .out .println (response .getHeaders ());
91
91
} catch (IOException ex ) {
92
92
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 );
94
120
}
95
121
96
122
//Refactor method
0 commit comments