-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathaccount.go
More file actions
115 lines (97 loc) · 2.95 KB
/
account.go
File metadata and controls
115 lines (97 loc) · 2.95 KB
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
package gobitlaunch
import (
"fmt"
"time"
)
// Account represents a BitLaunch account
type Account struct {
ID string `json:"id"`
Email string `json:"email"`
EmailConfirmed bool `json:"emailConfirmed"`
Created time.Time `json:"created"`
Used int `json:"used"`
Limit int `json:"limit"`
Twofa bool `json:"twofa"`
Balance int `json:"balance"`
CostPerHr int `json:"costPerHr"`
LowBalanceAlertDays int `json:"billingAlert"`
NegativeAllowance int `json:"negativeAllowance"`
}
type usageData struct {
Description string `json:"description"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
Cost int `json:"cost"`
Hours int `json:"hours"`
Amount int `json:"amount"`
Type string `json:"type"`
}
// AccountUsage represents the usage data of a BitLaunch account
type AccountUsage struct {
Server []usageData `json:"serverUsage"`
Backup []usageData `json:"backupUsage"`
Bandwidth []usageData `json:"bandwidthUsage"`
Protection []usageData `json:"protectionUsage"`
TotalUSD int `json:"totalUsd"`
PrevMonth string `json:"prevMonth"`
ThisMonth string `json:"thisMonth"`
NextMonth string `json:"nextMonth"`
}
// AccountHistory represents the usage data of a BitLaunch account
type AccountHistory struct {
History []struct {
ID string `json:"id"`
Time time.Time `json:"time"`
Description string `json:"description"`
} `json:"history"`
Total int `json:"total"`
}
// AccountService manages account API actions
type AccountService struct {
client *Client
}
// Show the account
func (as *AccountService) Show() (*Account, error) {
req, err := as.client.NewRequest("GET", "/user", nil)
if err != nil {
return nil, err
}
account := &Account{}
if err := as.client.DoRequest(req, account); err != nil {
return nil, err
}
return account, nil
}
// Usage shows the account usage
func (as *AccountService) Usage(filter ...string) (*AccountUsage, error) {
var search string
if len(filter) > 1 {
return nil, fmt.Errorf("Too many arguments")
} else if len(filter) == 0 {
search = "latest"
} else {
search = filter[0]
}
req, err := as.client.NewRequest("GET", "/usage?period="+search, nil)
if err != nil {
return nil, err
}
usage := &AccountUsage{}
if err := as.client.DoRequest(req, usage); err != nil {
return nil, err
}
return usage, nil
}
// History shows the account history/activity
func (as *AccountService) History(page, perPage int) (*AccountHistory, error) {
q := fmt.Sprintf("?page=%d&items=%d", page, perPage)
req, err := as.client.NewRequest("GET", "/security/history"+q, nil)
if err != nil {
return nil, err
}
history := &AccountHistory{}
if err := as.client.DoRequest(req, history); err != nil {
return nil, err
}
return history, nil
}