2022-08-13 21:59:09 -05:00
|
|
|
package client
|
|
|
|
|
2022-09-25 05:54:00 -05:00
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/bufbuild/connect-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
httpClient *http.Client
|
|
|
|
skipVerify bool
|
|
|
|
opts []connect.ClientOption
|
|
|
|
}
|
2022-08-13 21:59:09 -05:00
|
|
|
|
|
|
|
// An Option configures a mutex.
|
|
|
|
type Option interface {
|
2022-09-25 05:54:00 -05:00
|
|
|
apply(*config)
|
2022-08-13 21:59:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// OptionFunc is a function that configure a value.
|
2022-09-25 05:54:00 -05:00
|
|
|
type OptionFunc func(*config)
|
2022-08-13 21:59:09 -05:00
|
|
|
|
|
|
|
// Apply calls f(option)
|
2022-09-25 05:54:00 -05:00
|
|
|
func (f OptionFunc) apply(cfg *config) {
|
|
|
|
f(cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithSkipVerify(c bool) Option {
|
|
|
|
return OptionFunc(func(cfg *config) {
|
|
|
|
cfg.skipVerify = c
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithClientOptions(opts ...connect.ClientOption) Option {
|
|
|
|
return OptionFunc(func(cfg *config) {
|
|
|
|
cfg.opts = append(cfg.opts, opts...)
|
|
|
|
})
|
2022-08-13 21:59:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithGRPC configures clients to use the HTTP/2 gRPC protocol.
|
|
|
|
func WithGRPC(c bool) Option {
|
2022-09-25 05:54:00 -05:00
|
|
|
return OptionFunc(func(cfg *config) {
|
2022-08-13 21:59:09 -05:00
|
|
|
if !c {
|
|
|
|
return
|
|
|
|
}
|
2022-09-25 05:54:00 -05:00
|
|
|
cfg.opts = append(cfg.opts, connect.WithGRPC())
|
2022-08-13 21:59:09 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithGRPCWeb configures clients to use the gRPC-Web protocol.
|
|
|
|
func WithGRPCWeb(c bool) Option {
|
2022-09-25 05:54:00 -05:00
|
|
|
return OptionFunc(func(cfg *config) {
|
2022-08-13 21:59:09 -05:00
|
|
|
if !c {
|
|
|
|
return
|
|
|
|
}
|
2022-09-25 05:54:00 -05:00
|
|
|
cfg.opts = append(cfg.opts, connect.WithGRPCWeb())
|
2022-08-13 21:59:09 -05:00
|
|
|
})
|
|
|
|
}
|