60 lines
2.1 KiB
Java
60 lines
2.1 KiB
Java
package com.banesco.common.infrastructure.config;
|
|
|
|
import com.banesco.common.domain.model.PaymentRequestConfig;
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import io.quarkus.runtime.annotations.RegisterForReflection;
|
|
import jakarta.enterprise.context.ApplicationScoped;
|
|
import jakarta.inject.Inject;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.eclipse.microprofile.config.Config;
|
|
|
|
import java.util.Map;
|
|
|
|
@Slf4j
|
|
@ApplicationScoped
|
|
@RegisterForReflection
|
|
public class RestClientConfig {
|
|
|
|
private final Config config;
|
|
private final ObjectMapper objectMapper;
|
|
|
|
private static final String API_BASE = "api.rest-client.";
|
|
private static final String API_PAYMENT_FILE_NAME = "payment-file";
|
|
|
|
@Inject
|
|
public RestClientConfig(
|
|
Config config,
|
|
ObjectMapper objectMapper
|
|
) {
|
|
this.config = config;
|
|
this.objectMapper = objectMapper;
|
|
}
|
|
|
|
public PaymentRequestConfig getPaymentRequestConfig() {
|
|
return getConfig(API_PAYMENT_FILE_NAME, PaymentRequestConfig.class);
|
|
}
|
|
|
|
private <T> T getConfig(
|
|
String configName,
|
|
Class<T> configType
|
|
) {
|
|
try {
|
|
String fullConfigName = API_BASE + configName;
|
|
String json = config.getValue(fullConfigName, String.class);
|
|
log.info("Configurando {}: {}", fullConfigName, json);
|
|
|
|
if (json == null || json.trim().isEmpty()) {
|
|
throw new IllegalStateException("Configuracion no encontrada para: " + fullConfigName);
|
|
}
|
|
|
|
Map<String, Object> configMap = objectMapper.readValue(json, new TypeReference<>() {});
|
|
return objectMapper.convertValue(configMap, configType);
|
|
} catch (JsonProcessingException e) {
|
|
throw new IllegalArgumentException("Formato JSON invalido para " + configName + ": " + e.getMessage(), e);
|
|
} catch (Exception e) {
|
|
throw new IllegalStateException("Error cargando configuracion del servicio " + configName + ": " + e.getMessage(), e);
|
|
}
|
|
}
|
|
} |