diff --git a/client.go b/client.go index 20a1f4f..0349327 100644 --- a/client.go +++ b/client.go @@ -2,12 +2,13 @@ package lifx import ( //"crypto/tls" + "bytes" "encoding/json" "errors" "fmt" "io" - "io/ioutil" "net/http" + "net/url" ) const UserAgent = "go-lifx" @@ -75,44 +76,94 @@ func (c *Client) NewRequest(method, url string, body io.Reader) (req *http.Reque return } -func (c *Client) Request(method, url string, body io.Reader) (*http.Response, error) { - req, err := c.NewRequest(method, url, body) - if err != nil { +func (c *Client) setStateRequest(selector string, state State) (*http.Response, error) { + var ( + err error + j []byte + req *http.Request + resp *http.Response + ) + + if j, err = json.Marshal(state); err != nil { return nil, err } - resp, err := c.Client.Do(req) - if err != nil { + if req, err = c.NewRequest("PUT", EndpointState(selector), bytes.NewBuffer(j)); err != nil { return nil, err } - switch resp.StatusCode { - case http.StatusOK: - fallthrough - case http.StatusAccepted: - fallthrough - case http.StatusMultiStatus: - return resp, nil - } - - err, ok := errorMap[resp.StatusCode] - if ok { - return resp, err + if resp, err = c.Client.Do(req); err != nil { + return nil, err } return resp, nil } -func (c *Client) UnmarshalResponse(resp *http.Response, s interface{}) error { - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return err +func (c *Client) setStatesRequest(selector string, states States) (*http.Response, error) { + var ( + err error + j []byte + req *http.Request + resp *http.Response + ) + + if j, err = json.Marshal(states); err != nil { + return nil, err } - err = json.Unmarshal(body, &s) - if err != nil { - return err + if req, err = c.NewRequest("PUT", EndpointStates(), bytes.NewBuffer(j)); err != nil { + return nil, err } - return nil + if resp, err = c.Client.Do(req); err != nil { + return nil, err + } + + return resp, nil +} + +func (c *Client) toggleRequest(selector string, duration float64) (*http.Response, error) { + var ( + err error + j []byte + req *http.Request + resp *http.Response + ) + + if j, err = json.Marshal(&Toggle{Duration: duration}); err != nil { + return nil, err + } + + if req, err = c.NewRequest("POST", EndpointToggle(selector), bytes.NewBuffer(j)); err != nil { + return nil, err + } + + if resp, err = c.Client.Do(req); err != nil { + return nil, err + } + + return resp, nil +} + +func (c *Client) validateColor(color Color) (*http.Response, error) { + var ( + err error + req *http.Request + resp *http.Response + q url.Values + ) + + if req, err = c.NewRequest("GET", EndpointColor(), nil); err != nil { + return nil, err + } + + q = req.URL.Query() + q.Set("string", color.ColorString()) + req.URL.RawQuery = q.Encode() + + if resp, err = c.Client.Do(req); err != nil { + return nil, err + } + + return resp, nil } diff --git a/color.go b/color.go index 6c4a03a..7f1153b 100644 --- a/color.go +++ b/color.go @@ -1,7 +1,9 @@ package lifx import ( + "encoding/json" "fmt" + "net/http" "strings" ) @@ -88,14 +90,19 @@ func (c NamedColor) ColorString() string { } func (c *Client) ValidateColor(color Color) (Color, error) { - resp, err := c.Request("GET", EndpointColor(color.ColorString()), nil) - if err != nil { + var ( + err error + s *HSBKColor + resp *http.Response + ) + + if resp, err = c.validateColor(color); err != nil { return nil, err } + fmt.Println(resp) + defer resp.Body.Close() - s := &HSBKColor{} - err = c.UnmarshalResponse(resp, s) - if err != nil { + if err = json.NewDecoder(resp.Body).Decode(&s); err != nil { return nil, err } diff --git a/endpoints.go b/endpoints.go index cdba259..330df99 100644 --- a/endpoints.go +++ b/endpoints.go @@ -20,12 +20,8 @@ var ( EndpointStates = func() string { return BuildURL(Endpoint, "/lights/states") } - EndpointColor = func(color string) string { - u, _ := url.Parse(BuildURL(Endpoint, "/color")) - q := u.Query() - q.Set("string", color) - u.RawQuery = q.Encode() - return u.String() + EndpointColor = func() string { + return BuildURL(Endpoint, "/color") } EndpointToggle = func(selector string) string { return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/toggle", selector)) diff --git a/lights.go b/lights.go index 70cc85d..0909eea 100644 --- a/lights.go +++ b/lights.go @@ -1,10 +1,8 @@ package lifx import ( - "bytes" //"crypto/tls" "encoding/json" - "log" "net/http" ) @@ -45,30 +43,16 @@ func (s Status) Success() bool { return s == OK } -func (c *Client) SetState(selector string, state State) ([]Result, error) { +func (c *Client) SetState(selector string, state State) (*Response, error) { var ( err error s *Response - j []byte - req *http.Request resp *http.Response ) - if j, err = json.Marshal(state); err != nil { - log.Println(err) + if resp, err = c.setStateRequest(selector, state); err != nil { return nil, err } - - if req, err = c.NewRequest("PUT", EndpointState(selector), bytes.NewBuffer(j)); err != nil { - log.Println(err) - return nil, err - } - - if resp, err = c.Client.Do(req); err != nil { - log.Println(err) - return nil, err - } - defer resp.Body.Close() if state.Fast && resp.StatusCode == http.StatusAccepted { @@ -76,60 +60,56 @@ func (c *Client) SetState(selector string, state State) ([]Result, error) { } if err = json.NewDecoder(resp.Body).Decode(&s); err != nil { - log.Println(err) return nil, err } - return s.Results, nil + return s, nil } -func (c *Client) FastSetState(selector string, state State) ([]Result, error) { +func (c *Client) FastSetState(selector string, state State) (*Response, error) { state.Fast = true return c.SetState(selector, state) } -func (c *Client) SetStates(states States) ([]Result, error) { - j, err := json.Marshal(states) - if err != nil { +func (c *Client) SetStates(selector string, states States) (*Response, error) { + var ( + err error + s *Response + resp *http.Response + ) + + if resp, err = c.setStatesRequest(selector, states); err != nil { + return nil, err + } + defer resp.Body.Close() + + if err = json.NewDecoder(resp.Body).Decode(&s); err != nil { return nil, err } - resp, err := c.Request("PUT", EndpointStates(), bytes.NewBuffer(j)) - if err != nil { - return nil, err - } - - s := &Response{} - err = c.UnmarshalResponse(resp, s) - if err != nil { - return nil, err - } - - return s.Results, nil + return s, nil } -func (c *Client) Toggle(selector string, duration float64) ([]Result, error) { - j, err := json.Marshal(&Toggle{Duration: duration}) - if err != nil { - log.Println(err) +func (c *Client) Toggle(selector string, duration float64) (*Response, error) { + var ( + err error + s *Response + resp *http.Response + ) + + if resp, err = c.toggleRequest(selector, duration); err != nil { + return nil, err + } + defer resp.Body.Close() + + if err = json.NewDecoder(resp.Body).Decode(&s); err != nil { return nil, err } - resp, err := c.Request("POST", EndpointToggle(selector), bytes.NewBuffer(j)) - if err != nil { - return nil, err - } - - s := &Response{} - err = c.UnmarshalResponse(resp, s) - if err != nil { - return nil, err - } - - return s.Results, nil + return s, nil } -func (c *Client) PowerOff(selector string) ([]Result, error) { +func (c *Client) PowerOff(selector string) (*Response, error) { return c.SetState(selector, State{Power: "off"}) } @@ -137,7 +117,7 @@ func (c *Client) FastPowerOff(selector string) { c.SetState(selector, State{Power: "off", Fast: true}) } -func (c *Client) PowerOn(selector string) ([]Result, error) { +func (c *Client) PowerOn(selector string) (*Response, error) { return c.SetState(selector, State{Power: "on"}) }