This repository has been archived on 2024-04-06. You can view files and clone it, but cannot push or open issues or pull requests.
core-old/internal/tw/client_config.go
Dawid Wysokiński 8080b2d6fd
All checks were successful
continuous-integration/drone/push Build is passing
refactor: rename fn -> f, loadJSONFile -> readJSONFile
2023-02-24 07:24:16 +01:00

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(f func(since time.Time) bool) ClientOption {
return func(cfg *clientConfig) {
cfg.ennoblementsUseInterfaceFunc = f
}
}