Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions resources/sdk/purecloudjava/templates/ApiClient.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,10 @@ public class ApiClient implements AutoCloseable {
}
return new ApiResponseWrapper<>(statusCode, reasonPhrase, headers, body, entity);
}
else if (statusCode == 429) {
String message = "Max number of retries exceeded";
throw new ApiException(statusCode, message, headers, response.readBody());
}
else {
String message = "error";
String body = response.readBody();
Expand Down Expand Up @@ -889,6 +893,10 @@ public class ApiClient implements AutoCloseable {
if (e.getStatusCode() == 401 && shouldRefreshAccessToken) {
handleExpiredAccessToken();
return getAPIResponse(request, returnType, isAuthRequest);
} else if (e.getStatusCode() == 429) {
Map<String,String> responseHeaderCopy = new HashMap<>(e.getHeaders());
logger.error(connectorRequest.getMethod(), connectorRequest.getUrl(), connectorRequest.readBody(), e.getRawBody(), e.getStatusCode(), requestHeaderCopy, responseHeaderCopy);
throw e;
} else if (e.getStatusCode() < 200 || e.getStatusCode() >= 300) {
Map<String,String> responseHeaderCopy = new HashMap<>(e.getHeaders());
logger.error(connectorRequest.getMethod(), connectorRequest.getUrl(), connectorRequest.readBody(), e.getRawBody(), e.getStatusCode(), requestHeaderCopy, responseHeaderCopy);
Expand Down
31 changes: 31 additions & 0 deletions resources/sdk/purecloudjava/tests/ApiClientRetryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,37 @@ public void invokeTestWith_429_And_No_MaxRetryTime() throws IOException {
}
}

@Test
public void invokeTestWith_429_MaxRetriesExceeded() throws IOException {
ApiClient.RetryConfiguration retryConfiguration = new ApiClient.RetryConfiguration();

retryConfiguration.setMaxRetryTimeSec(6);
retryConfiguration.setRetryAfterDefaultMs(100);
retryConfiguration.setRetryMax(0);
retryConfiguration.setMaxRetriesBeforeBackoff(2);

apiClient = getApiClient(retryConfiguration);

mockResponse = getMockCloseableHttpResponse(429);

Header header = mock(Header.class);
when(header.getName()).thenReturn("Retry-After");
when(header.getValue()).thenReturn("3");

when(mockResponse.getAllHeaders()).thenReturn(new Header[]{header});
doReturn(mockResponse).when(spyClient).execute(any(HttpUriRequest.class));

try {
stopwatch = Stopwatch.createStarted();
ApiResponse<ApiClientConnectorResponse> response = apiClient.invoke(getConnectorRequest(), getReturnType());
} catch (ApiException ex) {
Assert.assertEquals(429, ex.getStatusCode());
Assert.assertEquals("Max number of retries exceeded", ex.getMessage());
Assert.assertTrue(stopwatch.elapsed(TimeUnit.MILLISECONDS) >= 3000 && stopwatch.elapsed(TimeUnit.MILLISECONDS) < 3100, "Since retryMax is 0, after one retry exception is thrown");
stopwatch.stop();
}
}

@Test
public void invokeTestWith_502() throws IOException {
ApiClient.RetryConfiguration retryConfiguration = new ApiClient.RetryConfiguration();
Expand Down