go-http-peering/client/option.go

58 lines
949 B
Go

package client
import (
"crypto/rsa"
"net/http"
)
type HTTPClient interface {
Do(*http.Request) (*http.Response, error)
}
type Options struct {
HTTPClient HTTPClient
BaseURL string
PrivateKey *rsa.PrivateKey
ServerToken string
}
type OptionFunc func(*Options)
func WithServerToken(token string) OptionFunc {
return func(opts *Options) {
opts.ServerToken = token
}
}
func WithPrivateKey(pk *rsa.PrivateKey) OptionFunc {
return func(opts *Options) {
opts.PrivateKey = pk
}
}
func WithHTTPClient(client HTTPClient) OptionFunc {
return func(opts *Options) {
opts.HTTPClient = client
}
}
func WithBaseURL(url string) OptionFunc {
return func(opts *Options) {
opts.BaseURL = url
}
}
func defaultOptions() *Options {
return &Options{
HTTPClient: http.DefaultClient,
}
}
func createOptions(funcs ...OptionFunc) *Options {
options := defaultOptions()
for _, fn := range funcs {
fn(options)
}
return options
}