Skip to content

Commit 5028af8

Browse files
Refactoring, correct package and fluent interface
1 parent 168bef6 commit 5028af8

20 files changed

+4658
-5942
lines changed

src/main/java/com/sendgrid/SendGrid.java

Lines changed: 102 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -5,113 +5,107 @@
55
import java.util.Map;
66

77
/**
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+
*/
1010
public class SendGrid {
11-
private static final String VERSION = "3.0.0";
12-
private static final String USER_AGENT = "sendgrid/" + VERSION + ";java";
13-
14-
private String apiKey;
15-
private String host;
16-
private String version;
17-
private Client client;
18-
private Map<String,String> requestHeaders;
19-
20-
/**
21-
* @param apiKey is your SendGrid API Key: https://app.sendgrid.com/settings/api_keys
22-
*/
23-
public SendGrid(String apiKey) {
24-
this.client = new Client();
25-
initializeSendGrid(apiKey);
26-
}
27-
28-
/**
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-
*/
32-
public SendGrid(String apiKey, Boolean test) {
33-
this.client = new Client(test);
34-
initializeSendGrid(apiKey);
35-
}
36-
37-
/**
38-
* @param apiKey is your SendGrid API Key: https://app.sendgrid.com/settings/api_keys
39-
* @param client the Client to use (allows to customize its configuration)
40-
*/
41-
public SendGrid(String apiKey, Client client) {
42-
this.client = client;
43-
initializeSendGrid(apiKey);
44-
}
45-
46-
public void initializeSendGrid(String apiKey) {
47-
this.apiKey = apiKey;
48-
this.host = "api.sendgrid.com";
49-
this.version = "v3";
50-
this.requestHeaders = new HashMap<String, String>();
51-
this.requestHeaders.put("Authorization", "Bearer " + apiKey);
52-
this.requestHeaders.put("User-agent", USER_AGENT);
53-
this.requestHeaders.put("Accept", "application/json");
54-
}
55-
56-
public String getLibraryVersion() {
57-
return this.VERSION;
58-
}
59-
60-
public String getVersion() {
61-
return this.version;
62-
}
63-
64-
public void setVersion(String version) {
65-
this.version = version;
66-
}
67-
68-
public Map<String,String> getRequestHeaders() {
69-
return this.requestHeaders;
70-
}
71-
72-
public Map<String,String> addRequestHeader(String key, String value) {
73-
this.requestHeaders.put(key, value);
74-
return getRequestHeaders();
75-
}
76-
77-
public Map<String,String> removeRequestHeader(String key) {
78-
this.requestHeaders.remove(key);
79-
return getRequestHeaders();
80-
}
81-
82-
public String getHost() {
83-
return this.host;
84-
}
85-
86-
public void setHost(String host) {
87-
this.host = host;
88-
}
89-
90-
/**
91-
* Class makeCall makes the call to the SendGrid API, override this method for testing.
92-
*/
93-
public Response makeCall(Request request) throws IOException {
94-
Response response = new Response();
95-
try {
96-
response = client.api(request);
97-
} catch (IOException ex) {
98-
throw ex;
99-
}
100-
return response;
101-
}
102-
103-
/**
104-
* Class api sets up the request to the SendGrid API, this is main interface.
105-
*/
106-
public Response api(Request request) throws IOException {
107-
Request req = new Request();
108-
req.method = request.method;
109-
req.baseUri = this.host;
110-
req.endpoint = "/" + version + "/" + request.endpoint;
111-
req.body = request.body;
112-
req.headers = this.requestHeaders;
113-
req.queryParams = request.queryParams;
114-
115-
return makeCall(req);
116-
}
11+
private static final String VERSION = "3.0.0";
12+
private static final String USER_AGENT = "sendgrid/" + VERSION + ";java";
13+
14+
private String apiKey;
15+
private String host;
16+
private String version;
17+
private final Client client;
18+
private Map<String, String> requestHeaders;
19+
20+
/**
21+
* @param apiKey is your SendGrid API Key: https://app.sendgrid.com/settings/api_keys
22+
*/
23+
public SendGrid(final String apiKey) {
24+
this.client = new Client();
25+
initializeSendGrid(apiKey);
26+
}
27+
28+
/**
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+
*/
32+
public SendGrid(final String apiKey, final Boolean test) {
33+
this.client = new Client(test);
34+
initializeSendGrid(apiKey);
35+
}
36+
37+
/**
38+
* @param apiKey is your SendGrid API Key: https://app.sendgrid.com/settings/api_keys
39+
* @param client the Client to use (allows to customize its configuration)
40+
*/
41+
public SendGrid(final String apiKey, final Client client) {
42+
this.client = client;
43+
initializeSendGrid(apiKey);
44+
}
45+
46+
private void initializeSendGrid(final String apiKey) {
47+
this.apiKey = apiKey;
48+
this.host = "api.sendgrid.com";
49+
this.version = "v3";
50+
this.requestHeaders = new HashMap<>();
51+
this.requestHeaders.put("Authorization", "Bearer " + apiKey);
52+
this.requestHeaders.put("User-agent", USER_AGENT);
53+
this.requestHeaders.put("Accept", "application/json");
54+
}
55+
56+
public String getLibraryVersion() {
57+
return VERSION;
58+
}
59+
60+
public String getVersion() {
61+
return this.version;
62+
}
63+
64+
public void setVersion(final String version) {
65+
this.version = version;
66+
}
67+
68+
public Map<String, String> getRequestHeaders() {
69+
return this.requestHeaders;
70+
}
71+
72+
public Map<String, String> addRequestHeader(final String key, final String value) {
73+
this.requestHeaders.put(key, value);
74+
return getRequestHeaders();
75+
}
76+
77+
public Map<String, String> removeRequestHeader(final String key) {
78+
this.requestHeaders.remove(key);
79+
return getRequestHeaders();
80+
}
81+
82+
public String getHost() {
83+
return this.host;
84+
}
85+
86+
public void setHost(final String host) {
87+
this.host = host;
88+
}
89+
90+
/**
91+
* Class makeCall makes the call to the SendGrid API, override this method for testing.
92+
*/
93+
public Response makeCall(final Request request) throws IOException {
94+
return this.client.api(request);
95+
}
96+
97+
/**
98+
* Class api sets up the request to the SendGrid API, this is main interface.
99+
*/
100+
public Response api(final Request request) throws IOException {
101+
final Request req = new Request();
102+
req.method = request.method;
103+
req.baseUri = this.host;
104+
req.endpoint = "/" + this.version + "/" + request.endpoint;
105+
req.body = request.body;
106+
req.headers = this.requestHeaders;
107+
req.queryParams = request.queryParams;
108+
109+
return makeCall(req);
110+
}
117111
}

0 commit comments

Comments
 (0)