diff options
| author | s <[email protected]> | 2025-11-03 21:17:12 -0500 |
|---|---|---|
| committer | s <[email protected]> | 2025-11-03 21:17:12 -0500 |
| commit | f7fcfa623e670dc533bb378912829c73a3593e63 (patch) | |
| tree | 910119ff7293b407affa9ff34706d627d77a3a04 /internal/client/admin.go | |
| download | dborg-f7fcfa623e670dc533bb378912829c73a3593e63.tar.gz dborg-f7fcfa623e670dc533bb378912829c73a3593e63.zip | |
hi
Diffstat (limited to 'internal/client/admin.go')
| -rw-r--r-- | internal/client/admin.go | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/internal/client/admin.go b/internal/client/admin.go new file mode 100644 index 0000000..a5a9519 --- /dev/null +++ b/internal/client/admin.go @@ -0,0 +1,124 @@ +package client + +import ( + "dborg/internal/models" + "encoding/json" + "fmt" + "net/url" +) + +func (c *Client) ListAccounts() (*models.AdminResponse, error) { + data, err := c.Get("/admin/accounts", nil) + if err != nil { + return nil, err + } + + var response models.AdminResponse + if err := json.Unmarshal(data, &response); err != nil { + return nil, fmt.Errorf("failed to parse admin response: %w", err) + } + + return &response, nil +} + +func (c *Client) CreateAccount(req *models.AccountCreateRequest) (*models.AdminResponse, error) { + data, err := c.Post("/admin/accounts", req) + if err != nil { + return nil, err + } + + var account models.Account + if err := json.Unmarshal(data, &account); err == nil && account.APIKey != "" { + return &models.AdminResponse{ + Success: true, + Account: &account, + }, nil + } + + var response models.AdminResponse + if err := json.Unmarshal(data, &response); err != nil { + return nil, fmt.Errorf("failed to parse admin response: %w", err) + } + + return &response, nil +} + +func (c *Client) DeleteAccount(apiKey string) (*models.AdminResponse, error) { + path := fmt.Sprintf("/admin/accounts/%s", url.PathEscape(apiKey)) + data, err := c.Delete(path) + if err != nil { + return nil, err + } + + var response models.AdminResponse + if err := json.Unmarshal(data, &response); err != nil { + return nil, fmt.Errorf("failed to parse admin response: %w", err) + } + + return &response, nil +} + +func (c *Client) UpdateCredits(apiKey string, credits int) (*models.AdminResponse, error) { + path := fmt.Sprintf("/admin/accounts/%s/credits", url.PathEscape(apiKey)) + req := &models.AddCreditsRequest{ + Credits: credits, + } + + data, err := c.Post(path, req) + if err != nil { + return nil, err + } + + var response models.AdminResponse + if err := json.Unmarshal(data, &response); err != nil { + return nil, fmt.Errorf("failed to parse admin response: %w", err) + } + + return &response, nil +} + +func (c *Client) SetCredits(apiKey string, credits int) (*models.AdminResponse, error) { + path := fmt.Sprintf("/admin/accounts/%s/credits", url.PathEscape(apiKey)) + req := &models.SetCreditsRequest{ + Credits: credits, + } + + data, err := c.Put(path, req) + if err != nil { + return nil, err + } + + var account models.Account + if err := json.Unmarshal(data, &account); err == nil && account.APIKey != "" { + return &models.AdminResponse{ + Success: true, + Account: &account, + }, nil + } + + var response models.AdminResponse + if err := json.Unmarshal(data, &response); err != nil { + return nil, fmt.Errorf("failed to parse admin response: %w", err) + } + + return &response, nil +} + +func (c *Client) ToggleAccount(apiKey string, enable bool) (*models.AdminResponse, error) { + path := fmt.Sprintf("/admin/accounts/%s/disable", url.PathEscape(apiKey)) + req := &models.DisableAccountRequest{ + Disabled: !enable, + } + + data, err := c.Patch(path, req) + if err != nil { + return nil, err + } + + var response models.AdminResponse + if err := json.Unmarshal(data, &response); err != nil { + return nil, fmt.Errorf("failed to parse admin response: %w", err) + } + + return &response, nil +} |
