Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/com/easypost/model/EventDeserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ private Object deserializeJsonElement(final JsonElement element) {
} else if (element.isJsonArray()) {
return deserializeJsonArray(element.getAsJsonArray());
} else {
System.err.printf("Unknown JSON element type for element %s. Please email us at %s.%n",
element.toString(), EasyPostResource.EASYPOST_SUPPORT_EMAIL);
System.err.printf("Unknown JSON element type for element %s. Please email us at %s.%n", element,
EasyPostResource.EASYPOST_SUPPORT_EMAIL);
return null;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/easypost/model/Shipment.java
Original file line number Diff line number Diff line change
Expand Up @@ -787,9 +787,9 @@ public List<Rate> getSmartrates(final String apiKey) throws EasyPostException {
* @throws EasyPostException when the request fails.
*/
public List<Rate> getSmartrates(final Map<String, Object> params, final String apiKey) throws EasyPostException {
SmartrateCollection smartrateCollection = request(RequestMethod.GET,
String.format("%s/smartrate", instanceURL(Shipment.class, this.getId())), params,
SmartrateCollection.class, apiKey);
SmartrateCollection smartrateCollection =
request(RequestMethod.GET, String.format("%s/smartrate", instanceURL(Shipment.class, this.getId())),
params, SmartrateCollection.class, apiKey);

return smartrateCollection.getRates();
}
Expand Down
67 changes: 61 additions & 6 deletions src/main/java/com/easypost/net/EasyPostResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ public abstract class EasyPostResource {
new ArrayList<>(Arrays.asList("getCreatedAt", "getUpdatedAt", "getFees"));
private static final int DEFAULT_CONNECT_TIMEOUT_MILLISECONDS = 30000;
private static final int DEFAULT_READ_TIMEOUT_MILLISECONDS = 60000;
private static final double APP_ENGINE_DEFAULT_TIMEOUT_SECONDS = 20.0;
private static final double DEFAULT_APP_ENGINE_TIMEOUT_SECONDS = 20.0;
private static final String DNS_CACHE_TTL_PROPERTY_NAME = "networkaddress.cache.ttl";
// Set this property to override your environment's default URLStreamHandler.
private static final String CUSTOM_URL_STREAM_HANDLER_PROPERTY_NAME = "com.easypost.net.customURLStreamHandler";
private static int connectTimeoutMilliseconds = DEFAULT_CONNECT_TIMEOUT_MILLISECONDS;
private static int readTimeoutMilliseconds = DEFAULT_READ_TIMEOUT_MILLISECONDS;
private static double appEngineTimeoutSeconds = DEFAULT_APP_ENGINE_TIMEOUT_SECONDS;
private Date createdAt;
private Date updatedAt;
private ArrayList<Fee> fees;
Expand Down Expand Up @@ -150,13 +152,13 @@ private static javax.net.ssl.HttpsURLConnection createEasyPostConnection(final S
easypostURL = new URL(url);
}
javax.net.ssl.HttpsURLConnection conn = (javax.net.ssl.HttpsURLConnection) easypostURL.openConnection();
conn.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLISECONDS);
conn.setConnectTimeout(getConnectTimeoutMilliseconds());

int readTimeout;
if (EasyPost.readTimeout != 0) {
readTimeout = EasyPost.readTimeout;
} else {
readTimeout = DEFAULT_READ_TIMEOUT_MILLISECONDS;
readTimeout = getReadTimeoutMilliseconds();
}
conn.setReadTimeout(readTimeout);

Expand Down Expand Up @@ -470,7 +472,7 @@ private static EasyPostResponse makeAppEngineRequest(final RequestMethod method,

// Heroku times out after 30s, so leave some time for the API to return a response
fetchOptionsClass.getDeclaredMethod("setDeadline", java.lang.Double.class)
.invoke(fetchOptions, APP_ENGINE_DEFAULT_TIMEOUT_SECONDS);
.invoke(fetchOptions, getAppEngineTimeoutSeconds());

Class<?> requestClass = Class.forName("com.google.appengine.api.urlfetch.HTTPRequest");

Expand Down Expand Up @@ -525,6 +527,60 @@ private static EasyPostResponse makeAppEngineRequest(final RequestMethod method,
}
}

/**
* Get the timeout in milliseconds for connecting to the API.
*
* @return the timeout in milliseconds
*/
public static int getConnectTimeoutMilliseconds() {
return connectTimeoutMilliseconds;
}

/**
* Set the timeout in milliseconds for connecting to the API.
*
* @param milliseconds the timeout in milliseconds
*/
public static void setConnectTimeoutMilliseconds(int milliseconds) {
connectTimeoutMilliseconds = milliseconds;
}

/**
* Get the timeout in milliseconds for reading API responses.
*
* @return the timeout in milliseconds
*/
public static int getReadTimeoutMilliseconds() {
return readTimeoutMilliseconds;
}

/**
* Set the timeout in milliseconds for reading API responses.
*
* @param milliseconds the timeout in milliseconds
*/
public static void setReadTimeoutMilliseconds(int milliseconds) {
readTimeoutMilliseconds = milliseconds;
}

/**
* Get the timeout in milliseconds for App Engine API requests.
*
* @return the timeout in milliseconds
*/
public static double getAppEngineTimeoutSeconds() {
return appEngineTimeoutSeconds;
}

/**
* Set the timeout in seconds for App Engine API requests.
*
* @param seconds the timeout in seconds
*/
public static void setAppEngineTimeoutSeconds(double seconds) {
appEngineTimeoutSeconds = seconds;
}

/**
* Returns a string representation of the object.
*/
Expand Down Expand Up @@ -703,7 +759,6 @@ protected enum RequestMethod {
PUT
}

// represents Errors returned as JSON
private static class ErrorContainer {
private EasyPostResource.Error error;
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/com/easypost/EasyPostTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,17 @@ public void testScanForm() throws EasyPostException {
assertEquals("IDs do not match", scanForms.getScanForms().get(0).getId(), scanForm.getId());
}

@Test
public void testClientTimeout() throws EasyPostException {
int timeout = 1;
Order.setConnectTimeoutMilliseconds(timeout);
Order.setReadTimeoutMilliseconds(timeout);
Order.setAppEngineTimeoutSeconds(timeout);
assertEquals(Order.getConnectTimeoutMilliseconds(), timeout);
assertEquals(Order.getReadTimeoutMilliseconds(), timeout);
assertEquals(Order.getAppEngineTimeoutSeconds(), timeout, 0.001);
}


//This test needs to have new set of dates to avoid "report already exists" error
/*
Expand Down