summaryrefslogtreecommitdiffstats
path: root/internal/client/admin.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/client/admin.go')
-rw-r--r--internal/client/admin.go124
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
+}