Update hostinger_service.go

This commit is contained in:
Lizandro Guarnizo
2026-05-14 18:35:28 -05:00
parent ed42707dff
commit 73f6a4e24a
+66 -41
View File
@@ -1,6 +1,7 @@
package services package services
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
@@ -21,35 +22,44 @@ func NewHostingerClient(token string) *HostingerClient {
return &HostingerClient{ return &HostingerClient{
token: token, token: token,
httpClient: &http.Client{ httpClient: &http.Client{
Timeout: 20 * time.Second, Timeout: 10 * time.Second,
}, },
} }
} }
// get realiza una petición GET autenticada y decodifica el cuerpo en dest. // getRaw realiza una petición GET autenticada y devuelve el body crudo.
func (c *HostingerClient) get(path string, dest interface{}) error { // El contexto permite cancelar la llamada si el caller abandona la petición.
req, err := http.NewRequest(http.MethodGet, hostingerBaseURL+path, nil) func (c *HostingerClient) getRaw(ctx context.Context, path string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, hostingerBaseURL+path, nil)
if err != nil { if err != nil {
return fmt.Errorf("hostinger: crear request: %w", err) return nil, fmt.Errorf("hostinger: crear request: %w", err)
} }
req.Header.Set("Authorization", "Bearer "+c.token) req.Header.Set("Authorization", "Bearer "+c.token)
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
resp, err := c.httpClient.Do(req) resp, err := c.httpClient.Do(req)
if err != nil { if err != nil {
return fmt.Errorf("hostinger: ejecutar request: %w", err) return nil, fmt.Errorf("hostinger: ejecutar request: %w", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return fmt.Errorf("hostinger: leer body: %w", err) return nil, fmt.Errorf("hostinger: leer body: %w", err)
} }
if resp.StatusCode < 200 || resp.StatusCode >= 300 { if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("hostinger: status %d %s", resp.StatusCode, string(body)) return nil, fmt.Errorf("hostinger: status %d %s", resp.StatusCode, string(body))
} }
return body, nil
}
// get realiza una petición GET autenticada y decodifica el cuerpo en dest.
func (c *HostingerClient) get(path string, dest interface{}) error {
body, err := c.getRaw(context.Background(), path)
if err != nil {
return err
}
if dest != nil { if dest != nil {
if err := json.Unmarshal(body, dest); err != nil { if err := json.Unmarshal(body, dest); err != nil {
return fmt.Errorf("hostinger: decodificar respuesta: %w", err) return fmt.Errorf("hostinger: decodificar respuesta: %w", err)
@@ -116,33 +126,40 @@ type HostingerHosting struct {
// GetVPSList obtiene la lista de VPS/VMs. // GetVPSList obtiene la lista de VPS/VMs.
func (c *HostingerClient) GetVPSList() ([]HostingerVPS, error) { func (c *HostingerClient) GetVPSList() ([]HostingerVPS, error) {
var result struct { body, err := c.getRaw(context.Background(), "/vps/v1/virtual-machines")
if err != nil {
return nil, err
}
var wrapped struct {
Data []HostingerVPS `json:"data"` Data []HostingerVPS `json:"data"`
} }
if err := c.get("/vps/v1/virtual-machines", &result); err != nil { if err := json.Unmarshal(body, &wrapped); err == nil && wrapped.Data != nil {
// intenta respuesta array directa return wrapped.Data, nil
var arr []HostingerVPS
if err2 := c.get("/vps/v1/virtual-machines", &arr); err2 != nil {
return nil, err
}
return arr, nil
} }
return result.Data, nil var arr []HostingerVPS
if err := json.Unmarshal(body, &arr); err != nil {
return nil, fmt.Errorf("hostinger: no se pudo interpretar respuesta VPS: %w", err)
}
return arr, nil
} }
// GetDomains obtiene el portafolio de dominios. // GetDomains obtiene el portafolio de dominios.
func (c *HostingerClient) GetDomains() ([]HostingerDomain, error) { func (c *HostingerClient) GetDomains() ([]HostingerDomain, error) {
var result struct { body, err := c.getRaw(context.Background(), "/domains/v1/portfolio")
if err != nil {
return nil, err
}
var wrapped struct {
Data []HostingerDomain `json:"data"` Data []HostingerDomain `json:"data"`
} }
if err := c.get("/domains/v1/portfolio", &result); err != nil { if err := json.Unmarshal(body, &wrapped); err == nil && wrapped.Data != nil {
var arr []HostingerDomain return wrapped.Data, nil
if err2 := c.get("/domains/v1/portfolio", &arr); err2 != nil {
return nil, err
}
return arr, nil
} }
return result.Data, nil var arr []HostingerDomain
if err := json.Unmarshal(body, &arr); err != nil {
return nil, fmt.Errorf("hostinger: no se pudo interpretar respuesta dominios: %w", err)
}
return arr, nil
} }
// hostingerDNSZone es la estructura interna de la respuesta de zona DNS. // hostingerDNSZone es la estructura interna de la respuesta de zona DNS.
@@ -166,31 +183,39 @@ func (c *HostingerClient) GetDNSRecords(domain string) ([]HostingerDNSRecord, er
// GetOrders obtiene las órdenes de facturación. // GetOrders obtiene las órdenes de facturación.
func (c *HostingerClient) GetOrders() ([]HostingerOrder, error) { func (c *HostingerClient) GetOrders() ([]HostingerOrder, error) {
var result struct { body, err := c.getRaw(context.Background(), "/billing/v1/orders")
if err != nil {
return nil, err
}
var wrapped struct {
Data []HostingerOrder `json:"data"` Data []HostingerOrder `json:"data"`
} }
if err := c.get("/billing/v1/orders", &result); err != nil { if err := json.Unmarshal(body, &wrapped); err == nil && wrapped.Data != nil {
var arr []HostingerOrder return wrapped.Data, nil
if err2 := c.get("/billing/v1/orders", &arr); err2 != nil {
return nil, err
}
return arr, nil
} }
return result.Data, nil var arr []HostingerOrder
if err := json.Unmarshal(body, &arr); err != nil {
return nil, fmt.Errorf("hostinger: no se pudo interpretar respuesta orders: %w", err)
}
return arr, nil
} }
// GetHostingAccounts obtiene las cuentas de hosting. // GetHostingAccounts obtiene las cuentas de hosting.
// Endpoint: GET /hosting/v1/websites // Endpoint: GET /hosting/v1/websites
func (c *HostingerClient) GetHostingAccounts() ([]HostingerHosting, error) { func (c *HostingerClient) GetHostingAccounts() ([]HostingerHosting, error) {
var result struct { body, err := c.getRaw(context.Background(), "/hosting/v1/websites")
if err != nil {
return nil, err
}
var wrapped struct {
Data []HostingerHosting `json:"data"` Data []HostingerHosting `json:"data"`
} }
if err := c.get("/hosting/v1/websites", &result); err != nil { if err := json.Unmarshal(body, &wrapped); err == nil && wrapped.Data != nil {
var arr []HostingerHosting return wrapped.Data, nil
if err2 := c.get("/hosting/v1/websites", &arr); err2 != nil {
return nil, err
}
return arr, nil
} }
return result.Data, nil var arr []HostingerHosting
if err := json.Unmarshal(body, &arr); err != nil {
return nil, fmt.Errorf("hostinger: no se pudo interpretar respuesta hosting: %w", err)
}
return arr, nil
} }