Skip to content

Commit 8a00492

Browse files
Allow user override of default API timeouts (#93)
* T38121 [Java CL] Add Getters/Setters for the Request and Connection Timeouts - Add setters/getters for all three timeouts - Add test to check that setters/getters operate as expected - Code rearrangement per file layout rules * - Minor auto-formatting after rebase * - Remove orphaned comments * Update src/main/java/com/easypost/net/EasyPostResource.java Co-authored-by: Justin Hammond <[email protected]> * Update src/main/java/com/easypost/net/EasyPostResource.java Co-authored-by: Justin Hammond <[email protected]> * - Fix bad indent * - Remove improper comment about URL_STREAM_HANDLER Co-authored-by: Justin Hammond <[email protected]>
1 parent 6a056bd commit 8a00492

File tree

4 files changed

+77
-11
lines changed

4 files changed

+77
-11
lines changed

src/main/java/com/easypost/model/EventDeserializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ private Object deserializeJsonElement(final JsonElement element) {
8989
} else if (element.isJsonArray()) {
9090
return deserializeJsonArray(element.getAsJsonArray());
9191
} else {
92-
System.err.printf("Unknown JSON element type for element %s. Please email us at %s.%n",
93-
element.toString(), EasyPostResource.EASYPOST_SUPPORT_EMAIL);
92+
System.err.printf("Unknown JSON element type for element %s. Please email us at %s.%n", element,
93+
EasyPostResource.EASYPOST_SUPPORT_EMAIL);
9494
return null;
9595
}
9696
}

