Compare commits

..

2 Commits

2 changed files with 35 additions and 4 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
} }
@ -70,12 +71,37 @@ var errorMap = map[int]error{
523: errors.New("Something went wrong on LIFX's end"), 523: errors.New("Something went wrong on LIFX's end"),
} }
func NewClient(accessToken string) *Client { func NewClient(accessToken string, options ...func(*Client)) *Client {
var c *Client
tr := &http.Transport{
//TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
}
c = &Client{
accessToken: accessToken,
Client: &http.Client{Transport: tr},
}
for _, option := range options {
option(c)
}
return c
}
func WithUserAgent(userAgent string) func(*Client) {
return func(c *Client) {
c.userAgent = userAgent
}
}
func NewClientWithUserAgent(accessToken string, userAgent string) *Client {
tr := &http.Transport{ tr := &http.Transport{
//TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper), //TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
} }
return &Client{ return &Client{
accessToken: accessToken, accessToken: accessToken,
userAgent: userAgent,
Client: &http.Client{Transport: tr}, Client: &http.Client{Transport: tr},
} }
} }
@ -135,7 +161,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,10 @@ func Main(args []string) (int, error) {
command := args[1] command := args[1]
c := lifx.NewClient(config.AccessToken) c := lifx.NewClient(
config.AccessToken,
lifx.WithUserAgent(userAgent),
)
cmdArgs := CmdArgs{ cmdArgs := CmdArgs{
Client: c, Client: c,