forked from sendgrid/sendgrid-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendGrid.java
More file actions
113 lines (95 loc) · 2.87 KB
/
SendGrid.java
File metadata and controls
113 lines (95 loc) · 2.87 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
package com.sendgrid;
import com.sendgrid.Client;
import com.sendgrid.Method;
import com.sendgrid.Request;
import com.sendgrid.Response;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* Class SendGrid allows for quick and easy access to the SendGrid API.
*/
public class SendGrid {
private static final String VERSION = "3.0.0";
private static final String USER_AGENT = "sendgrid/" + VERSION + ";java";
private String apiKey;
private String host;
private String version;
private Client client;
private Map<String,String> requestHeaders;
/**
* @param apiKey is your SendGrid API Key: https://app.sendgrid.com/settings/api_keys
*/
public SendGrid(String apiKey) {
this.client = new Client();
initializeSendGrid(apiKey);
}
/**
* @param apiKey is your SendGrid API Key: https://app.sendgrid.com/settings/api_keys
* @param test is true if you are unit testing
*/
public SendGrid(String apiKey, Boolean test) {
this.client = new Client(test);
initializeSendGrid(apiKey);
}
public void initializeSendGrid(String apiKey) {
this.apiKey = apiKey;
this.host = "api.sendgrid.com";
this.version = "v3";
this.requestHeaders = new HashMap<String, String>();
this.requestHeaders.put("Authorization", "Bearer " + apiKey);
this.requestHeaders.put("User-agent", USER_AGENT);
this.requestHeaders.put("Accept", "application/json");
}
public String getLibraryVersion() {
return this.VERSION;
}
public String getVersion() {
return this.version;
}
public void setVersion(String version) {
this.version = version;
}
public Map<String,String> getRequestHeaders() {
return this.requestHeaders;
}
public Map<String,String> addRequestHeader(String key, String value) {
this.requestHeaders.put(key, value);
return getRequestHeaders();
}
public Map<String,String> removeRequestHeader(String key) {
this.requestHeaders.remove(key);
return getRequestHeaders();
}
public String getHost() {
return this.host;
}
public void setHost(String host) {
this.host = host;
}
/**
* Class makeCall makes the call to the SendGrid API, override this method for testing.
*/
public Response makeCall(Request request) throws IOException {
Response response = new Response();
try {
response = client.api(request);
} catch (IOException ex) {
throw ex;
}
return response;
}
/**
* Class api sets up the request to the SendGrid API, this is main interface.
*/
public Response api(Request request) throws IOException {
Request req = new Request();
req.method = request.method;
req.baseUri = this.host;
req.endpoint = "/" + version + "/" + request.endpoint;
req.body = request.body;
req.headers = this.requestHeaders;
req.queryParams = request.queryParams;
return makeCall(req);
}
}