62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
|
package tw
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
defaultUserAgent = "tribalwarshelp/development"
|
||
|
defaultTimeout = 10 * time.Second
|
||
|
)
|
||
|
|
||
|
type EnnoblementsUseInterfaceFunc func(since time.Time) bool
|
||
|
|
||
|
type clientConfig struct {
|
||
|
userAgent string
|
||
|
client *http.Client
|
||
|
ennoblementsUseInterfaceFunc EnnoblementsUseInterfaceFunc
|
||
|
}
|
||
|
|
||
|
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 takes a function that will be called on every Client.GetEnnoblements call
|
||
|
// to determine whether the client should use /interface.php?func=get_conquer_extended or /map/conquer_extended.txt
|
||
|
// The default function checks whether since is after now() - 23h,
|
||
|
// if so, it calls /map/conquer_extended.txt, otherwise /interface.php?func=get_conquer_extended.
|
||
|
func WithEnnoblementsUseInterfaceFunc(f EnnoblementsUseInterfaceFunc) ClientOption {
|
||
|
return func(cfg *clientConfig) {
|
||
|
cfg.ennoblementsUseInterfaceFunc = f
|
||
|
}
|
||
|
}
|