/
options.go
177 lines (150 loc) · 4.25 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package zapi
import (
"crypto/tls"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
"strings"
opentracing "github.com/opentracing/opentracing-go"
"golang.org/x/oauth2"
)
// UserAgent is the user agent that will be provided by the RESTv1Client. It can
// be overridden by providing a custom transport using the WithTransport Option.
const UserAgent = "go-zapi v1"
// DefaultRestBaseURL is used by the RESTv1Client as the default base URL for all
// zveloAPI calls. It can be overridden using the WithRestBaseURL Option.
const DefaultRestBaseURL = "https://api.zvelo.com/"
// DefaultGrpcTarget is used by the GRPCv1Client as the default
// scheme://authority/endpoint_name for all zveloAPI calls. It can be
// overridden using the WithGrpcTarget Option.
const DefaultGrpcTarget = "dns:///api.zvelo.com"
type options struct {
oauth2.TokenSource
grpcTarget string
restBaseURL *url.URL
debug io.Writer
transport http.RoundTripper
tracerFunc func() opentracing.Tracer
tlsInsecureSkipVerify bool
withoutTLS bool
}
// An Option is used to configure different parts of this package. Not every
// Option is useful with every function that takes Options.
type Option func(*options)
func defaults(ts oauth2.TokenSource) *options {
o := options{
TokenSource: ts,
transport: http.DefaultTransport,
tracerFunc: opentracing.GlobalTracer,
debug: ioutil.Discard,
}
WithRestBaseURL(DefaultRestBaseURL)(&o)
WithGrpcTarget(DefaultGrpcTarget)(&o)
return &o
}
func (o options) tracer() opentracing.Tracer {
if tracer := o.tracerFunc(); tracer != nil {
return tracer
}
return opentracing.NoopTracer{}
}
func (o options) restURL(dir string, elem ...string) string {
u := *o.restBaseURL
parts := []string{u.Path, dir}
parts = append(parts, elem...)
u.Path = path.Join(parts...)
return u.String()
}
// WithTransport returns an Option that will cause all requests from the
// RESTv1Client to be processed by the given http.RoundTripper. If not
// specified, http.DefaultTransport will be used.
func WithTransport(val http.RoundTripper) Option {
if val == nil {
val = http.DefaultTransport
}
return func(o *options) {
o.transport = val
if o.tlsInsecureSkipVerify {
WithTLSInsecureSkipVerify()(o)
}
}
}
// WithTLSInsecureSkipVerify returns an Option that disables certificate chain
// and host name verification of the connection to zveloAPI. This should
// only be used for testing, e.g. with mocks.
func WithTLSInsecureSkipVerify() Option {
return func(o *options) {
o.tlsInsecureSkipVerify = true
if t, ok := o.transport.(*http.Transport); ok {
// #nosec
t.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
}
}
}
// WithoutTLS disables TLS when connecting to zveloAPI
func WithoutTLS() Option {
return func(o *options) {
o.withoutTLS = true
o.restBaseURL.Scheme = "http"
}
}
// WithTracer returns an Option that will cause requests to be instrumented by
// the given tracer. If not specified, opentracing.GlobalTracer will be used.
func WithTracer(val opentracing.Tracer) Option {
return func(o *options) {
if val == nil {
o.tracerFunc = opentracing.GlobalTracer
return
}
o.tracerFunc = func() opentracing.Tracer {
return val
}
}
}
// WithDebug returns an Option that will cause requests from the RESTv1Client
// and callbacks processed by the CallbackHandler to emit debug logs to the
// writer
func WithDebug(val io.Writer) Option {
if val == nil {
val = ioutil.Discard
}
return func(o *options) {
o.debug = val
}
}
// WithRestBaseURL returns an Option that overrides the default base URL for all
// zveloAPI requests
func WithRestBaseURL(val string) Option {
if val == "" {
val = DefaultRestBaseURL
}
return func(o *options) {
if !strings.Contains(val, "://") {
if o.restBaseURL != nil {
val = o.restBaseURL.Scheme + "://" + val
} else {
val = "https://" + val
}
}
if p, err := url.Parse(val); err == nil {
if p.Path == "" {
p.Path = "/"
}
o.restBaseURL = p
}
}
}
// WithGrpcTarget returns an Option that overrides the default gRPC target for
// all zveloAPI requests
func WithGrpcTarget(val string) Option {
if val == "" {
val = DefaultGrpcTarget
}
return func(o *options) {
o.grpcTarget = val
}
}