forked from fastly/go-fastly
/
billing.go
83 lines (74 loc) · 2.67 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
package fastly
import (
"fmt"
"time"
)
// Billing is the top-level representation of a billing response from the Fastly
// API.
type Billing struct {
EndTime *time.Time `mapstructure:"end_time"`
InvoiceID *string `mapstructure:"invoice_id"`
StartTime *time.Time `mapstructure:"start_time"`
Status *BillingStatus `mapstructure:"status"`
Total *BillingTotal `mapstructure:"total"`
}
// BillingStatus is a representation of the status of the bill from the Fastly
// API.
type BillingStatus struct {
InvoiceID *string `mapstructure:"invoice_id"`
SentAt *time.Time `mapstructure:"sent_at"`
Status *string `mapstructure:"status"`
}
// BillingTotal is a representation of the status of the usage for this bill
// from the Fastly API.
type BillingTotal struct {
Bandwidth *float64 `mapstructure:"bandwidth"`
BandwidthCost *float64 `mapstructure:"bandwidth_cost"`
Cost *float64 `mapstructure:"cost"`
CostBeforeDiscount *float64 `mapstructure:"cost_before_discount"`
Discount *float64 `mapstructure:"discount"`
Extras []*BillingExtra `mapstructure:"extras"`
ExtrasCost *float64 `mapstructure:"extras_cost"`
IncurredCost *float64 `mapstructure:"incurred_cost"`
Overage *float64 `mapstructure:"overage"`
PlanCode *string `mapstructure:"plan_code"`
PlanMinimum *string `mapstructure:"plan_minimum"`
PlanName *string `mapstructure:"plan_name"`
Requests *uint64 `mapstructure:"requests"`
RequestsCost *float64 `mapstructure:"requests_cost"`
Terms *string `mapstructure:"terms"`
}
// BillingExtra is a representation of extras (such as SSL addons) from the
// Fastly API.
type BillingExtra struct {
Name *string `mapstructure:"name"`
Recurring *float64 `mapstructure:"recurring"`
Setup *float64 `mapstructure:"setup"`
}
// GetBillingInput is used as input to the GetBilling function.
type GetBillingInput struct {
// Month is a 2-digit month (required).
Month uint8
// Year is a 4-digit year (required).
Year uint16
}
// GetBilling returns the billing information for the current account.
func (c *Client) GetBilling(i *GetBillingInput) (*Billing, error) {
if i.Year == 0 {
return nil, ErrMissingYear
}
if i.Month == 0 {
return nil, ErrMissingMonth
}
path := fmt.Sprintf("/billing/year/%d/month/%02d", i.Year, i.Month)
resp, err := c.Get(path, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var b *Billing
if err := decodeBodyMap(resp.Body, &b); err != nil {
return nil, err
}
return b, nil
}