1515package cloud
1616
1717import (
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
3032const (
3133 DefaultApiUrl = "https://api.tidbcloud.com"
34+ userAgent = "User-Agent"
3235)
3336
3437type 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