Files
OpenFlare/internal/apps/relay/httpclient/client.go
T
2026-06-19 17:20:39 +08:00

46 lines
1.4 KiB
Go

// Package httpclient provides an HTTP client for relay control-plane communication.
package httpclient
import (
"context"
"time"
edgehttp "github.com/Rain-kl/Wavelet/internal/apps/edge/httpclient"
service "github.com/Rain-kl/Wavelet/pkg/protocol"
)
// APIResponse wraps a control-plane API response with an optional error message.
type APIResponse[T any] struct {
ErrorMsg string `json:"error_msg"`
Data T `json:"data"`
}
// Client sends authenticated requests to the relay control-plane API.
type Client struct {
base *edgehttp.Client
}
// New creates a relay HTTP client with the given base URL, token, and timeout.
func New(baseURL string, token string, timeout time.Duration) *Client {
return &Client{
base: edgehttp.New(baseURL, token, timeout, "X-Agent-Token"),
}
}
// Heartbeat sends a relay heartbeat payload to the control plane.
func (c *Client) Heartbeat(ctx context.Context, payload service.RelayHeartbeatPayload) (*service.RelayHeartbeatResponse, error) {
resp := APIResponse[service.RelayHeartbeatResponse]{}
if err := c.base.PostJSON(ctx, "/api/v1/relay/heartbeat", payload, &resp); err != nil {
return nil, err
}
if err := edgehttp.APIError(resp.ErrorMsg); err != nil {
return nil, err
}
return &resp.Data, nil
}
// SetToken updates the authentication token used for API requests.
func (c *Client) SetToken(token string) {
c.base.SetToken(token)
}