Make the user agent configurable

This commit is contained in:
Ryan Cavicchioni 2021-02-10 22:16:15 -06:00
parent 8e7ff7f39a
commit b643635cf4
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
2 changed files with 18 additions and 3 deletions

View File

@ -13,11 +13,12 @@ import (
"time" "time"
) )
const UserAgent = "lume" const defaultUserAgent = "go-lifx"
type ( type (
Client struct { Client struct {
accessToken string accessToken string
userAgent string
Client *http.Client Client *http.Client
} }
@ -76,6 +77,18 @@ func NewClient(accessToken string) *Client {
} }
return &Client{ return &Client{
accessToken: accessToken, accessToken: accessToken,
userAgent: defaultUserAgent,
Client: &http.Client{Transport: tr},
}
}
func NewClientWithUserAgent(accessToken string, userAgent string) *Client {
tr := &http.Transport{
//TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
}
return &Client{
accessToken: accessToken,
userAgent: userAgent,
Client: &http.Client{Transport: tr}, Client: &http.Client{Transport: tr},
} }
} }
@ -135,7 +148,7 @@ func (c *Client) NewRequest(method, url string, body io.Reader) (req *http.Reque
} }
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.accessToken)) req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.accessToken))
req.Header.Add("Content-Type", "application/json") req.Header.Add("Content-Type", "application/json")
req.Header.Add("User-Agent", UserAgent) req.Header.Add("User-Agent", c.userAgent)
return return
} }

View File

@ -11,6 +11,8 @@ import (
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
) )
const userAgent = "lume"
func init() { func init() {
RegisterCommand("help", Command{ RegisterCommand("help", Command{
Func: HelpCmd, Func: HelpCmd,
@ -221,7 +223,7 @@ func Main(args []string) (int, error) {
command := args[1] command := args[1]
c := lifx.NewClient(config.AccessToken) c := lifx.NewClientWithUserAgent(config.AccessToken, userAgent)
cmdArgs := CmdArgs{ cmdArgs := CmdArgs{
Client: c, Client: c,