From a1519ff7685ac987f9d70b1fb49bf777028d49b0 Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Wed, 1 Mar 2023 00:34:35 +0100 Subject: Client + Exports + HTTP: Set a user-agent using the client's version --- internal/http/http.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'internal/http') diff --git a/internal/http/http.go b/internal/http/http.go index 995b36f..766a2db 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -14,6 +14,9 @@ import ( "github.com/go-errors/errors" ) +// UserAgent is the user agent that is used for requests +var UserAgent string + // URLParameters is a type used for the parameters in the URL. type URLParameters map[string]string @@ -188,6 +191,9 @@ func (c *Client) Do(method string, urlStr string, opts *OptionalParams) (http.He return nil, nil, errors.WrapPrefix(err, fmt.Sprintf("failed HTTP request with method %s and url %s", method, urlStr), 0) } + if UserAgent != "" { + req.Header.Add("User-Agent", UserAgent) + } // Make sure the headers contain all the parameters optionalHeaders(req, opts) @@ -239,3 +245,9 @@ func (e *StatusError) Error() string { e.Body, ) } + +// RegisterAgent registers the user agent for client and version +func RegisterAgent(client string, version string) { + UserAgent = fmt.Sprintf("%s/%s %s", client, version, "eduvpn-common/1.0.0") + +} -- cgit v1.2.3