Skip to content

Commit 57bc660

Browse files
authored
Merge pull request #34 from serverscom/add-account-balance
Add account service; fix desc for invoices
2 parents 1ce8cb9 + 1d3805f commit 57bc660

File tree

6 files changed

+91
-1
lines changed

6 files changed

+91
-1
lines changed

pkg/accounts.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package serverscom
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
)
7+
8+
const (
9+
accountBalancePath = "/account/balance"
10+
)
11+
12+
// AccountService is an interface for interfacing with accoount endpoints
13+
// API documentation:
14+
// https://developers.servers.com/api-documentation/v1/#tag/Account
15+
type AccountService interface {
16+
17+
// Generic operations
18+
GetBalance(ctx context.Context) (*AccountBalance, error)
19+
}
20+
21+
// AccountHandler handles operations around account
22+
type AccountHandler struct {
23+
client *Client
24+
}
25+
26+
// GetBalance returns account balance information
27+
// Endpoint: https://developers.servers.com/api-documentation/v1/#tag/Account/operation/GetCurrentAccountBalance
28+
func (h *AccountHandler) GetBalance(ctx context.Context) (*AccountBalance, error) {
29+
url := h.client.baseURL + accountBalancePath
30+
31+
body, err := h.client.buildAndExecRequest(ctx, "GET", url, nil)
32+
33+
if err != nil {
34+
return nil, err
35+
}
36+
37+
balance := new(AccountBalance)
38+
39+
if err := json.Unmarshal(body, &balance); err != nil {
40+
return nil, err
41+
}
42+
43+
return balance, nil
44+
}

pkg/accounts_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package serverscom
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestGetAccountBalance(t *testing.T) {
11+
g := NewGomegaWithT(t)
12+
13+
ts, client := newFakeServer().
14+
WithRequestPath("/account/balance").
15+
WithRequestMethod("GET").
16+
WithResponseBodyStubFile("fixtures/account/get_balance_response.json").
17+
WithResponseCode(200).
18+
Build()
19+
20+
defer ts.Close()
21+
22+
ctx := context.TODO()
23+
24+
balance, err := client.Account.GetBalance(ctx)
25+
26+
g.Expect(err).To(BeNil())
27+
g.Expect(balance).ToNot(BeNil())
28+
g.Expect(balance.Currency).To(Equal("EUR"))
29+
g.Expect(balance.CurrentBalance).To(Equal(float64(123.456)))
30+
g.Expect(balance.NextInvoiceTotalDue).To(Equal(float64(0.0)))
31+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"current_balance": 123.456,
3+
"next_invoice_total_due": 0.0,
4+
"currency": "EUR"
5+
}

pkg/invoices.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type InvoiceService interface {
2222
GetBillingInvoice(ctx context.Context, id string) (*Invoice, error)
2323
}
2424

25-
// InvoiceHandler handles operations around hosts
25+
// InvoiceHandler handles operations around invoices
2626
type InvoiceHandler struct {
2727
client *Client
2828
}

pkg/serverscom.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ type Client struct {
4242

4343
Invoices InvoiceService
4444

45+
Account AccountService
46+
4547
client *resty.Client
4648
}
4749

@@ -101,6 +103,7 @@ func (cli *Client) configureResources() {
101103
cli.CloudBlockStorageVolumes = &CloudBlockStorageVolumesHandler{cli}
102104
cli.KubernetesClusters = &KubernetesClustersHandler{cli}
103105
cli.Invoices = &InvoiceHandler{cli}
106+
cli.Account = &AccountHandler{cli}
104107
}
105108

106109
func (cli *Client) buildURL(path string, values ...interface{}) string {

pkg/types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,3 +1042,10 @@ type RealIPHeader struct {
10421042
Name RealIPHeaderName `json:"name"`
10431043
Networks []string `json:"networks"`
10441044
}
1045+
1046+
// AccountBalance represents account balance info
1047+
type AccountBalance struct {
1048+
CurrentBalance float64 `json:"current_balance"`
1049+
NextInvoiceTotalDue float64 `json:"next_invoice_total_due"`
1050+
Currency string `json:"currency"`
1051+
}

0 commit comments

Comments
 (0)