ADD - BaseWebClientBuilder
This commit is contained in:
parent
a654a8a0e3
commit
725c644fb9
@ -0,0 +1,111 @@
|
||||
package com.banesco.infraestructure.web;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* Clase constructora para crear instancias de WebClient utilizando
|
||||
* java.net.http.HttpClient
|
||||
*
|
||||
* @author [Tu Nombre]
|
||||
*/
|
||||
public class BaseWebClientBuilder {
|
||||
|
||||
private String baseUrl;
|
||||
private Duration connectTimeout;
|
||||
private Duration responseTimeout;
|
||||
|
||||
/**
|
||||
* Constructor para inicializar los timeouts con valores predeterminados.
|
||||
*/
|
||||
public BaseWebClientBuilder() {
|
||||
this.connectTimeout = Duration.ofSeconds(10);
|
||||
this.responseTimeout = Duration.ofSeconds(10);
|
||||
}
|
||||
|
||||
/**
|
||||
* Método estático para iniciar la construcción de un BaseWebClientBuilder.
|
||||
* Permite encadenar llamadas a los métodos de configuración.
|
||||
*
|
||||
* @return Una nueva instancia de BaseWebClientBuilder.
|
||||
*/
|
||||
public static BaseWebClientBuilder builder() {
|
||||
return new BaseWebClientBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Configura la URL base para el WebClient.
|
||||
*
|
||||
* @param baseUrl La URL base a utilizar.
|
||||
* @return La instancia actual de BaseWebClientBuilder para encadenar
|
||||
* llamadas.
|
||||
*/
|
||||
public BaseWebClientBuilder baseUrl(String baseUrl) {
|
||||
this.baseUrl = baseUrl;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configura el timeout de conexión.
|
||||
*
|
||||
* @param timeout El timeout de conexión.
|
||||
* @return La instancia actual de BaseWebClientBuilder para encadenar
|
||||
* llamadas.
|
||||
*/
|
||||
public BaseWebClientBuilder connectTimeout(Duration timeout) {
|
||||
this.connectTimeout = timeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configura el timeout de respuesta.
|
||||
*
|
||||
* @param timeout El timeout de respuesta.
|
||||
* @return La instancia actual de BaseWebClientBuilder para encadenar
|
||||
* llamadas.
|
||||
*/
|
||||
public BaseWebClientBuilder responseTimeout(Duration timeout) {
|
||||
this.responseTimeout = timeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construye y devuelve una instancia de HttpClient con la configuración
|
||||
* especificada.
|
||||
*
|
||||
* @return Una instancia de HttpClient configurada.
|
||||
*/
|
||||
public HttpClient build() {
|
||||
return HttpClient.newBuilder()
|
||||
.connectTimeout(connectTimeout)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Realiza una petición GET utilizando el HttpClient configurado.
|
||||
*
|
||||
* @param httpClient El HttpClient configurado.
|
||||
* @param path El path de la petición.
|
||||
* @return Un CompletableFuture que emite la respuesta como String.
|
||||
* @throws IOException
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public CompletableFuture<String> get(HttpClient httpClient, String path) {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(baseUrl + path))
|
||||
.header("Content-Type", "application/json")
|
||||
.header("Accept", "application/json")
|
||||
.timeout(responseTimeout)
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
return httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString())
|
||||
.thenApply(HttpResponse::body);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user