171 lines
4.5 KiB
Go
171 lines
4.5 KiB
Go
package obtencionCodigo
|
|
|
|
import (
|
|
"bytes"
|
|
"daemonService/internal/models/obtencionCodigos"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
func CodigoEnviarSOAPRequest(s obtencionCodigos.CuisServiceModel, soapReq string) (*http.Response, []byte, error) {
|
|
var resp *http.Response
|
|
var bodyBytes []byte
|
|
var lastErr error
|
|
|
|
backoff := time.Second
|
|
for i := 0; i < s.SOAPRetries; i++ {
|
|
if i > 0 {
|
|
log.Printf("Reintento %d después de %v debido a: %v", i, backoff, lastErr)
|
|
time.Sleep(backoff)
|
|
backoff *= 2
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", s.SOAPEndpoint, bytes.NewBufferString(soapReq))
|
|
if err != nil {
|
|
lastErr = fmt.Errorf("error creando la solicitud: %w", err)
|
|
continue
|
|
}
|
|
|
|
// Cabeceras SOAP comunes
|
|
req.Header.Set("Content-Type", "text/xml; charset=utf-8")
|
|
req.Header.Set("SOAPAction", "\"\"")
|
|
//req.Header.Set("apikey", s.APIKey)
|
|
req.Header.Set(s.TokenKey, s.TokenValue)
|
|
|
|
// Crea un transporte que ignore la configuración de proxy
|
|
transport := &http.Transport{
|
|
Proxy: nil, // Esto desactiva el uso de proxy
|
|
}
|
|
|
|
client := &http.Client{
|
|
Transport: transport,
|
|
Timeout: 30 * time.Second,
|
|
}
|
|
|
|
log.Printf("Enviando solicitud a: %s", s.SOAPEndpoint)
|
|
resp, err = client.Do(req)
|
|
if err != nil {
|
|
lastErr = fmt.Errorf("error enviando la solicitud: %w", err)
|
|
continue
|
|
}
|
|
|
|
// El resto del código sigue igual...
|
|
if resp.StatusCode != http.StatusOK {
|
|
bodyBytes, _ = io.ReadAll(resp.Body)
|
|
resp.Body.Close()
|
|
lastErr = fmt.Errorf("respuesta no válida, código: %d, cuerpo: %s",
|
|
resp.StatusCode, string(bodyBytes))
|
|
continue
|
|
}
|
|
|
|
bodyBytes, err = io.ReadAll(resp.Body)
|
|
resp.Body.Close()
|
|
if err != nil {
|
|
lastErr = fmt.Errorf("error leyendo el cuerpo de la respuesta: %w", err)
|
|
continue
|
|
}
|
|
|
|
return resp, bodyBytes, nil
|
|
}
|
|
|
|
return nil, nil, fmt.Errorf("fallo después de %d reintentos: %v", s.SOAPRetries, lastErr)
|
|
}
|
|
|
|
//func CodigoEnviarSOAPRequest(s obtencionCodigos.CuisServiceModel, soapReq string) (*http.Response, []byte, error) {
|
|
// var resp *http.Response
|
|
// var bodyBytes []byte
|
|
// var lastErr error
|
|
//
|
|
// backoff := time.Second
|
|
// for i := 0; i < s.SOAPRetries; i++ {
|
|
// if i > 0 {
|
|
// log.Printf("Reintento %d después de %v debido a: %v", i, backoff, lastErr)
|
|
// time.Sleep(backoff)
|
|
// backoff *= 2
|
|
// }
|
|
//
|
|
// req, err := http.NewRequest("POST", s.SOAPEndpoint, bytes.NewBufferString(soapReq))
|
|
// if err != nil {
|
|
// lastErr = fmt.Errorf("error creando la solicitud: %w", err)
|
|
// continue
|
|
// }
|
|
//
|
|
// // Cabeceras SOAP comunes
|
|
// req.Header.Set("Content-Type", "text/xml; charset=utf-8")
|
|
// req.Header.Set("SOAPAction", "\"\"") // Agrega SOAPAction si es necesario
|
|
// req.Header.Set("apikey", s.APIKey)
|
|
//
|
|
// // Opcional: imprime la solicitud para depuración
|
|
// log.Printf("Enviando solicitud a: %s", s.SOAPEndpoint)
|
|
//
|
|
// client := &http.Client{Timeout: 30 * time.Second}
|
|
// resp, err = client.Do(req)
|
|
// if err != nil {
|
|
// lastErr = fmt.Errorf("error enviando la solicitud: %w", err)
|
|
// continue
|
|
// }
|
|
//
|
|
// // Verifica el código de estado
|
|
// if resp.StatusCode != http.StatusOK {
|
|
// bodyBytes, _ = io.ReadAll(resp.Body)
|
|
// resp.Body.Close()
|
|
// lastErr = fmt.Errorf("respuesta no válida, código: %d, cuerpo: %s",
|
|
// resp.StatusCode, string(bodyBytes))
|
|
// continue
|
|
// }
|
|
//
|
|
// bodyBytes, err = io.ReadAll(resp.Body)
|
|
// resp.Body.Close() // Siempre cerrar el cuerpo
|
|
// if err != nil {
|
|
// lastErr = fmt.Errorf("error leyendo el cuerpo de la respuesta: %w", err)
|
|
// continue
|
|
// }
|
|
//
|
|
// // Solicitud exitosa
|
|
// return resp, bodyBytes, nil
|
|
// }
|
|
//
|
|
// return nil, nil, fmt.Errorf("fallo después de %d reintentos: %v", s.SOAPRetries, lastErr)
|
|
//}
|
|
|
|
//func CodigoEnviarSOAPRequest(s obtencionCodigos.CuisServiceModel, soapReq string) (*http.Response, []byte, error) {
|
|
// var resp *http.Response
|
|
// var bodyBytes []byte
|
|
// var err error
|
|
//
|
|
// backoff := time.Second
|
|
// for i := 0; i < s.SOAPRetries; i++ {
|
|
// if i > 0 {
|
|
// time.Sleep(backoff)
|
|
// backoff *= 2
|
|
// }
|
|
//
|
|
// req, err := http.NewRequest("POST", s.SOAPEndpoint, bytes.NewBufferString(soapReq))
|
|
// if err != nil {
|
|
// continue
|
|
// }
|
|
//
|
|
// req.Header.Set("Content-Type", "text/xml; charset=utf-8")
|
|
// req.Header.Set("apikey", s.APIKey)
|
|
//
|
|
// client := &http.Client{Timeout: 30 * time.Second}
|
|
// resp, err = client.Do(req)
|
|
// if err != nil || resp.StatusCode != http.StatusOK {
|
|
// continue
|
|
// }
|
|
//
|
|
// bodyBytes, err = io.ReadAll(resp.Body)
|
|
// if err != nil {
|
|
// resp.Body.Close()
|
|
// continue
|
|
// }
|
|
//
|
|
// return resp, bodyBytes, nil
|
|
// }
|
|
//
|
|
// return nil, nil, fmt.Errorf("fallo después de %d reintentos: %v", s.SOAPRetries, err)
|
|
//}
|