Skip to content

Commit

Permalink
resolved comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Sachin-Mamoru committed Apr 13, 2024
1 parent 6bb2177 commit dd0c083
Showing 1 changed file with 25 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ public abstract class AbstractHTTPFunction {
protected static final String TYPE_TEXT_PLAIN = "text/plain";
private static final char DOMAIN_SEPARATOR = '.';
private static final String RESPONSE = "response";
private static final int HTTP_STATUS_REQUEST_TIMEOUT = 408;
private static final int HTTP_STATUS_INTERNAL_SERVER_ERROR = 500;
private static final int HTTP_STATUS_BAD_REQUEST = 400;
private final int requestRetryCount;
private final List<String> allowedDomains;

Expand Down Expand Up @@ -110,8 +107,8 @@ protected void executeHttpMethod(HttpUriRequest clientRequest, Map<String, Objec
endpointURL);
asyncReturn.accept(context, Collections.emptyMap(), Constants.OUTCOME_FAIL);
} else {
Pair<Integer, Pair<String, JSONObject>> result = executeRequest(request, endpointURL);
if (result.getLeft() >= 400 && result.getLeft() <= 500) {
Pair<Boolean, Pair<String, JSONObject>> result = executeRequest(request, endpointURL);
if (Boolean.TRUE.equals(result.getLeft())) {
LOG.error("Error while calling endpoint. Url: " + endpointURL);
result = executeRequestWithRetries(request, endpointURL, requestRetryCount);
}
Expand All @@ -135,13 +132,13 @@ protected void executeHttpMethod(HttpUriRequest clientRequest, Map<String, Objec
* @param maxRetries Maximum number of retries.
* @return Pair of outcome and json.
*/
private Pair<Integer, Pair<String, JSONObject>> executeRequestWithRetries
private Pair<Boolean, Pair<String, JSONObject>> executeRequestWithRetries
(HttpUriRequest request, String endpointURL, int maxRetries) {

Pair<Integer, Pair<String, JSONObject>> result = Pair.of(0, Pair.of(Constants.OUTCOME_FAIL, null));
JSONObject json = null;
Pair<Boolean, Pair<String, JSONObject>> result;
String outcome = Constants.OUTCOME_FAIL;
int attempts = 0;
boolean isRetry = false;

while (attempts < maxRetries) {
attempts++;
Expand All @@ -158,11 +155,12 @@ protected void executeHttpMethod(HttpUriRequest clientRequest, Map<String, Objec
LoggerUtils.triggerDiagnosticLogEvent(diagnosticLogBuilder);
}
result = executeRequest(request, endpointURL);
if (result.getLeft() >= 200 && result.getLeft() < 400) {
isRetry = result.getLeft();
if (!isRetry) {
return result;
}
}
return Pair.of(result.getLeft(), Pair.of(outcome, json));
return Pair.of(isRetry, Pair.of(outcome, null));
}

/**
Expand All @@ -172,11 +170,11 @@ protected void executeHttpMethod(HttpUriRequest clientRequest, Map<String, Objec
* @param endpointURL Endpoint URL.
* @return Pair of outcome and json.
*/
private Pair<Integer, Pair<String, JSONObject>> executeRequest(HttpUriRequest request, String endpointURL) {
private Pair<Boolean, Pair<String, JSONObject>> executeRequest(HttpUriRequest request, String endpointURL) {

JSONObject json = null;
String outcome;
int statuscode = 500;
boolean isRetry = false;

try (CloseableHttpResponse response = client.execute(request)) {
int responseCode = response.getStatusLine().getStatusCode();
Expand Down Expand Up @@ -205,7 +203,7 @@ private Pair<Integer, Pair<String, JSONObject>> executeRequest(HttpUriRequest re
LOG.info("Successfully called the external api. Status code: " + responseCode + ". Url: " +
endpointURL);
outcome = Constants.OUTCOME_SUCCESS;
return Pair.of(responseCode, Pair.of(outcome, json)); // Success, return immediately
return Pair.of(false, Pair.of(outcome, json)); // Success, return immediately
} else if (responseCode >= 300 && responseCode < 400) {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
Expand All @@ -221,7 +219,7 @@ private Pair<Integer, Pair<String, JSONObject>> executeRequest(HttpUriRequest re
LOG.warn("External api invocation returned a redirection. Status code: " +
responseCode + ". Url: " + endpointURL);
outcome = Constants.OUTCOME_FAIL;
return Pair.of(responseCode, Pair.of(outcome, null)); // Unauthorized, no retry
return Pair.of(false, Pair.of(outcome, null)); // Unauthorized, no retry
} else if (responseCode >= 400 && responseCode < 500) {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
Expand All @@ -237,7 +235,7 @@ private Pair<Integer, Pair<String, JSONObject>> executeRequest(HttpUriRequest re
LOG.warn("External api invocation returned a client error. Status code: " +
responseCode + ". Url: " + endpointURL);
outcome = Constants.OUTCOME_FAIL;
return Pair.of(responseCode, Pair.of(outcome, null)); // Unauthorized, no retry
return Pair.of(false, Pair.of(outcome, null)); // Unauthorized, no retry
} else {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
Expand All @@ -253,7 +251,7 @@ private Pair<Integer, Pair<String, JSONObject>> executeRequest(HttpUriRequest re
LOG.error("Received unknown response from external API call. Status code: " +
responseCode + ". Url: " + endpointURL);
outcome = Constants.OUTCOME_FAIL;
return Pair.of(responseCode, Pair.of(outcome, null)); // Server error, retry if attempts left
return Pair.of(true, Pair.of(outcome, null)); // Server error, retry if attempts left
}
} catch (Exception e) {
// Log the error based on its type
Expand All @@ -268,7 +266,6 @@ private Pair<Integer, Pair<String, JSONObject>> executeRequest(HttpUriRequest re
.resultStatus(DiagnosticLog.ResultStatus.FAILED);
LoggerUtils.triggerDiagnosticLogEvent(diagnosticLogBuilder);
}
statuscode = HTTP_STATUS_BAD_REQUEST; // Client error, retry if attempts left
outcome = Constants.OUTCOME_FAIL;
LOG.error("Invalid Url: " + endpointURL, e);
} else if (e instanceof ConnectTimeoutException || e instanceof SocketTimeoutException) {
Expand All @@ -277,15 +274,17 @@ private Pair<Integer, Pair<String, JSONObject>> executeRequest(HttpUriRequest re
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
Constants.LogConstants.ActionIDs.RECEIVE_API_RESPONSE);
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.API, endpointURL)
.resultMessage("Creating a connection for the external API call timed out.")
.resultMessage("Request for the external API timed out.")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
.resultStatus(DiagnosticLog.ResultStatus.FAILED);
LoggerUtils.triggerDiagnosticLogEvent(diagnosticLogBuilder);
}
outcome = Constants.OUTCOME_TIMEOUT; // Timeout, retry if attempts left
isRetry = true; // Timeout, retry if attempts left
outcome = Constants.OUTCOME_TIMEOUT;
LOG.error("Error while waiting to connect to " + endpointURL, e);
} else if (e instanceof IOException) {
outcome = Constants.OUTCOME_TIMEOUT; // Timeout, retry if attempts left
isRetry = true; // Timeout, retry if attempts left
outcome = Constants.OUTCOME_TIMEOUT;
LOG.error("Error while calling endpoint. ", e);
} else if (e instanceof ParseException) {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
Expand All @@ -298,25 +297,27 @@ private Pair<Integer, Pair<String, JSONObject>> executeRequest(HttpUriRequest re
.resultStatus(DiagnosticLog.ResultStatus.FAILED);
LoggerUtils.triggerDiagnosticLogEvent(diagnosticLogBuilder);
}
outcome = Constants.OUTCOME_FAIL; // Server error, retry if attempts left
isRetry = true; // Timeout, retry if attempts left
outcome = Constants.OUTCOME_FAIL;
LOG.error("Error while parsing response. ", e);
} else {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
Constants.LogConstants.ActionIDs.RECEIVE_API_RESPONSE);
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.API, endpointURL)
.resultMessage("Received unknown exception from external API call.")
.resultMessage("Received an error while invoking the external API.")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
.resultStatus(DiagnosticLog.ResultStatus.FAILED);
LoggerUtils.triggerDiagnosticLogEvent(diagnosticLogBuilder);
}
outcome = Constants.OUTCOME_FAIL; // Server error, retry if attempts left
isRetry = true; // Timeout, retry if attempts left
outcome = Constants.OUTCOME_FAIL;
LOG.error("Error while calling endpoint. ", e);
}
}
// Return the outcome and json (which might be null if never successful)
return Pair.of(statuscode, Pair.of(outcome, json));
return Pair.of(isRetry, Pair.of(outcome, json));
}

private boolean isValidRequestDomain(URI url) {
Expand Down

0 comments on commit dd0c083

Please sign in to comment.