Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ts-java-sdk into eric/compatibility-parameter
  • Loading branch information
Eric Devenport authored and Eric Devenport committed Jun 14, 2024
2 parents 9904f63 + 6371b51 commit bae215c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 18 deletions.
56 changes: 40 additions & 16 deletions src/main/java/com/smartystreets/api/ClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import java.util.Map;

/**
* The ClientBuilder class helps you build a client object for one of the supported SmartyStreets APIs.<br>
* You can use ClientBuilder's methods to customize settings like maximum retries or timeout duration. These methods<br>
* The ClientBuilder class helps you build a client object for one of the
* supported SmartyStreets APIs.<br>
* You can use ClientBuilder's methods to customize settings like maximum
* retries or timeout duration. These methods<br>
* are chainable, so you can usually get set up with one line of code.
*/
public class ClientBuilder {
Expand All @@ -30,6 +32,7 @@ public class ClientBuilder {
private Proxy proxy;
private Map<String, Object> customHeaders;
private List<String> licenses = new ArrayList<>();
private String ip;

private ClientBuilder() {
this.serializer = new SmartySerializer();
Expand All @@ -47,7 +50,8 @@ public ClientBuilder(String authId, String authToken) {
}

/**
* @param maxRetries The maximum number of times to retry sending the request to the API. (Default is 5)
* @param maxRetries The maximum number of times to retry sending the request to
* the API. (Default is 5)
* @return Returns <b>this</b> to accommodate method chaining.
*/
public ClientBuilder retryAtMost(int maxRetries) {
Expand All @@ -56,7 +60,8 @@ public ClientBuilder retryAtMost(int maxRetries) {
}

/**
* @param maxTimeout The maximum time (in milliseconds) to wait for a connection, and also to wait for <br>
* @param maxTimeout The maximum time (in milliseconds) to wait for a
* connection, and also to wait for <br>
* the response to be read. (Default is 10000)
* @return Returns <b>this</b> to accommodate method chaining.
*/
Expand All @@ -66,7 +71,8 @@ public ClientBuilder withMaxTimeout(int maxTimeout) {
}

/**
* @param sender Default is a series of nested senders. See <b>buildSender()</b>.
* @param sender Default is a series of nested senders. See
* <b>buildSender()</b>.
* @return Returns <b>this</b> to accommodate method chaining.
*/
public ClientBuilder withSender(Sender sender) {
Expand All @@ -76,6 +82,7 @@ public ClientBuilder withSender(Sender sender) {

/**
* Changes the <b>Serializer</b> from the default <b>SmartySerializer</b>.
*
* @param serializer An object that implements the <b>Serializer</b> interface.
* @return Returns <b>this</b> to accommodate method chaining.
*/
Expand All @@ -86,7 +93,9 @@ public ClientBuilder withSerializer(Serializer serializer) {

/**
* This may be useful when using a local installation of the SmartyStreets APIs.
* @param baseUrl Defaults to the URL for the API corresponding to the <b>Client</b> object being built.
*
* @param baseUrl Defaults to the URL for the API corresponding to the
* <b>Client</b> object being built.
* @return Returns <b>this</b> to accommodate method chaining.
*/
public ClientBuilder withCustomBaseUrl(String baseUrl) {
Expand All @@ -96,7 +105,9 @@ public ClientBuilder withCustomBaseUrl(String baseUrl) {

/**
* Use this to add any additional headers you need.
* @param customHeaders A String to Object <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, Object> customHeaders) {
Expand All @@ -106,6 +117,7 @@ public ClientBuilder withCustomHeaders(Map<String, Object> customHeaders) {

/**
* Use this to specify a proxy through which to send all lookups.
*
* @param proxyType Choose a java.net.Proxy.Type.
* @param proxyHost The host of the proxy server (do not include the port).
* @param proxyPort The port on the proxy server to which you wish to connect.
Expand All @@ -116,8 +128,15 @@ public ClientBuilder withProxy(Proxy.Type proxyType, String proxyHost, int proxy
return this;
}

public ClientBuilder withXForwardedFor(String ip) {
this.ip = ip;
return this;
}

/**
* Enables debug mode, which will print information about the HTTP request and response to the console.
* Enables debug mode, which will print information about the HTTP request and
* response to the console.
*
* @return Returns <b>this</b> to accommodate method chaining.
*/
public ClientBuilder withDebug() {
Expand All @@ -127,6 +146,7 @@ public ClientBuilder withDebug() {

/**
* Allows caller to specify licenses (aka "tracks") they wish to use.
*
* @return Returns <b>this</b> to accommodate method chaining.
*/
public ClientBuilder withLicenses(List<String> licenses) {
Expand Down Expand Up @@ -184,23 +204,27 @@ private Sender buildSender() {
return this.httpSender;

Sender sender;
if (this.proxy != null)
if (this.proxy != null) {
sender = new SmartySender(this.maxTimeout, this.proxy);
else
} else {
sender = new SmartySender(this.maxTimeout);
}

sender = new StatusCodeSender(sender);

if (this.customHeaders != null)
if (this.ip != null) {
customHeaders.put("X-Forwarded-For", this.ip);
}
if (this.customHeaders != null) {
sender = new CustomHeaderSender(this.customHeaders, sender);

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

}
sender = new URLPrefixSender(this.urlPrefix, sender);

if (this.maxRetries > 0)
if (this.maxRetries > 0) {
sender = new RetrySender(this.maxRetries, new MySleeper(), new MyLogger(), sender);
}

sender = new LicenseSender(this.licenses, sender);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ public void testAllCustomHeadersAreAddedToTheRequest() throws Exception {
RequestCapturingSender inner = new RequestCapturingSender();
CustomHeaderSender sender = new CustomHeaderSender(headers, inner);
Request request = new Request();

sender.send(request);

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

}
}
29 changes: 29 additions & 0 deletions src/test/java/com/smartystreets/api/XForwardedForTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.smartystreets.api;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import com.smartystreets.api.mocks.RequestCapturingSender;

public class XForwardedForTest {
@Test
public void testAllCustomHeadersAreAddedToTheRequest() throws Exception {
HashMap<String, Object> headers = new HashMap<>();
headers.put("X-Forwarded-For", "ip");
RequestCapturingSender inner = new RequestCapturingSender();
CustomHeaderSender sender = new CustomHeaderSender(headers, inner);
Request request = new Request();

sender.send(request);

Map<String, Object> requestHeaders = inner.getRequest().getHeaders();
assertNotNull("Headers here.", requestHeaders);
assertEquals(headers.get("X-Forwarded-For"), inner.getRequest().getHeaders().get("X-Forwarded-For"));

}
}

0 comments on commit bae215c

Please sign in to comment.