-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathigcclient.go
263 lines (223 loc) · 6.54 KB
/
igcclient.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package igcclient
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/moonwalker/logger"
igcerr "github.com/moonwalker/igcclient/errors"
"github.com/moonwalker/igcclient/models"
)
const (
timeout = 30 * time.Second
DefaultLogMaxResponseSize = 10000
)
type IGCClient struct {
HTTPClient *http.Client
baseURL string
common service
Authentication *AuthenticationService
Banks *BanksService
Bonuses *BonusesService
Consent *ConsentService
Countries *CountriesService
Currencies *CurrenciesService
Devices *DevicesService
Games *GamesService
IPWhois *IPWhoisService
KYC *KYCService
Languages *LanguagesService
Payments *PaymentsService
RealityCheck *RealityCheckService
ResponsibleGaming *ResponsibleGamingService
Roles *RolesService
SecurityQuestions *SecurityQuestionsService
User *UserService
Validation *ValidationService
Wallet *WalletService
logRequestBody bool
logResponseData bool
logRequestBlacklist map[string]bool
logResponseBlacklist map[string]bool
logBlacklist map[string]bool
logMaxResponseSize int64
debug bool
invalidAuthCallback *func(string)
}
type service struct {
client *IGCClient
}
type Config struct {
BaseURL string
LogRequestBody bool
LogResponseData bool
LogRequestBlacklist []string
LogResponseBlacklist []string
LogBlacklist []string
LogMaxResponseSize int64
Debug bool
InvalidAuthCallback *func(string)
}
func NewIGCClient(cfg Config) (client *IGCClient, err error) {
if cfg.BaseURL == "" {
err = errors.New("base url can not be empty")
return
}
client = &IGCClient{
HTTPClient: &http.Client{
Timeout: timeout,
},
baseURL: cfg.BaseURL,
logRequestBody: cfg.LogRequestBody,
logResponseData: cfg.LogResponseData,
logRequestBlacklist: getLogBlacklist(cfg.LogRequestBlacklist),
logResponseBlacklist: getLogBlacklist(cfg.LogResponseBlacklist),
logBlacklist: getLogBlacklist(cfg.LogBlacklist),
debug: cfg.Debug,
invalidAuthCallback: cfg.InvalidAuthCallback,
logMaxResponseSize: cfg.LogMaxResponseSize,
}
if client.logMaxResponseSize == 0 {
client.logMaxResponseSize = DefaultLogMaxResponseSize
}
client.common.client = client
client.Authentication = (*AuthenticationService)(&client.common)
client.Banks = (*BanksService)(&client.common)
client.Bonuses = (*BonusesService)(&client.common)
client.Countries = (*CountriesService)(&client.common)
client.Consent = (*ConsentService)(&client.common)
client.Currencies = (*CurrenciesService)(&client.common)
client.Devices = (*DevicesService)(&client.common)
client.Games = (*GamesService)(&client.common)
client.KYC = (*KYCService)(&client.common)
client.IPWhois = (*IPWhoisService)(&client.common)
client.Languages = (*LanguagesService)(&client.common)
client.Payments = (*PaymentsService)(&client.common)
client.RealityCheck = (*RealityCheckService)(&client.common)
client.ResponsibleGaming = (*ResponsibleGamingService)(&client.common)
client.Roles = (*RolesService)(&client.common)
client.SecurityQuestions = (*SecurityQuestionsService)(&client.common)
client.User = (*UserService)(&client.common)
client.Validation = (*ValidationService)(&client.common)
client.Wallet = (*WalletService)(&client.common)
return
}
func getLogBlacklist(blacklist []string) map[string]bool {
bl := make(map[string]bool)
for _, blacklisted := range blacklist {
bl[strings.ToLower(blacklisted)] = true
}
return bl
}
func (c IGCClient) apiReq(method, endpoint string, params *url.Values, body interface{}, data interface{}, headers *map[string]string, log logger.Logger) error {
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(body)
logInfo := make(map[string]interface{})
ep := strings.ToLower(endpoint)
if c.logRequestBody && body != nil && !c.logRequestBlacklist[ep] {
logInfo["request"] = body
}
logInfo["method"] = method
req, err := http.NewRequest(method, c.baseURL+endpoint, b)
if err != nil {
return err
}
if headers != nil {
for k, v := range *headers {
if k == "X-Api-Key" {
if v != "" {
req.Header.Add(k, v)
logInfo[k] = c.obfuscate(v)
}
} else {
if v != "" {
req.Header.Add(k, v)
logInfo[k] = v
}
}
}
}
if b != nil {
req.Header.Add("Content-Type", "application/json")
}
req.Header.Add("Accept", "application/json")
query := endpoint[1:] //don't log the first '/'
logInfo["query"] = query
if params != nil {
pe := params.Encode()
req.URL.RawQuery = pe
logInfo["params"] = pe
}
startTime := time.Now()
resp, e := c.HTTPClient.Do(req)
if e != nil {
return e
}
logInfo["duration"] = time.Since(startTime).Milliseconds()
defer resp.Body.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
s := buf.Bytes()
ls := int64(len(s))
if headers != nil && (*headers)["AuthenticationToken"] != "" {
authToken := (*headers)["AuthenticationToken"]
c.checkForAuthError(s, authToken)
}
err = json.Unmarshal(s, data)
if c.logResponseData && !c.logResponseBlacklist[ep] {
if ls < c.logMaxResponseSize {
logInfo["response"] = data
} else {
logInfo["response"] = string(s)[:c.logMaxResponseSize]
}
}
if log != nil && !c.logBlacklist[endpoint] {
if c.debug {
log.Info(query+" request", logInfo)
} else {
log.Debug(query+" request", logInfo)
}
}
if err != nil && log != nil {
logFields := make(map[string]interface{})
logFields["error"] = err
var response []byte
response, err = base64.StdEncoding.DecodeString(string(s))
if err != nil {
response = s
}
if ls < c.logMaxResponseSize {
logFields["response"] = response
} else {
logFields["response"] = response[:c.logMaxResponseSize]
}
log.Error(fmt.Sprintf("failed to parse response from igc endpoint %s", query), logFields)
}
return err
}
func (c IGCClient) checkForAuthError(data []byte, authToken string) {
if c.invalidAuthCallback != nil {
d := &models.OperationResponse{}
if d.Errors != nil {
for _, e := range *d.Errors {
if e.ErrorCodeID != nil && *e.ErrorCodeID == igcerr.INVALID_AUTHENTICATION_TOKEN {
// User is not logged in
(*c.invalidAuthCallback)(authToken)
}
}
}
}
}
func (c IGCClient) obfuscate(val string) string {
if val == "" || len(val) < 3 {
return val
}
first3 := val[0:3]
last3 := val[len(val)-3:]
return fmt.Sprintf("%sxxxx%s", first3, last3)
}