Use functional options for the client constructor
This commit is contained in:
parent
b643635cf4
commit
a7fab72a1e
19
client.go
19
client.go
@ -71,15 +71,28 @@ 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{
|
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{
|
|
||||||
|
c = &Client{
|
||||||
accessToken: accessToken,
|
accessToken: accessToken,
|
||||||
userAgent: defaultUserAgent,
|
|
||||||
Client: &http.Client{Transport: tr},
|
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 {
|
func NewClientWithUserAgent(accessToken string, userAgent string) *Client {
|
||||||
|
@ -223,7 +223,10 @@ func Main(args []string) (int, error) {
|
|||||||
|
|
||||||
command := args[1]
|
command := args[1]
|
||||||
|
|
||||||
c := lifx.NewClientWithUserAgent(config.AccessToken, userAgent)
|
c := lifx.NewClient(
|
||||||
|
config.AccessToken,
|
||||||
|
lifx.WithUserAgent(userAgent),
|
||||||
|
)
|
||||||
|
|
||||||
cmdArgs := CmdArgs{
|
cmdArgs := CmdArgs{
|
||||||
Client: c,
|
Client: c,
|
||||||
|
Loading…
Reference in New Issue
Block a user