-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
209 lines (180 loc) · 7.64 KB
/
Copy pathoptions.go
File metadata and controls
209 lines (180 loc) · 7.64 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright (c) 2026 dexpace and Omar Aljarrah.
// Licensed under the MIT License. See LICENSE in the repository root for details.
package dexpace
import (
"log/slog"
"github.com/dexpace/go-sdk/auth"
cfgpkg "github.com/dexpace/go-sdk/config"
"github.com/dexpace/go-sdk/idempotency"
"github.com/dexpace/go-sdk/instrumentation"
"github.com/dexpace/go-sdk/pipeline"
"github.com/dexpace/go-sdk/retry"
)
// Option configures a [Client] built by [New]. Options are applied in the order
// given; later options override earlier ones for the same setting.
type Option func(*config)
type apiKeyConfig struct {
header string
key string
set bool
}
type config struct {
transport pipeline.Transporter
retry *retry.Options
credential auth.TokenCredential
scopes []string
tokenCache auth.TokenCache
basicAuth *auth.BasicCredential
apiKey apiKeyConfig
digestAuth *auth.BasicCredential
logger *slog.Logger
logging bool
date bool
userAgent string
custom []pipeline.Policy
noIdempotency bool
idempotency *idempotency.Options
before []pipeline.Placement
after []pipeline.Placement
errorsEnabled bool
tracer instrumentation.Tracer
meter instrumentation.Meter
redactAllow []string
cfgSource *cfgpkg.Config
}
// WithTransport sets the terminal transport. When unset, the default net/http
// transport ([github.com/dexpace/go-sdk/transport.New]) is used. Useful for
// tests (a stub transporter) or to share a tuned *http.Client.
func WithTransport(t pipeline.Transporter) Option {
return func(c *config) { c.transport = t }
}
// WithRetry configures the retry policy. When unset, a default retry policy is
// still installed; pass retry.Options{MaxRetries: -1} to disable retries.
func WithRetry(opts retry.Options) Option {
return func(c *config) { c.retry = &opts }
}
// WithCredential installs a bearer-token auth policy using cred and the given
// scopes. Without it, no Authorization header is added.
func WithCredential(cred auth.TokenCredential, scopes ...string) Option {
return func(c *config) {
c.credential = cred
c.scopes = scopes
}
}
// WithTokenCache shares cache across the bearer-token policy installed by
// WithCredential, so multiple clients can reuse cached tokens. A nil cache or no
// credential means the default per-client in-memory cache is used.
func WithTokenCache(cache auth.TokenCache) Option {
return func(c *config) { c.tokenCache = cache }
}
// WithBasicAuth authenticates requests with HTTP Basic auth. Like all credential
// policies it requires HTTPS. If multiple auth options are set, the precedence is
// WithCredential, then WithBasicAuth, then WithAPIKey.
func WithBasicAuth(username, password string) Option {
return func(c *config) {
c.basicAuth = &auth.BasicCredential{Username: username, Password: password}
}
}
// WithAPIKey authenticates requests by setting header to key (HTTPS-only). See
// WithBasicAuth for the precedence when multiple auth options are set.
func WithAPIKey(header, key string) Option {
return func(c *config) {
c.apiKey = apiKeyConfig{header: header, key: key, set: true}
}
}
// WithDigestAuth authenticates requests with HTTP Digest Access Authentication
// (RFC 7616). Like all credential policies it requires HTTPS. Precedence when
// multiple auth options are set: WithCredential, WithBasicAuth, WithAPIKey, then
// WithDigestAuth.
func WithDigestAuth(username, password string) Option {
return func(c *config) {
c.digestAuth = &auth.BasicCredential{Username: username, Password: password}
}
}
// WithLogging enables structured request/response logging via log/slog. A nil
// logger uses slog.Default().
func WithLogging(logger *slog.Logger) Option {
return func(c *config) {
c.logging = true
c.logger = logger
}
}
// WithoutIdempotency disables the default idempotency-key policy.
func WithoutIdempotency() Option {
return func(c *config) { c.noIdempotency = true }
}
// WithIdempotency configures the idempotency-key policy (which is on by
// default). Passing custom options also re-enables it if a prior
// WithoutIdempotency was set. Pass the zero idempotency.Options for the default
// behaviour (POST only, the Idempotency-Key header, and UUIDv4 keys).
func WithIdempotency(opts idempotency.Options) Option {
return func(c *config) {
c.idempotency = &opts
c.noIdempotency = false
}
}
// WithPolicyBefore inserts a custom policy immediately before (outside) the given
// stage, so it wraps that stage and everything inner to it. The stage need not
// be occupied by a built-in policy. Multiple insertions at the same position run
// in the order added.
func WithPolicyBefore(stage pipeline.Stage, p pipeline.Policy) Option {
return func(c *config) { c.before = append(c.before, pipeline.Before(stage, p)) }
}
// WithPolicyAfter inserts a custom policy immediately after (inside) the given
// stage, so the named stage wraps it. The stage need not be occupied by a
// built-in policy. Multiple insertions at the same position run in the order
// added.
func WithPolicyAfter(stage pipeline.Stage, p pipeline.Policy) Option {
return func(c *config) { c.after = append(c.after, pipeline.After(stage, p)) }
}
// WithDate stamps a Date header (RFC 1123) on each request that lacks one. Off
// by default; net/http does not set a request Date and most REST APIs do not
// need it, but some request-signing schemes require it.
func WithDate() Option {
return func(c *config) { c.date = true }
}
// WithErrors enables the typed error model. With it, Client.Do returns a
// *httperr.ResponseError for a non-2xx response and a *httperr.TransportError
// for a request that never produced a response (context cancellation/deadline
// errors are returned unchanged). Off by default: without it, Do mirrors
// http.Client.Do — a non-2xx status is not an error, and transport failures
// surface as raw net/http errors.
func WithErrors() Option {
return func(c *config) { c.errorsEnabled = true }
}
// WithTracing installs a tracing policy that records a span around each request
// attempt using tracer, injecting a W3C traceparent header. A nil tracer is
// ignored (no policy installed). Off by default.
func WithTracing(tracer instrumentation.Tracer) Option {
return func(c *config) { c.tracer = tracer }
}
// WithMetrics installs a metrics policy that records request duration and
// in-flight count using meter. A nil meter is ignored (no policy installed). Off
// by default.
func WithMetrics(meter instrumentation.Meter) Option {
return func(c *config) { c.meter = meter }
}
// WithConfig supplies client defaults from cfg for any setting the caller did not
// set explicitly: the User-Agent (DEXPACE_USER_AGENT), retry count and base delay
// (DEXPACE_MAX_RETRIES, DEXPACE_RETRY_BASE_DELAY), and the default-transport
// timeout (DEXPACE_HTTP_TIMEOUT, applied only when no transport is supplied via
// WithTransport). Explicit options always win, regardless of option order. A nil
// cfg is a no-op.
func WithConfig(cfg *cfgpkg.Config) Option {
return func(c *config) { c.cfgSource = cfg }
}
// WithRedactionAllowlist preserves the values of the named query parameters in
// redacted URLs (logs and traces); all other query values are redacted. Applies
// to the logging and tracing policies.
func WithRedactionAllowlist(params ...string) Option {
return func(c *config) { c.redactAllow = params }
}
// WithUserAgent overrides the default User-Agent ("dexpace-go-sdk/<version>").
func WithUserAgent(ua string) Option {
return func(c *config) { c.userAgent = ua }
}
// WithPolicies appends custom policies after the built-in stack and before the
// transport, in the order given.
func WithPolicies(policies ...pipeline.Policy) Option {
return func(c *config) { c.custom = append(c.custom, policies...) }
}