From 0ab0a6b264d8243f6afe9ad329cea0609689b073 Mon Sep 17 00:00:00 2001 From: Philipp Rosenkranz Date: Mon, 8 Apr 2019 12:07:50 +0200 Subject: [PATCH] mite: added version to user agent string --- main.go | 12 ++++++------ mite/api.go | 22 ++++++++++++---------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index 64a13d8..7ea8780 100644 --- a/main.go +++ b/main.go @@ -20,17 +20,17 @@ var ( ) func main() { - homeDirectory, err := homedir.Dir() - if err != nil { - _, _ = fmt.Fprintln(os.Stderr, err) - } - c := config.NewConfig(configFileName, homeDirectory, configType) - api := mite.NewApi(c.GetApiUrl(), c.GetApiKey()) v := cmd.Version{ Version: version, Commit: commit, Date: date, } + homeDirectory, err := homedir.Dir() + if err != nil { + _, _ = fmt.Fprintln(os.Stderr, err) + } + c := config.NewConfig(configFileName, homeDirectory, configType) + api := mite.NewApi(c.GetApiUrl(), c.GetApiKey(), v.Version) err = cmd.HandleCommands(c, api, v) if err != nil { diff --git a/mite/api.go b/mite/api.go index 316d4e5..62f64c8 100644 --- a/mite/api.go +++ b/mite/api.go @@ -10,7 +10,7 @@ import ( ) const contentType = "application/json" -const userAgent = "mite-go/0.1 (+github.com/leanovate/mite-go)" +const userAgentTemplate = "mite-go/%s (+github.com/leanovate/mite-go)" type AccountApi interface{} @@ -51,13 +51,15 @@ type Api interface { } type api struct { - base string - key string - client *http.Client + base string + key string + userAgent string + client *http.Client } -func NewApi(base string, key string) Api { - return &api{base: base, key: key, client: &http.Client{}} +func NewApi(base string, key string, version string) Api { + ua := fmt.Sprintf(userAgentTemplate, version) + return &api{base: base, key: key, userAgent: ua, client: &http.Client{}} } func (a *api) get(resource string, result interface{}) error { @@ -66,7 +68,7 @@ func (a *api) get(resource string, result interface{}) error { return err } - req.Header.Add("User-Agent", userAgent) + req.Header.Add("User-Agent", a.userAgent) req.Header.Add("X-MiteApiKey", a.key) res, err := a.client.Do(req) @@ -102,7 +104,7 @@ func (a *api) post(resource string, body interface{}, result interface{}) error } req.Header.Add("Content-Type", contentType) - req.Header.Add("User-Agent", userAgent) + req.Header.Add("User-Agent", a.userAgent) req.Header.Add("X-MiteApiKey", a.key) res, err := a.client.Do(req) @@ -134,7 +136,7 @@ func (a *api) patch(resource string, body interface{}, result interface{}) error } req.Header.Add("Content-Type", contentType) - req.Header.Add("User-Agent", userAgent) + req.Header.Add("User-Agent", a.userAgent) req.Header.Add("X-MiteApiKey", a.key) res, err := a.client.Do(req) @@ -160,7 +162,7 @@ func (a *api) delete(resource string, result interface{}) error { return err } - req.Header.Add("User-Agent", userAgent) + req.Header.Add("User-Agent", a.userAgent) req.Header.Add("X-MiteApiKey", a.key) res, err := a.client.Do(req)