-
Notifications
You must be signed in to change notification settings - Fork 26
/
account.go
55 lines (43 loc) · 1.06 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
package nexmo
import (
"encoding/json"
"io/ioutil"
"net/http"
)
// Account represents the user's account. Used when retrieving e.g current
// balance.
type Account struct {
client *Client
}
// GetBalance retrieves the current balance of your Nexmo account in Euros (€)
func (nexmo *Account) GetBalance() (float64, error) {
// Declare this locally, since we are only going to return a float64.
type AccountBalance struct {
Value float64 `json:"value"`
}
var accBalance *AccountBalance
r, reqErr := http.NewRequest("GET", apiRoot+"/account/get-balance/"+
nexmo.client.apiKey+"/"+nexmo.client.apiSecret, nil)
if reqErr != nil {
return 0.0, reqErr
}
r.Header.Add("Accept", "application/json")
resp, err := nexmo.client.HTTPClient.Do(r)
if err != nil {
return 0.0, err
}
defer func() {
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
}()
body, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
return 0.0, readErr
}
err = json.Unmarshal(body, &accBalance)
if err != nil {
return 0.0, err
}
return accBalance.Value, nil
}