Skip to content

Commit

Permalink
Added Custom Header Sender so users can send any special headers they…
Browse files Browse the repository at this point in the history
… need to.
  • Loading branch information
MouaYing committed Apr 5, 2018
1 parent 0d77e50 commit 75ff887
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/make -f

SOURCE_VERSION := 3.2
SOURCE_VERSION := 3.3

clean:
mvn clean
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/smartystreets/api/ClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.Map;

/**
* The ClientBuilder class helps you build a client object for one of the supported SmartyStreets APIs.<br>
Expand All @@ -16,6 +17,7 @@ public class ClientBuilder {
private int maxTimeout;
private String urlPrefix;
private Proxy proxy;
private Map<String, String> customHeaders;
private final String INTERNATIONAL_STREET_API_URL = "https://international-street.api.smartystreets.com/verify";
private final String US_AUTOCOMPLETE_API_URL = "https://us-autocomplete.api.smartystreets.com/suggest";
private final String US_EXTRACT_API_URL = "https://us-extract.api.smartystreets.com/";
Expand Down Expand Up @@ -85,6 +87,16 @@ public ClientBuilder withCustomBaseUrl(String baseUrl) {
return this;
}

/**
* Use this to add any additional headers you need.
* @param customHeaders A string to string <b>Map</b> of header name/value pairs.
* @return Returns <b>this</b> to accommodate method chaining.
*/
public ClientBuilder withCustomHeaders(Map<String, String> customHeaders) {
this.customHeaders = customHeaders;
return this;
}

/**
* Use this to specify a proxy through which to send all lookups.
* @param proxyType Choose a java.net.Proxy.Type.
Expand Down Expand Up @@ -139,6 +151,9 @@ private Sender buildSender() {

sender = new StatusCodeSender(sender);

if (this.customHeaders != null)
sender = new CustomHeaderSender(this.customHeaders, sender);

if (this.signer != null)
sender = new SigningSender(this.signer, sender);

Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/smartystreets/api/CustomHeaderSender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.smartystreets.api;

import com.smartystreets.api.exceptions.SmartyException;

import java.io.IOException;
import java.util.Map;

public class CustomHeaderSender implements Sender {
private Map<String, String> headers;
private Sender inner;

public CustomHeaderSender(Map<String, String> headers, Sender inner){
this.headers = headers;
this.inner = inner;
}

@Override
public Response send(Request request) throws SmartyException, IOException {
Map<String, String> requestHeaders = request.getHeaders();

for (Map.Entry entry : this.headers.entrySet()) {
request.putHeader((String)entry.getKey(), (String)entry.getValue());
}

return this.inner.send(request);
}
}
31 changes: 31 additions & 0 deletions src/test/java/com/smartystreets/api/CustomHeaderSenderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.smartystreets.api;

import com.smartystreets.api.exceptions.SmartyException;
import com.smartystreets.api.mocks.RequestCapturingSender;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.*;

public class CustomHeaderSenderTest {

@Test
public void testAllCustomHeadersAreAddedToTheRequest() throws IOException, SmartyException {
HashMap<String, String> headers = new HashMap<>();
headers.put("A", "1");
headers.put("B", "2");
RequestCapturingSender inner = new RequestCapturingSender();
CustomHeaderSender sender = new CustomHeaderSender(headers, inner);
Request request = new Request();

sender.send(request);

Map<String, String> requestHeaders = inner.getRequest().getHeaders();
assertNotNull("There should be headers here.", requestHeaders);
assertEquals(headers.get("A"), inner.getRequest().getHeaders().get("A"));

}
}

0 comments on commit 75ff887

Please sign in to comment.