fix: set the client user agent
The code already existed for setting the UserAgent header, but a value was never passed to the client. This change will now set a string matching the following format ``` faas-cli/<build version> ``` For example: ``` faas-cli/0.14.6-5-ga9c2b10f ``` Signed-off-by: Lucas Roesler <roesler.lucas@gmail.com>
This commit is contained in:
committed by
Alex Ellis
parent
81ea36ae7d
commit
29953f1836
@@ -11,9 +11,11 @@ import (
|
|||||||
gopath "path"
|
gopath "path"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/openfaas/faas-cli/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
//Client an API client to perform all operations
|
// Client an API client to perform all operations
|
||||||
type Client struct {
|
type Client struct {
|
||||||
httpClient *http.Client
|
httpClient *http.Client
|
||||||
//ClientAuth a type implementing ClientAuth interface for client authentication
|
//ClientAuth a type implementing ClientAuth interface for client authentication
|
||||||
@@ -24,13 +26,13 @@ type Client struct {
|
|||||||
UserAgent string
|
UserAgent string
|
||||||
}
|
}
|
||||||
|
|
||||||
//ClientAuth an interface for client authentication.
|
// ClientAuth an interface for client authentication.
|
||||||
// to add authentication to the client implement this interface
|
// to add authentication to the client implement this interface
|
||||||
type ClientAuth interface {
|
type ClientAuth interface {
|
||||||
Set(req *http.Request) error
|
Set(req *http.Request) error
|
||||||
}
|
}
|
||||||
|
|
||||||
//NewClient initializes a new API client
|
// NewClient initializes a new API client
|
||||||
func NewClient(auth ClientAuth, gatewayURL string, transport http.RoundTripper, timeout *time.Duration) (*Client, error) {
|
func NewClient(auth ClientAuth, gatewayURL string, transport http.RoundTripper, timeout *time.Duration) (*Client, error) {
|
||||||
gatewayURL = strings.TrimRight(gatewayURL, "/")
|
gatewayURL = strings.TrimRight(gatewayURL, "/")
|
||||||
baseURL, err := url.Parse(gatewayURL)
|
baseURL, err := url.Parse(gatewayURL)
|
||||||
@@ -51,10 +53,11 @@ func NewClient(auth ClientAuth, gatewayURL string, transport http.RoundTripper,
|
|||||||
ClientAuth: auth,
|
ClientAuth: auth,
|
||||||
httpClient: client,
|
httpClient: client,
|
||||||
GatewayURL: baseURL,
|
GatewayURL: baseURL,
|
||||||
|
UserAgent: fmt.Sprintf("faas-cli/%s", version.BuildVersion()),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//newRequest create a new HTTP request with authentication
|
// newRequest create a new HTTP request with authentication
|
||||||
func (c *Client) newRequest(method, path string, query url.Values, body io.Reader) (*http.Request, error) {
|
func (c *Client) newRequest(method, path string, query url.Values, body io.Reader) (*http.Request, error) {
|
||||||
|
|
||||||
// deep copy gateway url and then add the supplied path and args to the copy so that
|
// deep copy gateway url and then add the supplied path and args to the copy so that
|
||||||
@@ -85,7 +88,7 @@ func (c *Client) newRequest(method, path string, query url.Values, body io.Reade
|
|||||||
return req, err
|
return req, err
|
||||||
}
|
}
|
||||||
|
|
||||||
//doRequest perform an HTTP request with context
|
// doRequest perform an HTTP request with context
|
||||||
func (c *Client) doRequest(ctx context.Context, req *http.Request) (*http.Response, error) {
|
func (c *Client) doRequest(ctx context.Context, req *http.Request) (*http.Response, error) {
|
||||||
req = req.WithContext(ctx)
|
req = req.WithContext(ctx)
|
||||||
|
|
||||||
@@ -123,7 +126,7 @@ func addQueryParams(u string, params map[string]string) (string, error) {
|
|||||||
return parsedURL.String(), nil
|
return parsedURL.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//AddCheckRedirect add CheckRedirect to the client
|
// AddCheckRedirect add CheckRedirect to the client
|
||||||
func (c *Client) AddCheckRedirect(checkRedirect func(*http.Request, []*http.Request) error) {
|
func (c *Client) AddCheckRedirect(checkRedirect func(*http.Request, []*http.Request) error) {
|
||||||
c.httpClient.CheckRedirect = checkRedirect
|
c.httpClient.CheckRedirect = checkRedirect
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package version
|
package version
|
||||||
|
|
||||||
const UserAgent = "OpenFaaS CLI"
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Version, GitCommit string
|
Version, GitCommit string
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user