-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return balance values from Balance.Get
- Loading branch information
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters