Repository: sendgrid/sendgrid-java
Pull request #158 (2016-10-09 12:29:07)

[Enhancement] allow using custom Client
Issues: https://github.com/sendgrid/sendgrid-java/issues/138 & https://github.com/sendgrid/java-http-client/issues/8

Will allow to pass in a custom `Client` (that itself might have a custom `CloseableHttpClient` to use proxies for example).

**I can provide a signed CLA once I know this PR makes sense as it is** :wink: 

dmaicher: Something seems to be wrong in general with the build? Well the test I added should be passing :wink: 
dmaicher: And btw I think this PR should be favoured instead of https://github.com/sendgrid/sendgrid-java/pull/119 as the SendGrid class should have no knowledge about the http client implementation which is an internal detail of the `Client` abstraction.
thinkingserious: Thanks for the PR @dmaicher,

It looks good to me. I'll be looking forward to your signed CLA :)

To test locally, please follow steps 3-5 here: https://github.com/sendgrid/sendgrid-ruby/blob/master/CONTRIBUTING.md#initial-setup

With Best Regards,

Elmer
dmaicher: @thinkingserious I emailed the CLA :wink: 

Patch:
diff --git a/pom.xml b/pom.xml
index 10d4ba6..b9e4ab0 100644
--- a/pom.xml
+++ b/pom.xml
@@ -109,7 +109,13 @@
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit-dep</artifactId>
-            <version>4.10</version>
+            <version>4.11</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <version>2.1.0</version>
             <scope>test</scope>
         </dependency>
     </dependencies>
diff --git a/src/main/java/com/sendgrid/SendGrid.java b/src/main/java/com/sendgrid/SendGrid.java
index 4f0ded9..b8af3a3 100644
--- a/src/main/java/com/sendgrid/SendGrid.java
+++ b/src/main/java/com/sendgrid/SendGrid.java
@@ -1,10 +1,5 @@
 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;
@@ -39,6 +34,15 @@ public class SendGrid {
     initializeSendGrid(apiKey);
   }
 
+  /**
+   * @param apiKey is your SendGrid API Key: https://app.sendgrid.com/settings/api_keys
+   * @param client the Client to use (allows to customize its configuration)
+   */
+  public SendGrid(String apiKey, Client client) {
+    this.client = client;
+    initializeSendGrid(apiKey);
+  }
+
   public void initializeSendGrid(String apiKey) {
     this.apiKey = apiKey;
     this.host = "api.sendgrid.com";


Test patch:
diff --git a/src/test/java/com/sendgrid/SendGridTest.java b/src/test/java/com/sendgrid/SendGridTest.java
index a7856fd..dca0222 100644
--- a/src/test/java/com/sendgrid/SendGridTest.java
+++ b/src/test/java/com/sendgrid/SendGridTest.java
@@ -1,15 +1,12 @@
 package com.sendgrid;
 
 import org.junit.Assert;
-import org.junit.Before;
 import org.junit.Test;
 
-import java.io.BufferedReader;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
+import static org.mockito.Mockito.*;
 
 public class SendGridTest {
 
@@ -34,6 +31,15 @@ public class SendGridTest {
     Assert.assertEquals(sg.getRequestHeaders(), requestHeaders);
   }
 
+  @Test
+  public void testConstructWithClient() throws IOException {
+    Client client = mock(Client.class);
+    SendGrid sg = new SendGrid(SENDGRID_API_KEY, client);
+    Request request = new Request();
+    sg.makeCall(request);
+    verify(client).api(request);
+  }
+
   @Test
   public void testLibraryVersion() {
     SendGrid sg = new SendGrid(SENDGRID_API_KEY);