-
Notifications
You must be signed in to change notification settings - Fork 0
/
balance.go
67 lines (50 loc) · 1.14 KB
/
balance.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
package enswitch
import (
"context"
"fmt"
"net/http"
"net/url"
)
type BalanceParams struct {
ID uint32
Amount float64
Currency *string
Transaction *balanceTransactionType
Description *string
}
type BalanceResponse Response
type Balance struct {
client *Client
}
func (b *Balance) Update(ctx context.Context, input BalanceParams) (*BalanceResponse, error) {
qs := balanceQueryParam(input)
req, err := b.client.newRequest(ctx, "customers/balance/add", qs)
if err != nil {
return nil, err
}
var response *BalanceResponse
res, err := b.client.call(req, &response)
if err != nil {
return nil, err
}
if response.Responses[0].Code == http.StatusNotFound {
return nil, ErrNotFound
}
defer res.Body.Close()
return response, nil
}
func balanceQueryParam(b BalanceParams) url.Values {
qs := url.Values{}
qs.Add("id", fmt.Sprintf("%v", b.ID))
qs.Add("amount", fmt.Sprintf("%v", b.Amount))
if b.Currency != nil {
qs.Add("currency", *b.Currency)
}
if b.Transaction != nil {
qs.Add("transaction", string(*b.Transaction))
}
if b.Description != nil {
qs.Add("description", *b.Description)
}
return qs
}