67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
package http
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/go-resty/resty/v2"
|
|
"github.com/tuusuario/go-sync-service/internal/config"
|
|
"github.com/tuusuario/go-sync-service/internal/domain/dto"
|
|
)
|
|
|
|
func SendRequest(host string, opts dto.ServiceConfig) (*resty.Response, error) {
|
|
client := GetClient()
|
|
req := client.R()
|
|
|
|
// Establecer encabezados
|
|
for k, v := range opts.Headers {
|
|
req.SetHeader(k, v)
|
|
}
|
|
|
|
// Construir la URL completa
|
|
if host == "" {
|
|
return nil, errors.New("el host no puede estar vacío")
|
|
}
|
|
if opts.Path == "" {
|
|
return nil, errors.New("el path no puede estar vacío")
|
|
}
|
|
url := fmt.Sprintf("%s%s", host, opts.Path)
|
|
req.SetHeader("Content-Type", "application/json")
|
|
|
|
// Si es GraphQL
|
|
if opts.GraphQL != nil {
|
|
payload := map[string]interface{}{
|
|
"query": opts.GraphQL.Query,
|
|
"variables": opts.GraphQL.Variables,
|
|
}
|
|
|
|
req.SetBody(payload)
|
|
return req.Post(url)
|
|
}
|
|
|
|
// Si es REST
|
|
if opts.Rest != nil {
|
|
if opts.Rest.Query != nil {
|
|
req.SetQueryParams(opts.Rest.Query)
|
|
}
|
|
if opts.Rest.Body != nil {
|
|
req.SetBody(opts.Rest.Body)
|
|
config.Log.Println("📦 Body:", opts.Rest.Body)
|
|
}
|
|
}
|
|
|
|
// Método HTTP
|
|
switch opts.Method {
|
|
case "GET":
|
|
return req.Get(url)
|
|
case "POST":
|
|
return req.Post(url)
|
|
case "PUT":
|
|
return req.Put(url)
|
|
case "DELETE":
|
|
return req.Delete(url)
|
|
default:
|
|
return nil, fmt.Errorf("método HTTP no soportado: %s", opts.Method)
|
|
}
|
|
}
|