forked from go-acd/acd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
146 lines (125 loc) · 3.77 KB
/
account.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
package acd
import (
"encoding/json"
"net/http"
"time"
"gopkg.in/acd.v0/internal/constants"
"gopkg.in/acd.v0/internal/log"
)
type (
// AccountInfo represents information about an Amazon Cloud Drive account.
AccountInfo struct {
TermsOfUse string `json:"termsOfUse"`
Status string `json:"status"`
}
// AccountQuota represents information about the account quotas.
AccountQuota struct {
Quota uint64 `json:"quota"`
LastCalculated time.Time `json:"lastCalculated"`
Available uint64 `json:"available"`
}
// AccountUsage represents information about the account usage.
AccountUsage struct {
LastCalculated time.Time `json:"lastCalculated"`
Doc struct {
Billable struct {
Bytes uint64 `json:"bytes"`
Count uint32 `json:"count"`
} `json:"billable"`
Total struct {
Bytes uint64 `json:"bytes"`
Count uint32 `json:"count"`
} `json:"total"`
} `json:"doc"`
Other struct {
Billable struct {
Bytes uint64 `json:"bytes"`
Count uint32 `json:"count"`
} `json:"billable"`
Total struct {
Bytes uint64 `json:"bytes"`
Count uint32 `json:"count"`
} `json:"total"`
} `json:"other"`
Photo struct {
Billable struct {
Bytes uint64 `json:"bytes"`
Count uint32 `json:"count"`
} `json:"billable"`
Total struct {
Bytes uint64 `json:"bytes"`
Count uint32 `json:"count"`
} `json:"total"`
} `json:"photo"`
Video struct {
Billable struct {
Bytes uint64 `json:"bytes"`
Count uint32 `json:"count"`
} `json:"billable"`
Total struct {
Bytes uint64 `json:"bytes"`
Count uint32 `json:"count"`
} `json:"total"`
} `json:"video"`
}
)
// GetAccountInfo returns AccountInfo about the current account.
func (c *Client) GetAccountInfo() (*AccountInfo, error) {
var ai AccountInfo
req, err := http.NewRequest("GET", c.metadataURL+"/account/info", nil)
if err != nil {
log.Errorf("%s: %s", constants.ErrCreatingHTTPRequest, err)
return nil, constants.ErrCreatingHTTPRequest
}
res, err := c.Do(req)
if err != nil {
log.Errorf("%s: %s", constants.ErrDoingHTTPRequest, err)
return nil, constants.ErrDoingHTTPRequest
}
defer res.Body.Close()
if err := json.NewDecoder(res.Body).Decode(&ai); err != nil {
log.Errorf("%s: %s", constants.ErrJSONDecodingResponseBody, err)
return nil, constants.ErrJSONDecodingResponseBody
}
return &ai, nil
}
// GetAccountQuota returns AccountQuota about the current account.
func (c *Client) GetAccountQuota() (*AccountQuota, error) {
var aq AccountQuota
req, err := http.NewRequest("GET", c.metadataURL+"/account/quota", nil)
if err != nil {
log.Errorf("%s: %s", constants.ErrCreatingHTTPRequest, err)
return nil, constants.ErrCreatingHTTPRequest
}
res, err := c.Do(req)
if err != nil {
log.Errorf("%s: %s", constants.ErrDoingHTTPRequest, err)
return nil, constants.ErrDoingHTTPRequest
}
defer res.Body.Close()
if err := json.NewDecoder(res.Body).Decode(&aq); err != nil {
log.Errorf("%s: %s", constants.ErrJSONDecodingResponseBody, err)
return nil, constants.ErrJSONDecodingResponseBody
}
return &aq, nil
}
// GetAccountUsage returns AccountUsage about the current account.
func (c *Client) GetAccountUsage() (*AccountUsage, error) {
var au AccountUsage
req, err := http.NewRequest("GET", c.metadataURL+"/account/usage", nil)
if err != nil {
log.Errorf("%s: %s", constants.ErrCreatingHTTPRequest, err)
return nil, constants.ErrCreatingHTTPRequest
}
res, err := c.Do(req)
if err != nil {
log.Errorf("%s: %s", constants.ErrDoingHTTPRequest, err)
return nil, constants.ErrDoingHTTPRequest
}
defer res.Body.Close()
if err := json.NewDecoder(res.Body).Decode(&au); err != nil {
log.Errorf("%s: %s", constants.ErrJSONDecodingResponseBody, err)
return nil, constants.ErrJSONDecodingResponseBody
}
return &au, nil
}