Skip to content

Commit

Permalink
Return balance values from Balance.Get
Browse files Browse the repository at this point in the history
  • Loading branch information
rbutler committed Dec 11, 2019
1 parent 2a444d2 commit 6c2309d
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
52 changes: 52 additions & 0 deletions balance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package godo

import (
"context"
"net/http"
"time"
)

// BalanceService is an interface for interfacing with the Balance
// endpoints of the DigitalOcean API
// See: https://developers.digitalocean.com/documentation/v2/#balance
type BalanceService interface {
Get(context.Context) (*Balance, *Response, error)
}

// BalanceServiceOp handles communication with the Balance related methods of
// the DigitalOcean API.
type BalanceServiceOp struct {
client *Client
}

var _ BalanceService = &BalanceServiceOp{}

// Balance represents a DigitalOcean Balance
type Balance struct {
MonthToDateBalance string `json:"month_to_date_balance"`
AccountBalance string `json:"account_balance"`
MonthToDateUsage string `json:"month_to_date_usage"`
GeneratedAt time.Time `json:"generated_at"`
}

func (r Balance) String() string {
return Stringify(r)
}

// Get DigitalOcean balance info
func (s *BalanceServiceOp) Get(ctx context.Context) (*Balance, *Response, error) {
path := "v2/customers/my/balance"

req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

root := new(Balance)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}

return root, resp, err
}
44 changes: 44 additions & 0 deletions balance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package godo

import (
"fmt"
"net/http"
"reflect"
"testing"
"time"
)

func TestBalanceGet(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/v2/customers/my/balance", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)

response := `
{
"month_to_date_balance": "23.44",
"account_balance": "12.23",
"month_to_date_usage": "11.21",
"generated_at": "2018-06-21T08:44:38Z"
}
`

fmt.Fprint(w, response)
})

bal, _, err := client.Balance.Get(ctx)
if err != nil {
t.Errorf("Balance.Get returned error: %v", err)
}

expected := &Balance{
MonthToDateBalance: "23.44",
AccountBalance: "12.23",
MonthToDateUsage: "11.21",
GeneratedAt: time.Date(2018, 6, 21, 8, 44, 38, 0, time.UTC),
}
if !reflect.DeepEqual(bal, expected) {
t.Errorf("Balance.Get returned %+v, expected %+v", bal, expected)
}
}
2 changes: 2 additions & 0 deletions godo.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Client struct {
// Services used for communicating with the API
Account AccountService
Actions ActionsService
Balance BalanceService
CDNs CDNService
Domains DomainsService
Droplets DropletsService
Expand Down Expand Up @@ -165,6 +166,7 @@ func NewClient(httpClient *http.Client) *Client {
c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: userAgent}
c.Account = &AccountServiceOp{client: c}
c.Actions = &ActionsServiceOp{client: c}
c.Balance = &BalanceServiceOp{client: c}
c.CDNs = &CDNServiceOp{client: c}
c.Certificates = &CertificatesServiceOp{client: c}
c.Domains = &DomainsServiceOp{client: c}
Expand Down
1 change: 1 addition & 0 deletions godo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func testClientServices(t *testing.T, c *Client) {
services := []string{
"Account",
"Actions",
"Balance",
"CDNs",
"Domains",
"Droplets",
Expand Down

0 comments on commit 6c2309d

Please sign in to comment.