REFACTOR
This commit is contained in:
parent
725c644fb9
commit
5e40ae87ec
@ -1,23 +0,0 @@
|
||||
package com.banesco;
|
||||
|
||||
import com.banesco.domain.model.BackResponse;
|
||||
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args ) {
|
||||
BackResponse backResponse = BackResponse.builder()
|
||||
.backendCode("200")
|
||||
.httpCode("420")
|
||||
.statusCode("420")
|
||||
.description("asdasd")
|
||||
.build();
|
||||
|
||||
System.out.println(backResponse);
|
||||
|
||||
}
|
||||
}
|
||||
19
src/main/java/com/banesco/Commons.java
Normal file
19
src/main/java/com/banesco/Commons.java
Normal file
@ -0,0 +1,19 @@
|
||||
package com.banesco;
|
||||
|
||||
import com.banesco.domain.model.BackResponse;
|
||||
|
||||
|
||||
public class Commons {
|
||||
|
||||
public static void main(String[] args) {
|
||||
BackResponse backResponse = BackResponse.builder()
|
||||
.backendCode("200")
|
||||
.httpCode("420")
|
||||
.statusCode("420")
|
||||
.description("asdasd")
|
||||
.build();
|
||||
|
||||
System.out.println(backResponse);
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,10 +1,6 @@
|
||||
package com.banesco.infraestructure.config;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.banesco.domain.model.RequestBase;
|
||||
@ -12,80 +8,37 @@ import com.banesco.infraestructure.helpers.RequestHelper;
|
||||
import com.banesco.infraestructure.repository.SoapHeadersRepository;
|
||||
|
||||
public class RestClient {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(RestClient.class.getName());
|
||||
|
||||
private final String headerUrl;
|
||||
private final String idGeneratorUrl;
|
||||
private final String instanceIdPath;
|
||||
private final String requestIdPath;
|
||||
private String instanceId;
|
||||
private final RequestHelper requestHelper;
|
||||
private String instanceId;
|
||||
|
||||
// Constructor que recibe las URLs y la instancia de RequestHelper
|
||||
public RestClient(String headerUrl, String idGeneratorUrl, String instanceIdPath, String requestIdPath, RequestHelper requestHelper) {
|
||||
this.headerUrl = headerUrl;
|
||||
public RestClient(String headerUrl, String idGeneratorUrl, RequestHelper requestHelper) {
|
||||
this.idGeneratorUrl = idGeneratorUrl;
|
||||
this.instanceIdPath = instanceIdPath;
|
||||
this.requestIdPath = requestIdPath;
|
||||
this.requestHelper = requestHelper;
|
||||
this.init();
|
||||
}
|
||||
|
||||
// Inicializa el Instance ID si no se está ejecutando localmente.
|
||||
private void init() {
|
||||
if (!idGeneratorUrl.contains("localhost")) {
|
||||
System.out.println("Request to getInstanceId");
|
||||
this.instanceId = getInstanceId(); // Obtiene Instance ID
|
||||
System.out.println("Response to getInstanceId: " + this.instanceId);
|
||||
logger.log(Level.INFO, "Request to getInstanceId");
|
||||
this.instanceId = getInstanceId();
|
||||
logger.log(Level.INFO, "Response to getInstanceId: {0}", this.instanceId);
|
||||
}
|
||||
}
|
||||
|
||||
public RequestBase.MsgRqHdr getHeader(String headerName) {
|
||||
// Siempre usa SoapHeadersRepository independientemente de si es local o no
|
||||
return SoapHeadersRepository.getHeader(headerName);
|
||||
}
|
||||
|
||||
public String getRequestId() {
|
||||
if (idGeneratorUrl.contains("localhost")) {
|
||||
return requestHelper.getRequestId(instanceId); // Lógica local
|
||||
} else {
|
||||
try {
|
||||
String result = getHttpResponse(idGeneratorUrl + requestIdPath + instanceId); // Reutiliza función HTTP.
|
||||
System.out.println("Response to getRequestId: " + result);
|
||||
return result;
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error getting request ID: " + e.getMessage());
|
||||
return null; // Manejo de error.
|
||||
}
|
||||
}
|
||||
return requestHelper.getRequestId(instanceId);
|
||||
}
|
||||
|
||||
public String getInstanceId() {
|
||||
if (idGeneratorUrl.contains("localhost")) {
|
||||
return requestHelper.getInstanceId(); // Usa el método de RequestHelper
|
||||
} else {
|
||||
try {
|
||||
return getHttpResponse(idGeneratorUrl + instanceIdPath); // Reutiliza función HTTP.
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error getting instance ID: " + e.getMessage());
|
||||
return null; // Manejo de error.
|
||||
}
|
||||
}
|
||||
return requestHelper.getInstanceId();
|
||||
}
|
||||
|
||||
// Función reutilizable para realizar llamadas HTTP GET.
|
||||
private String getHttpResponse(String urlStr) throws IOException {
|
||||
URL url = new URL(urlStr);
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
|
||||
StringBuilder response = new StringBuilder();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
response.append(line);
|
||||
}
|
||||
return response.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user