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