Dawid Wysokiński
17d5068238
All checks were successful
continuous-integration/drone/push Build is passing
59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package tw
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
defaultUserAgent = "tribalwarshelp/development"
|
|
defaultTimeout = 10 * time.Second
|
|
)
|
|
|
|
type clientConfig struct {
|
|
userAgent string
|
|
client *http.Client
|
|
ennoblementsUseInterfaceFunc func(since time.Time) bool
|
|
}
|
|
|
|
type ClientOption func(c *clientConfig)
|
|
|
|
func newClientConfig(opts ...ClientOption) *clientConfig {
|
|
cfg := &clientConfig{
|
|
userAgent: defaultUserAgent,
|
|
client: &http.Client{
|
|
Timeout: defaultTimeout,
|
|
},
|
|
ennoblementsUseInterfaceFunc: func(since time.Time) bool {
|
|
return since.After(time.Now().Add(-23 * time.Hour))
|
|
},
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
opt(cfg)
|
|
}
|
|
|
|
return cfg
|
|
}
|
|
|
|
func WithUserAgent(ua string) ClientOption {
|
|
return func(cfg *clientConfig) {
|
|
cfg.userAgent = ua
|
|
}
|
|
}
|
|
|
|
func WithHTTPClient(hc *http.Client) ClientOption {
|
|
return func(cfg *clientConfig) {
|
|
cfg.client = hc
|
|
}
|
|
}
|
|
|
|
// WithEnnoblementsUseInterfaceFunc runs every time Client.GetEnnoblements is called, and allows conditionally
|
|
// configuring the client to use /interface.php?func=get_conquer_extended instead of /map/conquer_extended.txt.
|
|
// If none is specified, a default function is used that checks if since is after now - 23h.
|
|
func WithEnnoblementsUseInterfaceFunc(fn func(since time.Time) bool) ClientOption {
|
|
return func(cfg *clientConfig) {
|
|
cfg.ennoblementsUseInterfaceFunc = fn
|
|
}
|
|
}
|