5
5
import java .util .Map ;
6
6
7
7
/**
8
- * Class SendGrid allows for quick and easy access to the SendGrid API.
9
- */
8
+ * Class SendGrid allows for quick and easy access to the SendGrid API.
9
+ */
10
10
public class SendGrid {
11
+ /** The current library version. */
11
12
private static final String VERSION = "3.0.0" ;
13
+
14
+ /** The user agent string to return to SendGrid. */
12
15
private static final String USER_AGENT = "sendgrid/" + VERSION + ";java" ;
13
16
17
+ /** The user's API key. */
14
18
private String apiKey ;
19
+
20
+ /** The SendGrid host to which to connect. */
15
21
private String host ;
22
+
23
+ /** The API version. */
16
24
private String version ;
25
+
26
+ /** The HTTP client. */
17
27
private Client client ;
28
+
29
+ /** The request headers container. */
18
30
private Map <String ,String > requestHeaders ;
19
31
20
32
/**
21
- * @param apiKey is your SendGrid API Key: https://app.sendgrid.com/settings/api_keys
22
- */
33
+ * Construct a new SendGrid API wrapper.
34
+ * @param apiKey is your SendGrid API Key: https://app.sendgrid.com/settings/api_keys
35
+ * @return a SendGrid object.
36
+ */
23
37
public SendGrid (String apiKey ) {
24
38
this .client = new Client ();
25
39
initializeSendGrid (apiKey );
26
40
}
27
41
28
42
/**
29
- * @param apiKey is your SendGrid API Key: https://app.sendgrid.com/settings/api_keys
30
- * @param test is true if you are unit testing
31
- */
43
+ * Construct a new SendGrid API wrapper.
44
+ * @param apiKey is your SendGrid API Key: https://app.sendgrid.com/settings/api_keys
45
+ * @param test is true if you are unit testing
46
+ * @return a SendGrid object.
47
+ */
32
48
public SendGrid (String apiKey , Boolean test ) {
33
49
this .client = new Client (test );
34
50
initializeSendGrid (apiKey );
35
51
}
36
52
37
53
/**
54
+ * Construct a new SendGrid API wrapper.
38
55
* @param apiKey is your SendGrid API Key: https://app.sendgrid.com/settings/api_keys
39
56
* @param client the Client to use (allows to customize its configuration)
57
+ * @return a SendGrid object.
40
58
*/
41
59
public SendGrid (String apiKey , Client client ) {
42
60
this .client = client ;
43
61
initializeSendGrid (apiKey );
44
62
}
45
63
64
+ /**
65
+ * Initialize the client.
66
+ * @param apiKey the user's API key.
67
+ */
46
68
public void initializeSendGrid (String apiKey ) {
47
69
this .apiKey = apiKey ;
48
70
this .host = "api.sendgrid.com" ;
@@ -53,50 +75,91 @@ public void initializeSendGrid(String apiKey) {
53
75
this .requestHeaders .put ("Accept" , "application/json" );
54
76
}
55
77
78
+ /**
79
+ * Retrieve the current library version.
80
+ * @return the current version.
81
+ */
56
82
public String getLibraryVersion () {
57
83
return this .VERSION ;
58
84
}
59
85
86
+ /**
87
+ * Get the API version.
88
+ * @return the current API versioin (v3 by default).
89
+ */
60
90
public String getVersion () {
61
91
return this .version ;
62
92
}
63
93
94
+ /**
95
+ * Set the API version.
96
+ * @param version the new version.
97
+ */
64
98
public void setVersion (String version ) {
65
99
this .version = version ;
66
100
}
67
101
102
+ /**
103
+ * Obtain the request headers.
104
+ * @return the request headers.
105
+ */
68
106
public Map <String ,String > getRequestHeaders () {
69
107
return this .requestHeaders ;
70
108
}
71
109
110
+ /**
111
+ * Add a new request header.
112
+ * @param key the header key.
113
+ * @param value the header value.
114
+ * @return the new set of request headers.
115
+ */
72
116
public Map <String ,String > addRequestHeader (String key , String value ) {
73
117
this .requestHeaders .put (key , value );
74
118
return getRequestHeaders ();
75
119
}
76
120
121
+ /**
122
+ * Remove a request header.
123
+ * @param key the header key to remove.
124
+ * @return the new set of request headers.
125
+ */
77
126
public Map <String ,String > removeRequestHeader (String key ) {
78
127
this .requestHeaders .remove (key );
79
128
return getRequestHeaders ();
80
129
}
81
130
131
+ /**
132
+ * Get the SendGrid host (api.sendgrid.com by default).
133
+ * @return the SendGrid host.
134
+ */
82
135
public String getHost () {
83
136
return this .host ;
84
137
}
85
138
139
+ /**
140
+ * Set the SendGrid host.
141
+ * @host the new SendGrid host.
142
+ */
86
143
public void setHost (String host ) {
87
144
this .host = host ;
88
145
}
89
146
90
147
/**
91
- * Class makeCall makes the call to the SendGrid API, override this method for testing.
92
- */
148
+ * Class makeCall makes the call to the SendGrid API, override this method for testing.
149
+ * @param request the request to make.
150
+ * @return the response object.
151
+ * @throws IOException in case of a network error.
152
+ */
93
153
public Response makeCall (Request request ) throws IOException {
94
154
return client .api (request );
95
155
}
96
156
97
157
/**
98
- * Class api sets up the request to the SendGrid API, this is main interface.
99
- */
158
+ * Class api sets up the request to the SendGrid API, this is main interface.
159
+ * @param request the request object.
160
+ * @return the response object.
161
+ * @throws IOException in case of a network error.
162
+ */
100
163
public Response api (Request request ) throws IOException {
101
164
Request req = new Request ();
102
165
req .setMethod (request .getMethod ());
0 commit comments