// Path: src/main/java/com/verifalia/api/exceptions/EndpointServerErrorException.java
// public class EndpointServerErrorException extends VerifaliaException {
//     public EndpointServerErrorException(String errorMessage) {
//         super(errorMessage);
//     }
// }
// 
// Path: src/main/java/com/verifalia/api/exceptions/ServiceUnreachableException.java
// public class ServiceUnreachableException extends VerifaliaException {
//     public ServiceUnreachableException(String errorMessage) {
//         super(errorMessage);
//     }
// }
// 
// Path: src/main/java/com/verifalia/api/exceptions/VerifaliaException.java
// public class VerifaliaException extends Exception {
//     private RestResponse response;
// 
//     public VerifaliaException(@NonNull final RestResponse response) {
//         super(response.getStatusCode() + ": " + response.readAsString());
//         this.response = response;
//     }
// 
//     public VerifaliaException(String errorMessage) {
//         super(errorMessage);
//     }
// 
//     public VerifaliaException(String message, Throwable cause) {
//         super(message, cause);
//     }
// }
// 
// Path: src/main/java/com/verifalia/api/rest/security/AuthenticationProvider.java
// @Getter
// @Setter
// public abstract class AuthenticationProvider {
//     public void decorateRequest(RestClient client, HttpRequestBase request) throws VerifaliaException {
//     }
// 
//     public CloseableHttpClient buildClient(RestClient client) throws IOException {
//         return HttpClients.createDefault();
//     }
// }


import com.verifalia.api.exceptions.EndpointServerErrorException;
import com.verifalia.api.exceptions.ServiceUnreachableException;
import com.verifalia.api.exceptions.VerifaliaException;
import com.verifalia.api.rest.security.AuthenticationProvider;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import static java.util.Objects.nonNull;
                URI baseURI = this.baseURIs.get(currentBaseURIIndex++ % this.baseURIs.size());

                try {
                    response = sendRequest(baseURI, request, authenticationProvider);
                } catch (IOException e) {
                    // Continue with the next attempt on IO exceptions, if needed
                    errors.add(new EndpointServerError(baseURI, e));
                    continue;
                }

                if (nonNull(response)) {
                    int statusCode = response.getStatusLine().getStatusCode();

                    // Automatically retry with another host on HTTP 5xx status codes

                    if (statusCode >= 500 && statusCode <= 599) {
                        errors.add(new EndpointServerError(baseURI, new EndpointServerErrorException(String.format("The API endpoint %s returned a server error HTTP status code %d.", baseURI, statusCode))));
                        continue;
                    }

                    return new RestResponse(statusCode, response.getEntity());
                }
            }
        }

        // Aggregate exception

        StringBuilder sbAggregateError = new StringBuilder("All the base URIs are unreachable: ");
        errors.forEach(e -> sbAggregateError.append(e.baseUri).append(" => ").append(e.exception.getMessage()).append(" "));

// Path: src/main/java/com/verifalia/api/exceptions/EndpointServerErrorException.java
// public class EndpointServerErrorException extends VerifaliaException {
//     public EndpointServerErrorException(String errorMessage) {
//         super(errorMessage);
//     }
// }
// 
// Path: src/main/java/com/verifalia/api/exceptions/ServiceUnreachableException.java
// public class ServiceUnreachableException extends VerifaliaException {
//     public ServiceUnreachableException(String errorMessage) {
//         super(errorMessage);
//     }
// }
// 
// Path: src/main/java/com/verifalia/api/exceptions/VerifaliaException.java
// public class VerifaliaException extends Exception {
//     private RestResponse response;
// 
//     public VerifaliaException(@NonNull final RestResponse response) {
//         super(response.getStatusCode() + ": " + response.readAsString());
//         this.response = response;
//     }
// 
//     public VerifaliaException(String errorMessage) {
//         super(errorMessage);
//     }
// 
//     public VerifaliaException(String message, Throwable cause) {
//         super(message, cause);
//     }
// }
// 
// Path: src/main/java/com/verifalia/api/rest/security/AuthenticationProvider.java
// @Getter
// @Setter
// public abstract class AuthenticationProvider {
//     public void decorateRequest(RestClient client, HttpRequestBase request) throws VerifaliaException {
//     }
// 
//     public CloseableHttpClient buildClient(RestClient client) throws IOException {
//         return HttpClients.createDefault();
//     }
// }


// Path: src/main/java/com/verifalia/api/rest/RestClient.java
import com.verifalia.api.exceptions.EndpointServerErrorException;
import com.verifalia.api.exceptions.ServiceUnreachableException;
import com.verifalia.api.exceptions.VerifaliaException;
import com.verifalia.api.rest.security.AuthenticationProvider;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import static java.util.Objects.nonNull;

                URI baseURI = this.baseURIs.get(currentBaseURIIndex++ % this.baseURIs.size());

                try {
                    response = sendRequest(baseURI, request, authenticationProvider);
                } catch (IOException e) {
                    // Continue with the next attempt on IO exceptions, if needed
                    errors.add(new EndpointServerError(baseURI, e));
                    continue;
                }

                if (nonNull(response)) {
                    int statusCode = response.getStatusLine().getStatusCode();

                    // Automatically retry with another host on HTTP 5xx status codes

                    if (statusCode >= 500 && statusCode <= 599) {
                        errors.add(new EndpointServerError(baseURI, new EndpointServerErrorException(String.format("The API endpoint %s returned a server error HTTP status code %d.", baseURI, statusCode))));
                        continue;
                    }

                    return new RestResponse(statusCode, response.getEntity());
                }
            }
        }

        // Aggregate exception

        StringBuilder sbAggregateError = new StringBuilder("All the base URIs are unreachable: ");
        errors.forEach(e -> sbAggregateError.append(e.baseUri).append(" => ").append(e.exception.getMessage()).append(" "));

        throw new ServiceUnreachableException(sbAggregateError.toString());