Update hostinger_service.go
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -21,35 +22,44 @@ func NewHostingerClient(token string) *HostingerClient {
|
||||
return &HostingerClient{
|
||||
token: token,
|
||||
httpClient: &http.Client{
|
||||
Timeout: 20 * time.Second,
|
||||
Timeout: 10 * time.Second,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// get realiza una petición GET autenticada y decodifica el cuerpo en dest.
|
||||
func (c *HostingerClient) get(path string, dest interface{}) error {
|
||||
req, err := http.NewRequest(http.MethodGet, hostingerBaseURL+path, nil)
|
||||
// getRaw realiza una petición GET autenticada y devuelve el body crudo.
|
||||
// El contexto permite cancelar la llamada si el caller abandona la petición.
|
||||
func (c *HostingerClient) getRaw(ctx context.Context, path string) ([]byte, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, hostingerBaseURL+path, 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("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("hostinger: ejecutar request: %w", err)
|
||||
return nil, fmt.Errorf("hostinger: ejecutar request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
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 {
|
||||
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 err := json.Unmarshal(body, dest); err != nil {
|
||||
return fmt.Errorf("hostinger: decodificar respuesta: %w", err)
|
||||
@@ -116,33 +126,40 @@ type HostingerHosting struct {
|
||||
|
||||
// GetVPSList obtiene la lista de VPS/VMs.
|
||||
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"`
|
||||
}
|
||||
if err := c.get("/vps/v1/virtual-machines", &result); err != nil {
|
||||
// intenta respuesta array directa
|
||||
var arr []HostingerVPS
|
||||
if err2 := c.get("/vps/v1/virtual-machines", &arr); err2 != nil {
|
||||
return nil, err
|
||||
}
|
||||
return arr, nil
|
||||
if err := json.Unmarshal(body, &wrapped); err == nil && wrapped.Data != nil {
|
||||
return wrapped.Data, 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.
|
||||
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"`
|
||||
}
|
||||
if err := c.get("/domains/v1/portfolio", &result); err != nil {
|
||||
var arr []HostingerDomain
|
||||
if err2 := c.get("/domains/v1/portfolio", &arr); err2 != nil {
|
||||
return nil, err
|
||||
}
|
||||
return arr, nil
|
||||
if err := json.Unmarshal(body, &wrapped); err == nil && wrapped.Data != nil {
|
||||
return wrapped.Data, 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.
|
||||
@@ -166,31 +183,39 @@ func (c *HostingerClient) GetDNSRecords(domain string) ([]HostingerDNSRecord, er
|
||||
|
||||
// GetOrders obtiene las órdenes de facturación.
|
||||
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"`
|
||||
}
|
||||
if err := c.get("/billing/v1/orders", &result); err != nil {
|
||||
var arr []HostingerOrder
|
||||
if err2 := c.get("/billing/v1/orders", &arr); err2 != nil {
|
||||
return nil, err
|
||||
}
|
||||
return arr, nil
|
||||
if err := json.Unmarshal(body, &wrapped); err == nil && wrapped.Data != nil {
|
||||
return wrapped.Data, 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.
|
||||
// Endpoint: GET /hosting/v1/websites
|
||||
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"`
|
||||
}
|
||||
if err := c.get("/hosting/v1/websites", &result); err != nil {
|
||||
var arr []HostingerHosting
|
||||
if err2 := c.get("/hosting/v1/websites", &arr); err2 != nil {
|
||||
return nil, err
|
||||
}
|
||||
return arr, nil
|
||||
if err := json.Unmarshal(body, &wrapped); err == nil && wrapped.Data != nil {
|
||||
return wrapped.Data, 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user