summaryrefslogtreecommitdiff
path: root/client/gitlabnet.go
diff options
context:
space:
mode:
authorZeger-Jan van de Weg <git@zjvandeweg.nl>2020-10-14 16:56:49 +0200
committerZeger-Jan van de Weg <git@zjvandeweg.nl>2020-10-14 17:12:43 +0200
commit3f03127314bd768efd0bef57915320545afcdd78 (patch)
tree6bf635e8343edfed9d847726db27259dcb5f0a96 /client/gitlabnet.go
parentd5fdca30a0e5e7bec6f229aaff966a92ba369331 (diff)
downloadgitlab-shell-zj-override-user-agent.tar.gz
client: Allow User-Agent header to be overriddenzj-override-user-agent
The user agent for requests to the internal API endpoints used the default Go provided user agent. This change updates that to always set something else, by default `GitLab-Shell`. Than for others importing the package, there's a new API to set it to something else. This has been done with new method, a setter, to maintain backwards compatibility in the API.
Diffstat (limited to 'client/gitlabnet.go')
-rw-r--r--client/gitlabnet.go16
1 files changed, 14 insertions, 2 deletions
diff --git a/client/gitlabnet.go b/client/gitlabnet.go
index b908d04..fcefb24 100644
--- a/client/gitlabnet.go
+++ b/client/gitlabnet.go
@@ -19,6 +19,7 @@ import (
const (
internalApiPath = "/api/v4/internal"
secretHeaderName = "Gitlab-Shared-Secret"
+ defaultUserAgent = "GitLab-Shell"
)
type ErrorResponse struct {
@@ -26,8 +27,11 @@ type ErrorResponse struct {
}
type GitlabNetClient struct {
- httpClient *HttpClient
- user, password, secret string
+ httpClient *HttpClient
+ user string
+ password string
+ secret string
+ userAgent string
}
func NewGitlabNetClient(
@@ -46,9 +50,16 @@ func NewGitlabNetClient(
user: user,
password: password,
secret: secret,
+ userAgent: defaultUserAgent,
}, nil
}
+// SetUserAgent overrides the default user agent for the User-Agent header field
+// for subsequent requests for the GitlabNetClient
+func (c *GitlabNetClient) SetUserAgent(ua string) {
+ c.userAgent = ua
+}
+
func normalizePath(path string) string {
if !strings.HasPrefix(path, "/") {
path = "/" + path
@@ -119,6 +130,7 @@ func (c *GitlabNetClient) DoRequest(ctx context.Context, method, path string, da
request.Header.Set(secretHeaderName, encodedSecret)
request.Header.Add("Content-Type", "application/json")
+ request.Header.Add("User-Agent", c.userAgent)
request.Close = true
start := time.Now()