-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclientopts.go
More file actions
161 lines (147 loc) · 4.58 KB
/
clientopts.go
File metadata and controls
161 lines (147 loc) · 4.58 KB
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
package client
import (
"crypto/tls"
"io"
"net/http"
"net/url"
"strings"
"time"
// Package imports
transport "github.com/mutablelogic/go-client/pkg/transport"
httpresponse "github.com/mutablelogic/go-server/pkg/httpresponse"
trace "go.opentelemetry.io/otel/trace"
)
// OptEndpoint sets the endpoint for all requests.
func OptEndpoint(value string) ClientOpt {
return func(client *Client) error {
if url, err := url.Parse(value); err != nil {
return err
} else if url.Scheme == "" || url.Host == "" {
return httpresponse.ErrBadRequest.Withf("endpoint: %q", value)
} else if url.Scheme != "http" && url.Scheme != "https" {
return httpresponse.ErrBadRequest.Withf("endpoint: %q", value)
} else {
client.endpoint = url
}
return nil
}
}
// OptTimeout sets the timeout on any request. By default, a timeout
// of 30 seconds is used if OptTimeout is not set
func OptTimeout(value time.Duration) ClientOpt {
return func(client *Client) error {
client.Client.Timeout = value
return nil
}
}
// OptUserAgent sets the user agent string on each API request
// It is set to the default if empty string is passed
func OptUserAgent(value string) ClientOpt {
return func(client *Client) error {
value = strings.TrimSpace(value)
if value == "" {
client.ua = DefaultUserAgent
} else {
client.ua = value
}
return nil
}
}
// Deprecated: Use OptTransport with transport.NewLogging instead.
// OptTrace allows you to be the "man in the middle" on any
// requests so you can see traffic move back and forth.
// Setting verbose to true also displays the JSON response
func OptTrace(w io.Writer, verbose bool) ClientOpt {
return func(client *Client) error {
client.Client.Transport = transport.NewLogging(w, client.Client.Transport, verbose)
return nil
}
}
// OptTransport inserts a transport middleware for all requests made by this client.
// Multiple calls stack in order; the first call becomes the outermost layer.
func OptTransport(fn func(http.RoundTripper) http.RoundTripper) ClientOpt {
return func(client *Client) error {
if fn == nil {
return httpresponse.ErrBadRequest.With("OptTransport: nil middleware")
}
client.transports = append(client.transports, fn)
return nil
}
}
// OptStrict turns on strict content type checking on anything returned
// from the API
func OptStrict() ClientOpt {
return func(client *Client) error {
client.strict = true
return nil
}
}
// OptRateLimit sets the limit on number of requests per second
// and the API will sleep when exceeded. For account tokens this is 1 per second
func OptRateLimit(value float32) ClientOpt {
return func(client *Client) error {
if value < 0.0 {
return httpresponse.ErrBadRequest.With("OptRateLimit")
} else {
client.rate = value
return nil
}
}
}
// OptReqToken sets a request token for all client requests. This can be
// overridden by the client for individual requests using OptToken.
func OptReqToken(value Token) ClientOpt {
return func(client *Client) error {
client.setToken(value)
return nil
}
}
// OptTracer sets the OpenTelemetry tracer for this client. It wraps the
// underlying HTTP transport so that every HTTP call — including OAuth token
// refresh and redirect hops — produces a client span. Span names default
// to "METHOD /path" format.
func OptTracer(tracer trace.Tracer) ClientOpt {
return func(client *Client) error {
client.Client.Transport = transport.NewTransport(tracer, client.Client.Transport)
return nil
}
}
// OptSkipVerify skips TLS certificate domain verification.
// It clones the client's own transport rather than mutating http.DefaultTransport.
func OptSkipVerify() ClientOpt {
return func(client *Client) error {
t, ok := client.Client.Transport.(*http.Transport)
if !ok {
return httpresponse.ErrBadRequest.With("OptSkipVerify: transport is not *http.Transport")
}
clone := t.Clone()
clone.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
client.Client.Transport = clone
return nil
}
}
// OptHeader appends a custom header to each request
func OptHeader(key, value string) ClientOpt {
return func(client *Client) error {
if client.headers == nil {
client.headers = make(map[string]string, 2)
}
if key == "" {
return httpresponse.ErrBadRequest.With("OptHeader")
}
client.headers[key] = value
return nil
}
}
// OptParent sets the parent client for this client, which is
// used for setting additional client options in the parent
func OptParent(v any) ClientOpt {
return func(client *Client) error {
if v == nil {
return httpresponse.ErrBadRequest.With("OptParent")
} else {
client.Parent = v
}
return nil
}
}