From b643635cf473a85996a4cb5d434d7b6d833709a2 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Wed, 10 Feb 2021 22:16:15 -0600 Subject: [PATCH] Make the user agent configurable --- client.go | 17 +++++++++++++++-- cmd/main.go | 4 +++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/client.go b/client.go index 19c00fd..90a39e8 100644 --- a/client.go +++ b/client.go @@ -13,11 +13,12 @@ import ( "time" ) -const UserAgent = "lume" +const defaultUserAgent = "go-lifx" type ( Client struct { accessToken string + userAgent string Client *http.Client } @@ -76,6 +77,18 @@ func NewClient(accessToken string) *Client { } return &Client{ 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}, } } @@ -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("Content-Type", "application/json") - req.Header.Add("User-Agent", UserAgent) + req.Header.Add("User-Agent", c.userAgent) return } diff --git a/cmd/main.go b/cmd/main.go index d978c3a..af88bbd 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -11,6 +11,8 @@ import ( "github.com/BurntSushi/toml" ) +const userAgent = "lume" + func init() { RegisterCommand("help", Command{ Func: HelpCmd, @@ -221,7 +223,7 @@ func Main(args []string) (int, error) { command := args[1] - c := lifx.NewClient(config.AccessToken) + c := lifx.NewClientWithUserAgent(config.AccessToken, userAgent) cmdArgs := CmdArgs{ Client: c,