src/main/java/com/easypost/model/Shipment.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -787,9 +787,9 @@ public List<Rate> getSmartrates(final String apiKey) throws EasyPostException {
787787
* @throws EasyPostException when the request fails.
788788
*/
789789
public List<Rate> getSmartrates(final Map<String, Object> params, final String apiKey) throws EasyPostException {
790-
SmartrateCollection smartrateCollection = request(RequestMethod.GET,
791-
String.format("%s/smartrate", instanceURL(Shipment.class, this.getId())), params,
792-
SmartrateCollection.class, apiKey);
790+
SmartrateCollection smartrateCollection =
791+
request(RequestMethod.GET, String.format("%s/smartrate", instanceURL(Shipment.class, this.getId())),
792+
params, SmartrateCollection.class, apiKey);
793793

794794
return smartrateCollection.getRates();
795795
}

src/main/java/com/easypost/net/EasyPostResource.java

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ public abstract class EasyPostResource {
5959
new ArrayList<>(Arrays.asList("getCreatedAt", "getUpdatedAt", "getFees"));
6060
private static final int DEFAULT_CONNECT_TIMEOUT_MILLISECONDS = 30000;
6161
private static final int DEFAULT_READ_TIMEOUT_MILLISECONDS = 60000;
62-
private static final double APP_ENGINE_DEFAULT_TIMEOUT_SECONDS = 20.0;
62+
private static final double DEFAULT_APP_ENGINE_TIMEOUT_SECONDS = 20.0;
6363
private static final String DNS_CACHE_TTL_PROPERTY_NAME = "networkaddress.cache.ttl";
64-
// Set this property to override your environment's default URLStreamHandler.
6564
private static final String CUSTOM_URL_STREAM_HANDLER_PROPERTY_NAME = "com.easypost.net.customURLStreamHandler";
65+
private static int connectTimeoutMilliseconds = DEFAULT_CONNECT_TIMEOUT_MILLISECONDS;
66+
private static int readTimeoutMilliseconds = DEFAULT_READ_TIMEOUT_MILLISECONDS;
67+
private static double appEngineTimeoutSeconds = DEFAULT_APP_ENGINE_TIMEOUT_SECONDS;
6668
private Date createdAt;
6769
private Date updatedAt;
6870
private ArrayList<Fee> fees;
@@ -150,13 +152,13 @@ private static javax.net.ssl.HttpsURLConnection createEasyPostConnection(final S
150152
easypostURL = new URL(url);
151153
}
152154
javax.net.ssl.HttpsURLConnection conn = (javax.net.ssl.HttpsURLConnection) easypostURL.openConnection();
153-
conn.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLISECONDS);
155+
conn.setConnectTimeout(getConnectTimeoutMilliseconds());
154156

155157
int readTimeout;
156158
if (EasyPost.readTimeout != 0) {
157159
readTimeout = EasyPost.readTimeout;
158160
} else {
159-
readTimeout = DEFAULT_READ_TIMEOUT_MILLISECONDS;
161+
readTimeout = getReadTimeoutMilliseconds();
160162
}
161163
conn.setReadTimeout(readTimeout);
162164

@@ -470,7 +472,7 @@ private static EasyPostResponse makeAppEngineRequest(final RequestMethod method,
470472

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

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

@@ -525,6 +527,60 @@ private static EasyPostResponse makeAppEngineRequest(final RequestMethod method,
525527
}
526528
}
527529

530+
/**
531+
* Get the timeout in milliseconds for connecting to the API.
532+
*
533+
* @return the timeout in milliseconds
534+
*/
535+
public static int getConnectTimeoutMilliseconds() {
536+
return connectTimeoutMilliseconds;
537+
}
538+
539+
/**
540+
* Set the timeout in milliseconds for connecting to the API.
541+
*
542+
* @param milliseconds the timeout in milliseconds
543+
*/
544+
public static void setConnectTimeoutMilliseconds(int milliseconds) {
545+
connectTimeoutMilliseconds = milliseconds;
546+
}
547+
548+
/**
549+
* Get the timeout in milliseconds for reading API responses.
550+
*
551+
* @return the timeout in milliseconds
552+
*/
553+
public static int getReadTimeoutMilliseconds() {
554+
return readTimeoutMilliseconds;
555+
}
556+
557+
/**
558+
* Set the timeout in milliseconds for reading API responses.
559+
*
560+
* @param milliseconds the timeout in milliseconds
561+
*/
562+
public static void setReadTimeoutMilliseconds(int milliseconds) {
563+
readTimeoutMilliseconds = milliseconds;
564+
}
565+
566+
/**
567+
* Get the timeout in milliseconds for App Engine API requests.
568+
*
569+
* @return the timeout in milliseconds
570+
*/
571+
public static double getAppEngineTimeoutSeconds() {
572+
return appEngineTimeoutSeconds;
573+
}
574+
575+
/**
576+
* Set the timeout in seconds for App Engine API requests.
577+
*
578+
* @param seconds the timeout in seconds
579+
*/
580+
public static void setAppEngineTimeoutSeconds(double seconds) {
581+
appEngineTimeoutSeconds = seconds;
582+
}
583+
528584
/**
529585
* Returns a string representation of the object.
530586
*/
@@ -703,7 +759,6 @@ protected enum RequestMethod {
703759
PUT
704760
}
705761

706-
// represents Errors returned as JSON
707762
private static class ErrorContainer {
708763
private EasyPostResource.Error error;
709764
}

src/test/java/com/easypost/EasyPostTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,17 @@ public void testScanForm() throws EasyPostException {
10631063
assertEquals("IDs do not match", scanForms.getScanForms().get(0).getId(), scanForm.getId());
10641064
}
10651065

1066+
@Test
1067+
public void testClientTimeout() throws EasyPostException {
1068+
int timeout = 1;
1069+
Order.setConnectTimeoutMilliseconds(timeout);
1070+
Order.setReadTimeoutMilliseconds(timeout);
1071+
Order.setAppEngineTimeoutSeconds(timeout);
1072+
assertEquals(Order.getConnectTimeoutMilliseconds(), timeout);
1073+
assertEquals(Order.getReadTimeoutMilliseconds(), timeout);
1074+
assertEquals(Order.getAppEngineTimeoutSeconds(), timeout, 0.001);
1075+
}
1076+
10661077

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

0 commit comments

Comments
 (0)