Search in sources :

Example 1 with NoSpineResultException

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);
        }
    });
}
Also used : UnexpectedSpineResponseException(uk.nhs.adaptors.scr.exceptions.UnexpectedSpineResponseException) HttpGet(org.apache.http.client.methods.HttpGet) NoSpineResultException(uk.nhs.adaptors.scr.exceptions.NoSpineResultException) BackOffInterruptedException(org.springframework.retry.backoff.BackOffInterruptedException) ScrTimeoutException(uk.nhs.adaptors.scr.exceptions.ScrTimeoutException) LogExecutionTime(uk.nhs.adaptors.scr.logging.LogExecutionTime)

Aggregations

HttpGet (org.apache.http.client.methods.HttpGet)1 BackOffInterruptedException (org.springframework.retry.backoff.BackOffInterruptedException)1 NoSpineResultException (uk.nhs.adaptors.scr.exceptions.NoSpineResultException)1 ScrTimeoutException (uk.nhs.adaptors.scr.exceptions.ScrTimeoutException)1 UnexpectedSpineResponseException (uk.nhs.adaptors.scr.exceptions.UnexpectedSpineResponseException)1 LogExecutionTime (uk.nhs.adaptors.scr.logging.LogExecutionTime)1