-
Notifications
You must be signed in to change notification settings - Fork 0
/
ovoid.go
144 lines (132 loc) · 3.2 KB
/
ovoid.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
package ovoid
import (
"context"
"encoding/json"
"fmt"
"time"
)
type (
//Ovo holds struct token and hold methods
Ovo struct {
AuthToken string `json:"token,omitempty"`
Timeout time.Duration
}
)
//NewClient creates new instance ovo struct inside for authToken
func NewClient(authToken string, timeout time.Duration) (*Ovo, error) {
if authToken == "" {
return nil, fmt.Errorf("authToken required")
}
return &Ovo{AuthToken: authToken, Timeout: timeout}, nil
}
//GetAllBalances gets all balances in account
func (o *Ovo) GetAllBalances(ctx context.Context) (*RespBalance, error) {
req := &request{
Method: "GET",
Path: "v1.0/api/front/",
Authorization: o.AuthToken,
Timeout: o.Timeout,
}
resp, err := post(ctx, req)
if err != nil {
return nil, err
}
var result = new(RespBalance)
err = json.NewDecoder(resp.Body).Decode(result)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return result, nil
}
//GetBudgets gets all budget in account
func (o *Ovo) GetBudgets(ctx context.Context) (*RespBudget, error) {
req := &request{
Method: "GET",
Path: "v1.0/budget/detail",
Authorization: o.AuthToken,
Timeout: o.Timeout,
}
resp, err := post(ctx, req)
if err != nil {
return nil, err
}
var result = new(RespBudget)
err = json.NewDecoder(resp.Body).Decode(result)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return result, nil
}
//GetCountUnreadNotifications get count number of notification that unread
//will return pointer of int because the number can be zero
func (o *Ovo) GetCountUnreadNotifications(ctx context.Context) (*int, error) {
req := &request{
Method: "GET",
Path: "v1.0/notification/status/count/UNREAD",
Authorization: o.AuthToken,
Timeout: o.Timeout,
}
resp, err := post(ctx, req)
if err != nil {
return nil, err
}
var result = map[string]int{}
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return nil, err
}
defer resp.Body.Close()
res := result["Total"]
return &res, nil
}
//GetAllNotifications gets all notifications
func (o *Ovo) GetAllNotifications(ctx context.Context) ([]Notifications, error) {
req := &request{
Method: "GET",
Path: "v1.0/notification/status/all",
Authorization: o.AuthToken,
Timeout: o.Timeout,
}
resp, err := post(ctx, req)
if err != nil {
return nil, err
}
var res map[string]interface{}
var response []Notifications
err = json.NewDecoder(resp.Body).Decode(&res)
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, err := json.Marshal(res["notifications"])
if err != nil {
return nil, err
}
err = json.Unmarshal(b, &response)
if err != nil {
return nil, err
}
return response, nil
}
//GetRefBank Get bank referenceID
func (o *Ovo) GetRefBank(ctx context.Context) (*RefBank, error) {
req := &request{
Method: "GET",
Path: "v1.0/reference/master/ref_bank",
Authorization: o.AuthToken,
Timeout: o.Timeout,
}
resp, err := post(ctx, req)
if err != nil {
return nil, err
}
var result = new(RefBank)
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return result, nil
}