mirror of
https://gitlab.com/psuapp/psu.git
synced 2024-08-30 18:12:34 +00:00
Add custom User-Agent to API requests
This commit is contained in:
parent
4c2d66e7b4
commit
cf823f9dcf
@ -21,6 +21,7 @@ type Config struct {
|
|||||||
User string
|
User string
|
||||||
Password string
|
Password string
|
||||||
Token string
|
Token string
|
||||||
|
UserAgent string
|
||||||
DoNotUseToken bool
|
DoNotUseToken bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,6 +72,7 @@ type portainerClientImp struct {
|
|||||||
user string
|
user string
|
||||||
password string
|
password string
|
||||||
token string
|
token string
|
||||||
|
userAgent string
|
||||||
doNotUseToken bool
|
doNotUseToken bool
|
||||||
beforeRequestHooks []func(req *http.Request) (err error)
|
beforeRequestHooks []func(req *http.Request) (err error)
|
||||||
afterResponseHooks []func(resp *http.Response) (err error)
|
afterResponseHooks []func(resp *http.Response) (err error)
|
||||||
@ -115,6 +117,7 @@ func (n *portainerClientImp) do(uri, method string, request io.Reader, requestTy
|
|||||||
|
|
||||||
if request != nil {
|
if request != nil {
|
||||||
req.Header.Set("Content-Type", requestType)
|
req.Header.Set("Content-Type", requestType)
|
||||||
|
req.Header.Set("User-Agent", n.userAgent)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !n.doNotUseToken {
|
if !n.doNotUseToken {
|
||||||
@ -308,5 +311,6 @@ func NewClient(httpClient *http.Client, config Config) PortainerClient {
|
|||||||
user: config.User,
|
user: config.User,
|
||||||
password: config.Password,
|
password: config.Password,
|
||||||
token: config.Token,
|
token: config.Token,
|
||||||
|
userAgent: config.UserAgent,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,6 +44,9 @@ func TestClientAuthenticates(t *testing.T) {
|
|||||||
assert.NotNil(t, req.Header["Content-Type"])
|
assert.NotNil(t, req.Header["Content-Type"])
|
||||||
assert.NotNil(t, req.Header["Content-Type"][0])
|
assert.NotNil(t, req.Header["Content-Type"][0])
|
||||||
assert.Equal(t, req.Header["Content-Type"][0], "application/json")
|
assert.Equal(t, req.Header["Content-Type"][0], "application/json")
|
||||||
|
assert.NotNil(t, req.Header["User-Agent"])
|
||||||
|
assert.NotNil(t, req.Header["User-Agent"][0])
|
||||||
|
assert.Equal(t, req.Header["User-Agent"][0], "GE007")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.NotNil(t, body["Username"])
|
assert.NotNil(t, body["Username"])
|
||||||
assert.Equal(t, body["Username"], "admin")
|
assert.Equal(t, body["Username"], "admin")
|
||||||
@ -59,9 +62,10 @@ func TestClientAuthenticates(t *testing.T) {
|
|||||||
apiUrl, _ := url.Parse(ts.URL + "/api/")
|
apiUrl, _ := url.Parse(ts.URL + "/api/")
|
||||||
|
|
||||||
customClient := NewClient(ts.Client(), Config{
|
customClient := NewClient(ts.Client(), Config{
|
||||||
Url: apiUrl,
|
Url: apiUrl,
|
||||||
User: "admin",
|
User: "admin",
|
||||||
Password: "a",
|
Password: "a",
|
||||||
|
UserAgent: "GE007",
|
||||||
})
|
})
|
||||||
token, err := customClient.Authenticate()
|
token, err := customClient.Authenticate()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
@ -92,6 +92,7 @@ func GetDefaultClientConfig() (config client.Config, err error) {
|
|||||||
User: viper.GetString("user"),
|
User: viper.GetString("user"),
|
||||||
Password: viper.GetString("password"),
|
Password: viper.GetString("password"),
|
||||||
Token: viper.GetString("auth-token"),
|
Token: viper.GetString("auth-token"),
|
||||||
|
UserAgent: BuildUseAgentString(),
|
||||||
DoNotUseToken: false,
|
DoNotUseToken: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,3 +33,15 @@ func BuildVersionString() string {
|
|||||||
|
|
||||||
return fmt.Sprintf("%s %s %s BuildDate: %s", programName, version, osArch, buildDate)
|
return fmt.Sprintf("%s %s %s BuildDate: %s", programName, version, osArch, buildDate)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BuildUseAgentString() string {
|
||||||
|
var theVersion = version
|
||||||
|
if theVersion == "" {
|
||||||
|
theVersion = "SNAPSHOT"
|
||||||
|
}
|
||||||
|
if commitHash != "" {
|
||||||
|
theVersion += "+" + strings.ToUpper(commitHash)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("%s %s (%s/%s)", programName, theVersion, runtime.GOOS, runtime.GOARCH)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user