Skip to content

Commit 7114bd7

Browse files
authored
Add user agent http header (#54)
- Use `RoundTripper` to add `User-Agent` header to HTTP request. - Fix version message when the version is dev.
1 parent 8a73027 commit 7114bd7

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

internal/cli/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func Execute(ctx context.Context, ver, commit, buildDate string) {
5353
if apiUrl == "" {
5454
apiUrl = cloud.DefaultApiUrl
5555
}
56-
delegate, err := cloud.NewClientDelegate(publicKey, privateKey, apiUrl)
56+
delegate, err := cloud.NewClientDelegate(publicKey, privateKey, apiUrl, ver)
5757
if err != nil {
5858
return nil, err
5959
}

internal/cli/version/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func VersionCmd(h *internal.Helper, ver, commit, buildDate string) *cobra.Comman
3939

4040
// Format formats a version string with the given information.
4141
func Format(ver, commit, buildDate string) string {
42-
if ver == "" && buildDate == "" && commit == "" {
42+
if ver == config.DevVersion && buildDate == "" && commit == "" {
4343
return fmt.Sprintf("%s version (built from source)", config.CliName)
4444
}
4545

internal/service/cloud/api_client.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
package cloud
1616

1717
import (
18+
"fmt"
1819
"net/http"
1920

21+
"tidbcloud-cli/internal/config"
2022
"tidbcloud-cli/internal/prop"
2123

2224
apiClient "github.com/c4pt0r/go-tidbcloud-sdk-v1/client"
@@ -29,6 +31,7 @@ import (
2931

3032
const (
3133
DefaultApiUrl = "https://api.tidbcloud.com"
34+
userAgent = "User-Agent"
3235
)
3336

3437
type TiDBCloudClient interface {
@@ -49,8 +52,8 @@ type ClientDelegate struct {
4952
c *apiClient.GoTidbcloud
5053
}
5154

52-
func NewClientDelegate(publicKey string, privateKey string, apiUrl string) (*ClientDelegate, error) {
53-
client, err := NewApiClient(publicKey, privateKey, apiUrl)
55+
func NewClientDelegate(publicKey string, privateKey string, apiUrl string, ver string) (*ClientDelegate, error) {
56+
client, err := NewApiClient(publicKey, privateKey, apiUrl, ver)
5457
if err != nil {
5558
return nil, err
5659
}
@@ -83,12 +86,12 @@ func (d *ClientDelegate) ListProjects(params *project.ListProjectsParams, opts .
8386
return d.c.Project.ListProjects(params, opts...)
8487
}
8588

86-
func NewApiClient(publicKey string, privateKey string, apiUrl string) (*apiClient.GoTidbcloud, error) {
89+
func NewApiClient(publicKey string, privateKey string, apiUrl string, ver string) (*apiClient.GoTidbcloud, error) {
8790
httpclient := &http.Client{
88-
Transport: &digest.Transport{
91+
Transport: NewTransportWithAgent(&digest.Transport{
8992
Username: publicKey,
9093
Password: privateKey,
91-
},
94+
}, fmt.Sprintf("%s/%s", config.CliName, ver)),
9295
}
9396

9497
// Parse the URL
@@ -97,5 +100,25 @@ func NewApiClient(publicKey string, privateKey string, apiUrl string) (*apiClien
97100
return nil, err
98101
}
99102

100-
return apiClient.New(httpTransport.NewWithClient(u.Host, u.Path, []string{u.Scheme}, httpclient), strfmt.Default), nil
103+
transport := httpTransport.NewWithClient(u.Host, u.Path, []string{u.Scheme}, httpclient)
104+
return apiClient.New(transport, strfmt.Default), nil
105+
}
106+
107+
// NewTransportWithAgent returns a new http.RoundTripper that add the User-Agent header,
108+
// according to https://github.com/go-swagger/go-swagger/issues/1563.
109+
func NewTransportWithAgent(inner http.RoundTripper, userAgent string) http.RoundTripper {
110+
return &UserAgentTransport{
111+
inner: inner,
112+
Agent: userAgent,
113+
}
114+
}
115+
116+
type UserAgentTransport struct {
117+
inner http.RoundTripper
118+
Agent string
119+
}
120+
121+
func (ug *UserAgentTransport) RoundTrip(r *http.Request) (*http.Response, error) {
122+
r.Header.Set(userAgent, ug.Agent)
123+
return ug.inner.RoundTrip(r)
101124
}

0 commit comments

Comments
 (0)