Skip to content

Commit

Permalink
mite: added version to user agent string
Browse files Browse the repository at this point in the history
  • Loading branch information
phiros committed Apr 8, 2019
1 parent ece452b commit 0ab0a6b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 12 additions & 10 deletions mite/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 0ab0a6b

Please sign in to comment.