use of uk.nhs.adaptors.scr.exceptions.NoSpineResultException in project summary-care-record-api by NHSDigital.
the class SpineClient method getScrProcessingResult.
@Override
@LogExecutionTime
public ProcessingResult getScrProcessingResult(String contentLocation, long initialWaitTime, String nhsdAsid, String nhsdIdentity, String nhsdSessionUrid) {
var repeatTimeout = spineConfiguration.getScrResultRepeatTimeout();
var template = RetryTemplate.builder().withinMillis(repeatTimeout).customBackoff(new ScrRetryBackoffPolicy()).retryOn(NoSpineResultException.class).build();
LOGGER.info("Starting polling result. First request in {}ms", initialWaitTime);
try {
Thread.sleep(initialWaitTime);
} catch (InterruptedException e) {
throw new ScrTimeoutException(e);
}
return template.execute(ctx -> {
LOGGER.info("Fetching SCR processing result. RetryCount={}", ctx.getRetryCount());
var request = new HttpGet(spineConfiguration.getUrl() + contentLocation);
setCommonHeaders(request, nhsdAsid, nhsdIdentity, nhsdSessionUrid);
var result = spineHttpClient.sendRequest(request, stringResponseHandler);
int statusCode = result.getStatusCode();
if (statusCode == OK.value()) {
LOGGER.info("{} processing result received.", statusCode);
return ProcessingResult.parseProcessingResult(result.getBody());
} else if (statusCode == ACCEPTED.value()) {
var nextRetryAfter = Long.parseLong(SpineHttpClient.getHeader(result.getHeaders(), RETRY_AFTER));
LOGGER.info("{} received. NextRetry in {}ms", statusCode, nextRetryAfter);
throw new NoSpineResultException(nextRetryAfter);
} else {
LOGGER.error("Unexpected spine polling response: {} {}", statusCode, result.getBody());
throw new UnexpectedSpineResponseException("Unexpected spine polling response " + statusCode);
}
});
}
Aggregations