-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathequinox.go
129 lines (118 loc) · 3.8 KB
/
equinox.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
package equinox
//go:generate go run ./codegen
import (
"context"
"net/http"
"time"
"github.com/Kyagara/equinox/v2/api"
"github.com/Kyagara/equinox/v2/cache"
"github.com/Kyagara/equinox/v2/clients/lol"
"github.com/Kyagara/equinox/v2/clients/lor"
"github.com/Kyagara/equinox/v2/clients/riot"
"github.com/Kyagara/equinox/v2/clients/tft"
"github.com/Kyagara/equinox/v2/clients/val"
"github.com/Kyagara/equinox/v2/internal"
"github.com/Kyagara/equinox/v2/ratelimit"
"github.com/allegro/bigcache/v3"
"github.com/rs/zerolog"
)
type Equinox struct {
Internal *internal.Client
Cache *cache.Cache
RateLimit *ratelimit.RateLimit
Riot *riot.Client
LOL *lol.Client
TFT *tft.Client
VAL *val.Client
LOR *lor.Client
}
// Returns an equinox client with the default configuration:
//
// - Key : The provided Riot API key.
// - HTTPClient : Default http.Client.
// - Cache : BigCache with a TTL of 4 minutes.
// - RateLimit : InternalRateLimit with a limit usage factor of 0.99 and interval overhead of 1 second.
// - Logger : Logger with zerolog.WarnLevel. Will log if rate limited or when retrying a request.
// - Retry : Will retry a request a maximum of 3 times with a jitter of 500 milliseconds.
func NewClient(key string) (*Equinox, error) {
config := DefaultConfig(key)
cache, err := DefaultCache()
if err != nil {
return nil, err
}
rateLimit := DefaultRateLimit()
return NewCustomClient(config, nil, cache, rateLimit)
}
// Returns an equinox client using a custom configuration.
//
// - Config : The api.EquinoxConfig.
// - HTTPClient : Can be nil.
// - Cache : Can be nil.
// - RateLimit : Can be nil, only disable it if you know what you're doing.
func NewCustomClient(config api.EquinoxConfig, httpClient *http.Client, cache *cache.Cache, rateLimit *ratelimit.RateLimit) (*Equinox, error) {
client, err := internal.NewInternalClient(config, httpClient, cache, rateLimit)
if err != nil {
return nil, err
}
equinox := &Equinox{
Internal: client,
Cache: cache,
RateLimit: rateLimit,
Riot: riot.NewRiotClient(client),
LOL: lol.NewLOLClient(client),
TFT: tft.NewTFTClient(client),
VAL: val.NewVALClient(client),
LOR: lor.NewLORClient(client),
}
return equinox, nil
}
// Returns the default equinox config.
//
// Logger with zerolog.WarnLevel. Retry with a limit of 3 and jitter of 500 milliseconds.
func DefaultConfig(key string) api.EquinoxConfig {
return api.EquinoxConfig{
Key: key,
Retry: DefaultRetry(),
Logger: DefaultLogger(),
}
}
// Returns the default Cache.
//
// BigCache with an eviction time of 4 minutes.
func DefaultCache() (*cache.Cache, error) {
ctx := context.Background()
cacheConfig := bigcache.DefaultConfig(4 * time.Minute)
cacheConfig.Verbose = false
return cache.NewBigCache(ctx, cacheConfig)
}
// Returns the default RateLimit.
//
// - StoreType : InternalRateLimit
// - LimitUsageFactor : 0.99
// - IntervalOverhead : 1 second
func DefaultRateLimit() *ratelimit.RateLimit {
return ratelimit.NewInternalRateLimit(0.99, time.Second)
}
// Returns the default Retry config.
//
// - MaxRetries : 3
// - Jitter : 500 milliseconds
func DefaultRetry() api.Retry {
return api.Retry{MaxRetries: 3, Jitter: 500 * time.Millisecond}
}
// Returns the default Logger config.
//
// - Level : zerolog.WarnLevel
// - Pretty : false
// - TimeFieldFormat : zerolog.TimeFormatUnix
// - EnableTimestamp : true
// - EnableConfigurationLogging : false
func DefaultLogger() api.Logger {
return api.Logger{
Level: zerolog.WarnLevel,
Pretty: false,
TimeFieldFormat: zerolog.TimeFormatUnix,
EnableTimestamp: true,
EnableConfigurationLogging: false,
}
}