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
|
||||
Password string
|
||||
Token string
|
||||
UserAgent string
|
||||
DoNotUseToken bool
|
||||
}
|
||||
|
||||
@ -71,6 +72,7 @@ type portainerClientImp struct {
|
||||
user string
|
||||
password string
|
||||
token string
|
||||
userAgent string
|
||||
doNotUseToken bool
|
||||
beforeRequestHooks []func(req *http.Request) (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 {
|
||||
req.Header.Set("Content-Type", requestType)
|
||||
req.Header.Set("User-Agent", n.userAgent)
|
||||
}
|
||||
|
||||
if !n.doNotUseToken {
|
||||
@ -308,5 +311,6 @@ func NewClient(httpClient *http.Client, config Config) PortainerClient {
|
||||
user: config.User,
|
||||
password: config.Password,
|
||||
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"][0])
|
||||
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.NotNil(t, body["Username"])
|
||||
assert.Equal(t, body["Username"], "admin")
|
||||
@ -59,9 +62,10 @@ func TestClientAuthenticates(t *testing.T) {
|
||||
apiUrl, _ := url.Parse(ts.URL + "/api/")
|
||||
|
||||
customClient := NewClient(ts.Client(), Config{
|
||||
Url: apiUrl,
|
||||
User: "admin",
|
||||
Password: "a",
|
||||
Url: apiUrl,
|
||||
User: "admin",
|
||||
Password: "a",
|
||||
UserAgent: "GE007",
|
||||
})
|
||||
token, err := customClient.Authenticate()
|
||||
assert.Nil(t, err)
|
||||
|
@ -92,6 +92,7 @@ func GetDefaultClientConfig() (config client.Config, err error) {
|
||||
User: viper.GetString("user"),
|
||||
Password: viper.GetString("password"),
|
||||
Token: viper.GetString("auth-token"),
|
||||
UserAgent: BuildUseAgentString(),
|
||||
DoNotUseToken: false,
|
||||
}
|
||||
|
||||
|
@ -33,3 +33,15 @@ func BuildVersionString() string {
|
||||
|
||||
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…
Reference in New Issue
Block a user