Skip to content

Add account service; fix desc for invoices #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions pkg/accounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package serverscom

import (
"context"
"encoding/json"
)

const (
accountBalancePath = "/account/balance"
)

// AccountService is an interface for interfacing with accoount endpoints
// API documentation:
// https://developers.servers.com/api-documentation/v1/#tag/Account
type AccountService interface {

// Generic operations
GetBalance(ctx context.Context) (*AccountBalance, error)
}

// AccountHandler handles operations around account
type AccountHandler struct {
client *Client
}

// GetBalance returns account balance information
// Endpoint: https://developers.servers.com/api-documentation/v1/#tag/Account/operation/GetCurrentAccountBalance
func (h *AccountHandler) GetBalance(ctx context.Context) (*AccountBalance, error) {
url := h.client.baseURL + accountBalancePath

body, err := h.client.buildAndExecRequest(ctx, "GET", url, nil)

if err != nil {
return nil, err
}

balance := new(AccountBalance)

if err := json.Unmarshal(body, &balance); err != nil {
return nil, err
}

return balance, nil
}
31 changes: 31 additions & 0 deletions pkg/accounts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package serverscom

import (
"context"
"testing"

. "github.com/onsi/gomega"
)

func TestGetAccountBalance(t *testing.T) {
g := NewGomegaWithT(t)

ts, client := newFakeServer().
WithRequestPath("/account/balance").
WithRequestMethod("GET").
WithResponseBodyStubFile("fixtures/account/get_balance_response.json").
WithResponseCode(200).
Build()

defer ts.Close()

ctx := context.TODO()

balance, err := client.Account.GetBalance(ctx)

g.Expect(err).To(BeNil())
g.Expect(balance).ToNot(BeNil())
g.Expect(balance.Currency).To(Equal("EUR"))
g.Expect(balance.CurrentBalance).To(Equal(float64(123.456)))
g.Expect(balance.NextInvoiceTotalDue).To(Equal(float64(0.0)))
}
5 changes: 5 additions & 0 deletions pkg/fixtures/account/get_balance_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"current_balance": 123.456,
"next_invoice_total_due": 0.0,
"currency": "EUR"
}
2 changes: 1 addition & 1 deletion pkg/invoices.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type InvoiceService interface {
GetBillingInvoice(ctx context.Context, id string) (*Invoice, error)
}

// InvoiceHandler handles operations around hosts
// InvoiceHandler handles operations around invoices
type InvoiceHandler struct {
client *Client
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/serverscom.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type Client struct {

Invoices InvoiceService

Account AccountService

client *resty.Client
}

Expand Down Expand Up @@ -101,6 +103,7 @@ func (cli *Client) configureResources() {
cli.CloudBlockStorageVolumes = &CloudBlockStorageVolumesHandler{cli}
cli.KubernetesClusters = &KubernetesClustersHandler{cli}
cli.Invoices = &InvoiceHandler{cli}
cli.Account = &AccountHandler{cli}
}

func (cli *Client) buildURL(path string, values ...interface{}) string {
Expand Down
7 changes: 7 additions & 0 deletions pkg/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1042,3 +1042,10 @@ type RealIPHeader struct {
Name RealIPHeaderName `json:"name"`
Networks []string `json:"networks"`
}

// AccountBalance represents account balance info
type AccountBalance struct {
CurrentBalance float64 `json:"current_balance"`
NextInvoiceTotalDue float64 `json:"next_invoice_total_due"`
Currency string `json:"currency"`
}
Loading