Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use built-in User-Agent setting #235

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 12 additions & 24 deletions internal/service/cloud/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const (
DefaultApiUrl = "https://api.tidbcloud.com"
DefaultServerlessEndpoint = "https://serverless.tidbapi.com"
DefaultIAMEndpoint = "https://iam.tidbapi.com"
userAgent = "User-Agent"
)

type TiDBCloudClient interface {
Expand Down Expand Up @@ -481,33 +480,42 @@ func NewApiClient(rt http.RoundTripper, apiUrl string, serverlessEndpoint string
return nil, nil, nil, nil, nil, nil, nil, err
}

userAgent := fmt.Sprintf("%s/%s", config.CliName, version.Version)

iamCfg := iam.NewConfiguration()
iamCfg.HTTPClient = httpclient
iamCfg.Host = iamURL.Host
iamCfg.UserAgent = userAgent

clusterCfg := cluster.NewConfiguration()
clusterCfg.HTTPClient = httpclient
clusterCfg.Host = serverlessURL.Host
clusterCfg.UserAgent = userAgent

branchCfg := branch.NewConfiguration()
branchCfg.HTTPClient = httpclient
branchCfg.Host = serverlessURL.Host
branchCfg.UserAgent = userAgent

exportCfg := export.NewConfiguration()
exportCfg.HTTPClient = httpclient
exportCfg.Host = serverlessURL.Host
exportCfg.UserAgent = userAgent

importCfg := imp.NewConfiguration()
importCfg.HTTPClient = httpclient
importCfg.Host = serverlessURL.Host
importCfg.UserAgent = userAgent

backupRestoreCfg := br.NewConfiguration()
backupRestoreCfg.HTTPClient = httpclient
backupRestoreCfg.Host = serverlessURL.Host
backupRestoreCfg.UserAgent = userAgent

pingchatCfg := pingchat.NewConfiguration()
pingchatCfg.HTTPClient = httpclient
pingchatCfg.Host = u.Host
pingchatCfg.UserAgent = userAgent

return branch.NewAPIClient(branchCfg), cluster.NewAPIClient(clusterCfg),
pingchat.NewAPIClient(pingchatCfg), br.NewAPIClient(backupRestoreCfg),
Expand All @@ -516,35 +524,15 @@ func NewApiClient(rt http.RoundTripper, apiUrl string, serverlessEndpoint string
}

func NewDigestTransport(publicKey, privateKey string) http.RoundTripper {
return NewTransportWithAgent(&digest.Transport{
return &digest.Transport{
Username: publicKey,
Password: privateKey,
Transport: NewDebugTransport(http.DefaultTransport),
}, fmt.Sprintf("%s/%s", config.CliName, version.Version))
}

func NewBearTokenTransport(token string) http.RoundTripper {
return NewTransportWithAgent(NewTransportWithBearToken(NewDebugTransport(http.DefaultTransport), token),
fmt.Sprintf("%s/%s", config.CliName, version.Version))
}

// NewTransportWithAgent returns a new http.RoundTripper that add the User-Agent header,
// according to https://github.com/go-swagger/go-swagger/issues/1563.
func NewTransportWithAgent(inner http.RoundTripper, userAgent string) http.RoundTripper {
return &UserAgentTransport{
inner: inner,
Agent: userAgent,
}
}

type UserAgentTransport struct {
inner http.RoundTripper
Agent string
}

func (ug *UserAgentTransport) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Set(userAgent, ug.Agent)
return ug.inner.RoundTrip(r)
func NewBearTokenTransport(token string) http.RoundTripper {
return NewTransportWithBearToken(NewDebugTransport(http.DefaultTransport), token)
}

func NewTransportWithBearToken(inner http.RoundTripper, token string) http.RoundTripper {
Expand Down
Loading