diff --git a/src/main/java/io/ljunggren/usps/api/service/BaseService.java b/src/main/java/io/ljunggren/usps/api/service/BaseService.java index f803004..dd8a67d 100644 --- a/src/main/java/io/ljunggren/usps/api/service/BaseService.java +++ b/src/main/java/io/ljunggren/usps/api/service/BaseService.java @@ -26,8 +26,11 @@ public class BaseService { + protected static final int CONNECT_TIMEOUT_MS = 5000; + protected static final int SOCKET_TIMEOUT_MS = 15000; + protected String domain; - + protected BaseService(String domain) { this.domain = domain; } @@ -35,7 +38,11 @@ protected CloseableHttpClient getHttpClient() { return HttpClients.custom() .setDefaultRequestConfig(RequestConfig.custom() - .setCookieSpec(CookieSpecs.STANDARD).build()) + .setCookieSpec(CookieSpecs.STANDARD) + .setConnectTimeout(CONNECT_TIMEOUT_MS) + .setConnectionRequestTimeout(CONNECT_TIMEOUT_MS) + .setSocketTimeout(SOCKET_TIMEOUT_MS) + .build()) .build(); } diff --git a/src/main/java/io/ljunggren/usps/api/service/OauthService.java b/src/main/java/io/ljunggren/usps/api/service/OauthService.java index 248f31e..bbdebd9 100644 --- a/src/main/java/io/ljunggren/usps/api/service/OauthService.java +++ b/src/main/java/io/ljunggren/usps/api/service/OauthService.java @@ -1,5 +1,6 @@ package io.ljunggren.usps.api.service; +import java.io.IOException; import java.util.Arrays; import java.util.List; @@ -17,6 +18,9 @@ public class OauthService extends BaseService { + private static final int MAX_ATTEMPTS = 3; + private static final long RETRY_BACKOFF_MS = 1000; + public OauthService(String domain) { super(domain); } @@ -30,23 +34,32 @@ public AuthToken authorize(String clientId, String clientSecret, CloseableHttpClient httpClient) throws Exception { HttpPost post = new HttpPost(URI); List parameters = generateParameters(clientId, clientSecret); + post.setEntity(new UrlEncodedFormEntity(parameters)); + IOException lastFailure = null; try { - post.setEntity(new UrlEncodedFormEntity(parameters)); - try (CloseableHttpResponse response = httpClient.execute(post)) { - int responseCode = response.getStatusLine().getStatusCode(); - if (HttpStatusCode.THROTTLING.getCode() == responseCode) { - throw429(); + for (int attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) { + try (CloseableHttpResponse response = httpClient.execute(post)) { + int responseCode = response.getStatusLine().getStatusCode(); + if (HttpStatusCode.THROTTLING.getCode() == responseCode) { + throw429(); + } + if (HttpStatusCode.SERVICE_UNAVAILABLE.getCode() == responseCode) { + throw new Exception("USPS API not found"); + } + String json = getResult(response); + checkForError(responseCode, json); + return JsonUtils.jsonToObject(json, AuthToken.class); + } catch (IOException e) { + lastFailure = e; + if (attempt < MAX_ATTEMPTS) { + Thread.sleep(RETRY_BACKOFF_MS * attempt); + } } - if (HttpStatusCode.SERVICE_UNAVAILABLE.getCode() == responseCode) { - throw new Exception("USPS API not found"); - } - String json = getResult(response); - checkForError(responseCode, json); - return JsonUtils.jsonToObject(json, AuthToken.class); } } finally { httpClient.close(); } + throw lastFailure; } public Debug debug(String clientId, String clientSecret) throws Exception {