@@ -105,13 +105,19 @@ var loggableMimeTypes = map[string]struct{}{
105
105
"text/html" : {},
106
106
}
107
107
108
+ type ClientOption func (* Client )
109
+
108
110
// New creates a Coder client for the provided URL.
109
- func New (serverURL * url.URL )* Client {
110
- return & Client {
111
+ func New (serverURL * url.URL , opts ... ClientOption )* Client {
112
+ client := & Client {
111
113
URL :serverURL ,
112
114
HTTPClient :& http.Client {},
113
115
SessionTokenProvider :FixedSessionTokenProvider {},
114
116
}
117
+ for _ ,opt := range opts {
118
+ opt (client )
119
+ }
120
+ return client
115
121
}
116
122
117
123
// Client is an HTTP caller for methods to the Coder API.
@@ -129,15 +135,18 @@ type Client struct {
129
135
130
136
// PlainLogger may be set to log HTTP traffic in a human-readable form.
131
137
// It uses the LogBodies option.
138
+ // Deprecated: Use WithPlainLogger to set this.
132
139
PlainLogger io.Writer
133
140
134
141
// Trace can be enabled to propagate tracing spans to the Coder API.
135
142
// This is useful for tracking a request end-to-end.
143
+ // Deprecated: Use WithTrace to set this.
136
144
Trace bool
137
145
138
146
// DisableDirectConnections forces any connections to workspaces to go
139
147
// through DERP, regardless of the BlockEndpoints setting on each
140
148
// connection.
149
+ // Deprecated: Use WithDisableDirectConnections to set this.
141
150
DisableDirectConnections bool
142
151
}
143
152
@@ -149,6 +158,7 @@ func (c *Client) Logger() slog.Logger {
149
158
}
150
159
151
160
// SetLogger sets the logger for the client.
161
+ // Deprecated: Use WithLogger to set this.
152
162
func (c * Client )SetLogger (logger slog.Logger ) {
153
163
c .mu .Lock ()
154
164
defer c .mu .Unlock ()
@@ -163,6 +173,7 @@ func (c *Client) LogBodies() bool {
163
173
}
164
174
165
175
// SetLogBodies sets whether to log request and response bodies.
176
+ // Deprecated: Use WithLogBodies to set this.
166
177
func (c * Client )SetLogBodies (logBodies bool ) {
167
178
c .mu .Lock ()
168
179
defer c .mu .Unlock ()
@@ -177,16 +188,11 @@ func (c *Client) SessionToken() string {
177
188
}
178
189
179
190
// SetSessionToken sets a fixed token for the client.
180
- // Deprecated:Create a new client instead of changing the token after creation.
191
+ // Deprecated:Build a new client using WithSessionToken instead of changing the token after creation.
181
192
func (c * Client )SetSessionToken (token string ) {
182
- c .SetSessionTokenProvider (FixedSessionTokenProvider {SessionToken :token })
183
- }
184
-
185
- // SetSessionTokenProvider sets the session token provider for the client.
186
- func (c * Client )SetSessionTokenProvider (provider SessionTokenProvider ) {
187
193
c .mu .Lock ()
188
194
defer c .mu .Unlock ()
189
- c .SessionTokenProvider = provider
195
+ c .SessionTokenProvider = FixedSessionTokenProvider { SessionToken : token }
190
196
}
191
197
192
198
func prefixLines (prefix ,s []byte ) []byte {
@@ -641,3 +647,47 @@ func (h *HeaderTransport) CloseIdleConnections() {
641
647
tr .CloseIdleConnections ()
642
648
}
643
649
}
650
+
651
+ // ClientOptions
652
+
653
+ func WithSessionToken (token string )ClientOption {
654
+ return func (c * Client ) {
655
+ c .SessionTokenProvider = FixedSessionTokenProvider {SessionToken :token }
656
+ }
657
+ }
658
+
659
+ func WithHTTPClient (httpClient * http.Client )ClientOption {
660
+ return func (c * Client ) {
661
+ c .HTTPClient = httpClient
662
+ }
663
+ }
664
+
665
+ func WithLogger (logger slog.Logger )ClientOption {
666
+ return func (c * Client ) {
667
+ c .logger = logger
668
+ }
669
+ }
670
+
671
+ func WithLogBodies ()ClientOption {
672
+ return func (c * Client ) {
673
+ c .logBodies = true
674
+ }
675
+ }
676
+
677
+ func WithPlainLogger (plainLogger io.Writer )ClientOption {
678
+ return func (c * Client ) {
679
+ c .PlainLogger = plainLogger
680
+ }
681
+ }
682
+
683
+ func WithTrace ()ClientOption {
684
+ return func (c * Client ) {
685
+ c .Trace = true
686
+ }
687
+ }
688
+
689
+ func WithDisableDirectConnections ()ClientOption {
690
+ return func (c * Client ) {
691
+ c .DisableDirectConnections = true
692
+ }
693
+ }