fix: send account-manager token as x-api-key

This commit is contained in:
2026-04-03 19:12:34 +00:00
parent e2177521e0
commit ad0e123666
2 changed files with 16 additions and 2 deletions

View File

@@ -14,6 +14,7 @@ import (
type Client struct { type Client struct {
BaseURL string BaseURL string
Token string Token string
APIKey string
HTTPClient *http.Client HTTPClient *http.Client
} }
@@ -28,6 +29,17 @@ func New(baseURL, token string) *Client {
} }
} }
// NewWithAPIKey creates a Client that authenticates using X-API-Key.
func NewWithAPIKey(baseURL, apiKey string) *Client {
return &Client{
BaseURL: strings.TrimRight(baseURL, "/"),
APIKey: apiKey,
HTTPClient: &http.Client{
Timeout: 30 * time.Second,
},
}
}
// RequestError represents a non-2xx HTTP response. // RequestError represents a non-2xx HTTP response.
type RequestError struct { type RequestError struct {
StatusCode int StatusCode int
@@ -45,7 +57,9 @@ func (c *Client) Do(method, path string, body io.Reader) ([]byte, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot create request: %w", err) return nil, fmt.Errorf("cannot create request: %w", err)
} }
if c.Token != "" { if c.APIKey != "" {
req.Header.Set("X-API-Key", c.APIKey)
} else if c.Token != "" {
req.Header.Set("Authorization", "Bearer "+c.Token) req.Header.Set("Authorization", "Bearer "+c.Token)
} }
if body != nil { if body != nil {

View File

@@ -194,7 +194,7 @@ func RunUserCreate(username, password, email, fullName, accMgrTokenFlag string)
if err != nil { if err != nil {
output.Errorf("config error: %v", err) output.Errorf("config error: %v", err)
} }
c := client.New(cfg.BaseURL, accMgrToken) c := client.NewWithAPIKey(cfg.BaseURL, accMgrToken)
data, err := c.Post("/users", bytes.NewReader(body)) data, err := c.Post("/users", bytes.NewReader(body))
if err != nil { if err != nil {
output.Errorf("failed to create user: %v", err) output.Errorf("failed to create user: %v", err)