-
Notifications
You must be signed in to change notification settings - Fork 56
/
billing.go
163 lines (133 loc) · 4.76 KB
/
billing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package govultr
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
// BillingService is the interface to interact with the billing endpoint on the Vultr API
// Link : https://www.vultr.com/api/#tag/billing
type BillingService interface {
ListHistory(ctx context.Context, options *ListOptions) ([]History, *Meta, *http.Response, error)
ListInvoices(ctx context.Context, options *ListOptions) ([]Invoice, *Meta, *http.Response, error)
GetInvoice(ctx context.Context, invoiceID string) (*Invoice, *http.Response, error)
ListInvoiceItems(ctx context.Context, invoiceID int, options *ListOptions) ([]InvoiceItem, *Meta, *http.Response, error)
}
// BillingServiceHandler handles interaction with the billing methods for the Vultr API
type BillingServiceHandler struct {
client *Client
}
// History represents a billing history item on an account
type History struct {
ID int `json:"id"`
Date string `json:"date"`
Type string `json:"type"`
Description string `json:"description"`
Amount float32 `json:"amount"`
Balance float32 `json:"balance"`
}
// Invoice represents an invoice on an account
type Invoice struct {
ID int `json:"id"`
Date string `json:"date"`
Description string `json:"description"`
Amount float32 `json:"amount"`
Balance float32 `json:"balance"`
}
// InvoiceItem represents an item on an accounts invoice
type InvoiceItem struct {
Description string `json:"description"`
Product string `json:"product"`
StartDate string `json:"start_date"`
EndDate string `json:"end_date"`
Units int `json:"units"`
UnitType string `json:"unit_type"`
UnitPrice float32 `json:"unit_price"`
Total float32 `json:"total"`
}
type billingHistoryBase struct {
History []History `json:"billing_history"`
Meta *Meta `json:"meta"`
}
type invoicesBase struct {
Invoice []Invoice `json:"billing_invoices"`
Meta *Meta `json:"meta"`
}
type invoiceBase struct {
Invoice *Invoice `json:"billing_invoice"`
}
type invoiceItemsBase struct {
InvoiceItems []InvoiceItem `json:"invoice_items"`
Meta *Meta `json:"meta"`
}
// ListHistory retrieves a list of all billing history on the current account
func (b *BillingServiceHandler) ListHistory(ctx context.Context, options *ListOptions) ([]History, *Meta, *http.Response, error) { //nolint:dupl,lll
uri := "/v2/billing/history"
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
invoices := new(billingHistoryBase)
resp, err := b.client.DoWithContext(ctx, req, invoices)
if err != nil {
return nil, nil, resp, err
}
return invoices.History, invoices.Meta, resp, nil
}
// ListInvoices retrieves a list of all billing invoices on the current account
func (b *BillingServiceHandler) ListInvoices(ctx context.Context, options *ListOptions) ([]Invoice, *Meta, *http.Response, error) { //nolint:dupl,lll
uri := "/v2/billing/invoices"
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
invoices := new(invoicesBase)
resp, err := b.client.DoWithContext(ctx, req, invoices)
if err != nil {
return nil, nil, resp, err
}
return invoices.Invoice, invoices.Meta, resp, nil
}
// GetInvoice retrieves an invoice that matches the given invoiceID
func (b *BillingServiceHandler) GetInvoice(ctx context.Context, invoiceID string) (*Invoice, *http.Response, error) {
uri := fmt.Sprintf("/v2/billing/invoices/%s", invoiceID)
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
invoice := new(invoiceBase)
resp, err := b.client.DoWithContext(ctx, req, invoice)
if err != nil {
return nil, resp, err
}
return invoice.Invoice, resp, nil
}
// ListInvoiceItems retrieves items in an invoice that matches the given invoiceID
func (b *BillingServiceHandler) ListInvoiceItems(ctx context.Context, invoiceID int, options *ListOptions) ([]InvoiceItem, *Meta, *http.Response, error) { //nolint:dupl,lll
uri := fmt.Sprintf("/v2/billing/invoices/%d/items", invoiceID)
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
invoice := new(invoiceItemsBase)
resp, err := b.client.DoWithContext(ctx, req, invoice)
if err != nil {
return nil, nil, resp, err
}
return invoice.InvoiceItems, invoice.Meta, resp, nil
}