Skip to content

Commit 4a82ea0

Browse files
Merge pull request #158 from dmaicher/client-customization
[Enhancement] allow using custom Client
2 parents 9d14b95 + 09d9114 commit 4a82ea0

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,13 @@
109109
<dependency>
110110
<groupId>junit</groupId>
111111
<artifactId>junit-dep</artifactId>
112-
<version>4.10</version>
112+
<version>4.11</version>
113+
<scope>test</scope>
114+
</dependency>
115+
<dependency>
116+
<groupId>org.mockito</groupId>
117+
<artifactId>mockito-core</artifactId>
118+
<version>2.1.0</version>
113119
<scope>test</scope>
114120
</dependency>
115121
</dependencies>

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package com.sendgrid;
22

3-
import com.sendgrid.Client;
4-
import com.sendgrid.Method;
5-
import com.sendgrid.Request;
6-
import com.sendgrid.Response;
7-
83
import java.io.IOException;
94
import java.util.HashMap;
105
import java.util.Map;
@@ -39,6 +34,15 @@ public SendGrid(String apiKey, Boolean test) {
3934
initializeSendGrid(apiKey);
4035
}
4136

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+
4246
public void initializeSendGrid(String apiKey) {
4347
this.apiKey = apiKey;
4448
this.host = "api.sendgrid.com";

src/test/java/com/sendgrid/SendGridTest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package com.sendgrid;
22

33
import org.junit.Assert;
4-
import org.junit.Before;
54
import org.junit.Test;
65

7-
import java.io.BufferedReader;
8-
import java.io.FileNotFoundException;
9-
import java.io.FileReader;
106
import java.io.IOException;
117
import java.util.HashMap;
128
import java.util.Map;
9+
import static org.mockito.Mockito.*;
1310

1411
public class SendGridTest {
1512

@@ -34,6 +31,15 @@ public void testInitialization() {
3431
Assert.assertEquals(sg.getRequestHeaders(), requestHeaders);
3532
}
3633

34+
@Test
35+
public void testConstructWithClient() throws IOException {
36+
Client client = mock(Client.class);
37+
SendGrid sg = new SendGrid(SENDGRID_API_KEY, client);
38+
Request request = new Request();
39+
sg.makeCall(request);
40+
verify(client).api(request);
41+
}
42+
3743
@Test
3844
public void testLibraryVersion() {
3945
SendGrid sg = new SendGrid(SENDGRID_API_KEY);

0 commit comments

Comments
 (0)