-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclient.go
388 lines (335 loc) · 11.4 KB
/
client.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package minercraft
import (
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"math/big"
"net"
"net/http"
"strings"
"time"
"github.com/gojektech/heimdall/v6"
"github.com/gojektech/heimdall/v6/httpclient"
)
// HTTPInterface is used for the http client (mocking heimdall)
type HTTPInterface interface {
Do(req *http.Request) (*http.Response, error)
}
// Client is the parent struct that contains the miner clients and list of miners to use
type Client struct {
apiType APIType // The API type to use
httpClient HTTPInterface // Interface for all HTTP requests
miners []*Miner // List of loaded miners
minerAPIs []*MinerAPIs // List of loaded miners APIs
Options *ClientOptions // Client options config
}
// AddMiner will add a new miner to the list of miners
func (c *Client) AddMiner(miner Miner, apis []API) error {
// Check if miner name is empty
if len(miner.Name) == 0 {
return errors.New("missing miner name")
}
// Check if apis is empty or nil
if len(apis) == 0 || apis == nil {
return errors.New("at least one API must be provided")
}
// Check if a miner with that name already exists
existingMiner := c.MinerByName(miner.Name)
if existingMiner != nil {
return fmt.Errorf("miner %s already exists", miner.Name)
}
// Check if a miner with the minerID already exists
if len(miner.MinerID) > 0 {
if existingMiner = c.MinerByID(miner.MinerID); existingMiner != nil {
return fmt.Errorf("miner %s already exists", miner.MinerID)
}
}
// Check if the MinerAPIs already exist for the given MinerID
existingMinerAPIs := c.MinerAPIsByMinerID(miner.MinerID)
if existingMinerAPIs != nil {
return fmt.Errorf("miner APIs for MinerID %s already exist", miner.MinerID)
}
// Check if the API types are valid
for _, api := range apis {
if !isValidAPIType(api.Type) {
return fmt.Errorf("invalid API type: %s", api.Type)
}
}
// Check if the API types are unique within the provided APIs
apiTypes := make(map[APIType]bool)
for _, api := range apis {
if apiTypes[api.Type] {
return fmt.Errorf("duplicate API type found: %s", api.Type)
}
apiTypes[api.Type] = true
}
// Check if the MinerID is unique or generate a new one
if len(miner.MinerID) == 0 || !c.isUniqueMinerID(miner.MinerID) {
miner.MinerID = generateUniqueMinerID()
}
// Append the new miner
c.miners = append(c.miners, &miner)
// Append the new miner APIs
c.minerAPIs = append(c.minerAPIs, &MinerAPIs{
MinerID: miner.MinerID,
APIs: apis,
})
return nil
}
// RemoveMiner will remove a miner from the list
func (c *Client) RemoveMiner(miner *Miner) bool {
for i, m := range c.miners {
if m.Name == miner.Name || m.MinerID == miner.MinerID {
c.miners[i] = c.miners[len(c.miners)-1]
c.miners = c.miners[:len(c.miners)-1]
return true
}
}
// Miner not found
return false
}
// MinerByName will return a miner given a name
func (c *Client) MinerByName(name string) *Miner {
return MinerByName(c.miners, name)
}
// MinerByName will return a miner from a given set of miners
func MinerByName(miners []*Miner, minerName string) *Miner {
for index, miner := range miners {
if strings.EqualFold(minerName, miner.Name) {
return miners[index]
}
}
return nil
}
// MinerByID will return a miner given a miner id
func (c *Client) MinerByID(minerID string) *Miner {
return MinerByID(c.miners, minerID)
}
// MinerByID will return a miner from a given set of miners
func MinerByID(miners []*Miner, minerID string) *Miner {
for index, miner := range miners {
if strings.EqualFold(minerID, miner.MinerID) {
return miners[index]
}
}
return nil
}
// MinerAPIByMinerID will return a miner's API given a miner id and API type
func (c *Client) MinerAPIByMinerID(minerID string, apiType APIType) (*API, error) {
for _, minerAPI := range c.minerAPIs {
if minerAPI.MinerID == minerID {
for i := range minerAPI.APIs {
if minerAPI.APIs[i].Type == apiType {
return &minerAPI.APIs[i], nil
}
}
}
}
return nil, &APINotFoundError{MinerID: minerID, APIType: apiType}
}
// MinerAPIsByMinerID will return a miner's APIs given a miner id
func (c *Client) MinerAPIsByMinerID(minerID string) *MinerAPIs {
for _, minerAPIs := range c.minerAPIs {
if minerAPIs.MinerID == minerID {
return minerAPIs
}
}
return nil
}
// ActionRouteByAPIType will return the route for a given action and API type
func ActionRouteByAPIType(actionName APIActionName, apiType APIType) (string, error) {
for _, apiRoute := range Routes {
if apiRoute.Name == actionName {
for _, route := range apiRoute.Routes {
if route.APIType == apiType {
return route.Route, nil
}
}
}
}
return "", &ActionRouteNotFoundError{ActionName: actionName, APIType: apiType}
}
// MinerUpdateToken will find a miner by name and update the token
func (c *Client) MinerUpdateToken(name, token string, apiType APIType) {
if miner := c.MinerByName(name); miner != nil {
api, _ := c.MinerAPIByMinerID(miner.MinerID, apiType)
api.Token = token
}
}
// Miners will return the list of miners
func (c *Client) Miners() []*Miner {
return c.miners
}
// UserAgent will return the user agent
func (c *Client) UserAgent() string {
return c.Options.UserAgent
}
// ClientOptions holds all the configuration for connection, dialer and transport
type ClientOptions struct {
BackOffExponentFactor float64 `json:"back_off_exponent_factor"`
BackOffInitialTimeout time.Duration `json:"back_off_initial_timeout"`
BackOffMaximumJitterInterval time.Duration `json:"back_off_maximum_jitter_interval"`
BackOffMaxTimeout time.Duration `json:"back_off_max_timeout"`
DialerKeepAlive time.Duration `json:"dialer_keep_alive"`
DialerTimeout time.Duration `json:"dialer_timeout"`
RequestRetryCount int `json:"request_retry_count"`
RequestTimeout time.Duration `json:"request_timeout"`
TransportExpectContinueTimeout time.Duration `json:"transport_expect_continue_timeout"`
TransportIdleTimeout time.Duration `json:"transport_idle_timeout"`
TransportMaxIdleConnections int `json:"transport_max_idle_connections"`
TransportTLSHandshakeTimeout time.Duration `json:"transport_tls_handshake_timeout"`
UserAgent string `json:"user_agent"`
}
// DefaultClientOptions will return a ClientOptions struct with the default settings.
// Useful for starting with the default and then modifying as needed
func DefaultClientOptions() (clientOptions *ClientOptions) {
return &ClientOptions{
BackOffExponentFactor: 2.0,
BackOffInitialTimeout: 2 * time.Millisecond,
BackOffMaximumJitterInterval: 2 * time.Millisecond,
BackOffMaxTimeout: 10 * time.Millisecond,
DialerKeepAlive: 20 * time.Second,
DialerTimeout: 5 * time.Second,
RequestRetryCount: 2,
RequestTimeout: 30 * time.Second,
TransportExpectContinueTimeout: 3 * time.Second,
TransportIdleTimeout: 20 * time.Second,
TransportMaxIdleConnections: 10,
TransportTLSHandshakeTimeout: 5 * time.Second,
UserAgent: defaultUserAgent,
}
}
// NewClient creates a new client for requests
//
// clientOptions: inject custom client options on load
// customHTTPClient: use your own custom HTTP client
// customMiners: use your own custom list of miners
func NewClient(clientOptions *ClientOptions, customHTTPClient HTTPInterface,
apiType APIType, customMiners []*Miner, customMinersAPIDef []*MinerAPIs) (client ClientInterface, err error) {
// Create the new client
return createClient(clientOptions, apiType, customHTTPClient, customMiners, customMinersAPIDef)
}
// createClient will make a new http client based on the options provided
func createClient(options *ClientOptions, apiType APIType, customHTTPClient HTTPInterface,
customMiners []*Miner, customMinersAPIDef []*MinerAPIs) (c *Client, err error) {
// Create a client
c = new(Client)
// For now set MerchantAPI as the default if not set
if apiType == "" {
apiType = MAPI
}
// Set the client API type
c.apiType = apiType
// Set options (either default or user modified)
if options == nil {
options = DefaultClientOptions()
}
// Set the options
c.Options = options
// Load custom vs pre-defined
if len(customMiners) > 0 && len(customMinersAPIDef) > 0 {
c.miners = customMiners
c.minerAPIs = customMinersAPIDef
} else {
c.miners, err = DefaultMiners()
if err != nil {
return nil, err
}
c.minerAPIs, err = DefaultMinersAPIs()
if err != nil {
return nil, err
}
}
// Is there a custom HTTP client to use?
if customHTTPClient != nil {
c.httpClient = customHTTPClient
return
}
// dial is the net dialer for clientDefaultTransport
dial := &net.Dialer{KeepAlive: options.DialerKeepAlive, Timeout: options.DialerTimeout}
// clientDefaultTransport is the default transport struct for the HTTP client
clientDefaultTransport := &http.Transport{
DialContext: dial.DialContext,
ExpectContinueTimeout: options.TransportExpectContinueTimeout,
IdleConnTimeout: options.TransportIdleTimeout,
MaxIdleConns: options.TransportMaxIdleConnections,
Proxy: http.ProxyFromEnvironment,
TLSHandshakeTimeout: options.TransportTLSHandshakeTimeout,
}
// Determine the strategy for the http client
if options.RequestRetryCount <= 0 {
// no retry enabled
c.httpClient = httpclient.NewClient(
httpclient.WithHTTPTimeout(options.RequestTimeout),
httpclient.WithHTTPClient(&http.Client{
Transport: clientDefaultTransport,
Timeout: options.RequestTimeout,
}),
)
return
}
// Retry enabled - create exponential back-off
c.httpClient = httpclient.NewClient(
httpclient.WithHTTPTimeout(options.RequestTimeout),
httpclient.WithRetrier(heimdall.NewRetrier(
heimdall.NewExponentialBackoff(
options.BackOffInitialTimeout,
options.BackOffMaxTimeout,
options.BackOffExponentFactor,
options.BackOffMaximumJitterInterval,
))),
httpclient.WithRetryCount(options.RequestRetryCount),
httpclient.WithHTTPClient(&http.Client{
Transport: clientDefaultTransport,
Timeout: options.RequestTimeout,
}),
)
return
}
// DefaultMiners will parse the config JSON and return a list of miners
func DefaultMiners() (miners []*Miner, err error) {
err = json.Unmarshal([]byte(KnownMiners), &miners)
return
}
// DefaultMinersAPIs will parse the config JSON and return a list of miner APIs
func DefaultMinersAPIs() (minerAPIs []*MinerAPIs, err error) {
err = json.Unmarshal([]byte(KnownMinersAPIs), &minerAPIs)
return
}
// APIType will return the API type
func (c *Client) APIType() APIType {
return c.apiType
}
// isValidAPIType will return true if the API type is valid and part of our predefined list
func isValidAPIType(apiType APIType) bool {
switch apiType {
case MAPI, Arc:
return true
default:
return false
}
}
// isUniqueMinerID will return true if the miner ID is unique
func (c *Client) isUniqueMinerID(minerID string) bool {
for _, miner := range c.miners {
if miner.MinerID == minerID {
return false
}
}
return true
}
// generateUniqueMinerID will generate a unique miner ID
func generateUniqueMinerID() string {
const idLength = 8
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
id := make([]byte, idLength)
for i := range id {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
if err != nil {
return ""
}
id[i] = letters[num.Int64()]
}
return string(id)
}