Skip to content

Commit 08f1287

Browse files
committed
tailscale: allow specifying a custom http.Client
Fixes #79 Signed-off-by: Percy Wegmann <[email protected]>
1 parent 27aa0bf commit 08f1287

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

v2/client.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ type (
3232
// Tailnet allows specifying a specific Tailnet by name, to which this Client will connect by default.
3333
Tailnet string
3434

35-
// http is the http client to use for requests to the API server. If specified, this supercedes the above configuration.
36-
http *http.Client
35+
// HTTP is the [http.Client] to use for requests to the API server.
36+
// If not specified, a new [http.Client] with a Timeout of 1 minute will be used.
37+
// This will be ignored if using [Client.UseOAuth].
38+
HTTP *http.Client
3739

3840
initOnce sync.Once
3941

@@ -96,8 +98,8 @@ func (c *Client) init() {
9698
if c.UserAgent == "" {
9799
c.UserAgent = defaultUserAgent
98100
}
99-
if c.http == nil {
100-
c.http = &http.Client{Timeout: defaultHttpClientTimeout}
101+
if c.HTTP == nil {
102+
c.HTTP = &http.Client{Timeout: defaultHttpClientTimeout}
101103
}
102104
c.contacts = &ContactsResource{c}
103105
c.devicePosture = &DevicePostureResource{c}
@@ -113,6 +115,7 @@ func (c *Client) init() {
113115
}
114116

115117
// UseOAuth configures the client to use the specified OAuth credentials.
118+
// If [Client.HTTP] was previously specified, this replaces it.
116119
func (c *Client) UseOAuth(clientID, clientSecret string, scopes []string) {
117120
oauthConfig := clientcredentials.Config{
118121
ClientID: clientID,
@@ -122,8 +125,8 @@ func (c *Client) UseOAuth(clientID, clientSecret string, scopes []string) {
122125
}
123126

124127
// use context.Background() here, since this is used to refresh the token in the future
125-
c.http = oauthConfig.Client(context.Background())
126-
c.http.Timeout = defaultHttpClientTimeout
128+
c.HTTP = oauthConfig.Client(context.Background())
129+
c.HTTP.Timeout = defaultHttpClientTimeout
127130
}
128131

129132
func (c *Client) Contacts() *ContactsResource {
@@ -275,7 +278,7 @@ func (c *Client) buildRequest(ctx context.Context, method string, uri *url.URL,
275278
}
276279

277280
func (c *Client) do(req *http.Request, out interface{}) error {
278-
res, err := c.http.Do(req)
281+
res, err := c.HTTP.Do(req)
279282
if err != nil {
280283
return err
281284
}

0 commit comments

Comments
 (0)