Skip to content

Commit

Permalink
Header values can now be any object, not just Strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
MouaYing committed Apr 11, 2018
1 parent 75ff887 commit 7681b0a
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/smartystreets/api/ClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ClientBuilder {
private int maxTimeout;
private String urlPrefix;
private Proxy proxy;
private Map<String, String> customHeaders;
private Map<String, Object> 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 @@ -89,10 +89,10 @@ public ClientBuilder withCustomBaseUrl(String baseUrl) {

/**
* Use this to add any additional headers you need.
* @param customHeaders A string to string <b>Map</b> of header name/value pairs.
* @param customHeaders A String to Object <b>Map</b> of header name/value pairs.
* @return Returns <b>this</b> to accommodate method chaining.
*/
public ClientBuilder withCustomHeaders(Map<String, String> customHeaders) {
public ClientBuilder withCustomHeaders(Map<String, Object> customHeaders) {
this.customHeaders = customHeaders;
return this;
}
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/com/smartystreets/api/CustomHeaderSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@
import java.util.Map;

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

public CustomHeaderSender(Map<String, String> headers, Sender inner){
public CustomHeaderSender(Map<String, Object> 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());
request.putHeader((String)entry.getKey(), entry.getValue());
}

return this.inner.send(request);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/smartystreets/api/GoogleSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private HttpRequest buildHttpRequest(Request smartyRequest) throws IOException {
private void copyHeaders(Request smartyRequest, HttpRequest httpRequest) {
HttpHeaders httpHeaders = httpRequest.getHeaders();

Map<String, String> headers = smartyRequest.getHeaders();
Map<String, Object> headers = smartyRequest.getHeaders();
for (String headerName : headers.keySet())
httpHeaders.set(headerName, headers.get(headerName));

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/smartystreets/api/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class Request {
private static final String CHARSET = "UTF-8";
private final Map<String, String> headers;
private final Map<String, Object> headers;
private final Map<String, String> parameters;
private String urlPrefix;
private String method;
Expand All @@ -22,7 +22,7 @@ public Request() {
this.contentType = "application/json";
}

void putHeader(String name, String value) {
void putHeader(String name, Object value) {
this.headers.put(name, value);
}

Expand Down Expand Up @@ -65,7 +65,7 @@ public String getMethod() {
return method;
}

public Map<String, String> getHeaders() {
public Map<String, Object> getHeaders() {
return this.headers;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class CustomHeaderSenderTest {

@Test
public void testAllCustomHeadersAreAddedToTheRequest() throws IOException, SmartyException {
HashMap<String, String> headers = new HashMap<>();
HashMap<String, Object> headers = new HashMap<>();
headers.put("A", "1");
headers.put("B", "2");
RequestCapturingSender inner = new RequestCapturingSender();
Expand All @@ -23,7 +23,7 @@ public void testAllCustomHeadersAreAddedToTheRequest() throws IOException, Smart

sender.send(request);

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

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/smartystreets/api/GoogleSenderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void testHttpRequestContainsCorrectHeaders() throws Exception {

sender.send(request);

Map<String, String> headers = request.getHeaders();
Map<String, Object> headers = request.getHeaders();
assertEquals("value1", headers.get("X-name1"));
assertEquals("value2", headers.get("X-name2"));
}
Expand Down

0 comments on commit 7681b0a

Please sign in to comment.