-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlists.go
175 lines (167 loc) · 4.03 KB
/
lists.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package satisgo
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"github.com/buger/jsonparser"
)
//GetRefundFromChargeID returns all charges from the beginning
func (p *Satis) GetRefundFromChargeID(chargeID string) (*[]Refund, error) {
total := make([]Refund, 0, 100)
temp := make([]Refund, 0, 100)
last := ""
stopper := true
for stopper {
q := url.Values{}
q.Set("limit", "100")
q.Set("charge_id", chargeID)
if last != "" {
q.Set("starting_after", last)
}
query := q.Encode()
more, err := p.getList(&temp, p.refundsURL(), query)
if err != nil {
return nil, err
}
total = append(total, temp...)
stopper = more
if len(temp) > 0 {
last = temp[len(temp)-1].ID
}
}
return &total, nil
}
//GetRefundSinceChargeID returns all charges from the beginning
func (p *Satis) GetRefundSinceChargeID(chargeID string) (*[]Refund, error) {
total := make([]Refund, 0, 100)
temp := make([]Refund, 0, 100)
last := "chargeID"
stopper := true
for stopper {
q := url.Values{}
q.Set("limit", "100")
if last != "" {
q.Set("starting_after", last)
}
query := q.Encode()
more, err := p.getList(&temp, p.refundsURL(), query)
if err != nil {
return nil, err
}
total = append(total, temp...)
stopper = more
if len(temp) > 0 {
last = temp[len(temp)-1].ID
}
}
return &total, nil
}
//GetAllRefunds returns all charges from the beginning
func (p *Satis) GetAllRefunds() (*[]Refund, error) {
total := make([]Refund, 0, 100)
temp := make([]Refund, 0, 100)
last := ""
stopper := true
for stopper {
q := url.Values{}
q.Set("limit", "100")
if last != "" {
q.Set("starting_after", last)
}
query := q.Encode()
more, err := p.getList(&temp, p.refundsURL(), query)
if err != nil {
return nil, err
}
total = append(total, temp...)
stopper = more
if len(temp) > 0 {
last = temp[len(temp)-1].ID
}
}
return &total, nil
}
//GetAllUsers returns all charges from the beginning
func (p *Satis) GetAllUsers() (*[]User, error) {
total := make([]User, 0, 100)
temp := make([]User, 0, 100)
last := ""
stopper := true
for stopper {
q := url.Values{}
q.Set("limit", "100")
if last != "" {
q.Set("starting_after", last)
}
query := q.Encode()
more, err := p.getList(&temp, p.usersURL(), query)
if err != nil {
return nil, err
}
total = append(total, temp...)
stopper = more
last = temp[len(temp)-1].ID
}
return &total, nil
}
//GetAllCharges returns all charges from the beginning
func (p *Satis) GetAllCharges() (*[]Charge, error) {
total := make([]Charge, 0, 100)
temp := make([]Charge, 0, 100)
last := new(Charge)
stopper := true
for stopper {
q := url.Values{}
q.Set("limit", "100")
if last.ID != "" {
q.Set("starting_after", last.ID)
}
query := q.Encode()
more, err := p.getList(&temp, p.chargesURL(), query)
if err != nil {
return nil, err
}
total = append(total, temp...)
stopper = more
last.ID = temp[len(temp)-1].ID
}
return &total, nil
}
//getList is used to manage general lists in the satispay API. the bool in the return indicates if there are more where this came from
func (p *Satis) getList(list interface{}, baseURL, query string) (bool, error) {
//maybe some checking into the baseURL and query string can be done but since this is an internal function will leave it be wild nad young
var uri string
if baseURL == "" {
return false, fmt.Errorf("no baseURL provided")
}
if query == "" {
uri = baseURL
} else {
uri = baseURL + "?" + query
}
r, err := http.NewRequest("GET", uri, nil)
if err != nil {
return false, err
}
status, b, err := p.makeCall(r)
if err != nil {
return false, fmt.Errorf("Error making the call to API: %s", err.Error())
}
if status != 200 {
return false, fmt.Errorf("Return status is %d:not compatible with the success case", status)
}
data, _, _, err := jsonparser.Get(b, "list")
if err != nil {
return false, err
}
err = json.Unmarshal(data, list)
if err != nil {
return false, err
}
hasMore, err := jsonparser.GetBoolean(b, "has_more")
if err != nil {
return false, err
}
return hasMore, nil
}