@@ -21,10 +21,11 @@ import (
21
21
type (
22
22
// Client type is used to perform actions against the Tailscale API.
23
23
Client struct {
24
- apiKey string
25
- http * http.Client
26
- baseURL * url.URL
27
- tailnet string
24
+ apiKey string
25
+ http * http.Client
26
+ baseURL * url.URL
27
+ tailnet string
28
+ userAgent string // empty string means Go's default value.
28
29
}
29
30
30
31
// APIError type describes an error as returned by the Tailscale API.
47
48
const baseURL = "https://api.tailscale.com"
48
49
const contentType = "application/json"
49
50
const defaultHttpClientTimeout = time .Minute
51
+ const defaultUserAgent = "tailscale-client-go"
50
52
51
53
// NewClient returns a new instance of the Client type that will perform operations against a chosen tailnet and will
52
54
// provide the apiKey for authorization. Additional options can be provided, see ClientOption for more details.
@@ -65,8 +67,9 @@ func NewClient(apiKey, tailnet string, options ...ClientOption) (*Client, error)
65
67
}
66
68
67
69
c := & Client {
68
- baseURL : u ,
69
- tailnet : tailnet ,
70
+ baseURL : u ,
71
+ tailnet : tailnet ,
72
+ userAgent : defaultUserAgent ,
70
73
}
71
74
72
75
if apiKey != "" {
@@ -122,6 +125,15 @@ func WithOAuthClientCredentials(clientID, clientSecret string, scopes []string)
122
125
}
123
126
}
124
127
128
+ // WithUserAgent sets a custom User-Agent header in HTTP requests.
129
+ // Passing an empty string will make the client use Go's default value.
130
+ func WithUserAgent (ua string ) ClientOption {
131
+ return func (c * Client ) error {
132
+ c .userAgent = ua
133
+ return nil
134
+ }
135
+ }
136
+
125
137
// TODO: consider setting `headers` and `body` via opts to decrease the number of arguments.
126
138
func (c * Client ) buildRequest (ctx context.Context , method , uri string , headers map [string ]string , body interface {}) (* http.Request , error ) {
127
139
u , err := c .baseURL .Parse (uri )
@@ -142,6 +154,10 @@ func (c *Client) buildRequest(ctx context.Context, method, uri string, headers m
142
154
return nil , err
143
155
}
144
156
157
+ if c .userAgent != "" {
158
+ req .Header .Set ("User-Agent" , c .userAgent )
159
+ }
160
+
145
161
for k , v := range headers {
146
162
req .Header .Set (k , v )
147
163
}
0 commit comments