Compare commits

..

No commits in common. "e373e7e273c47f9e64f0db37a1ac68642e06a776" and "5bafaa35e4dd73f4d0ec9df8a96c2b5ddc141fc1" have entirely different histories.

2 changed files with 26 additions and 19 deletions

View File

@ -114,20 +114,6 @@ func NewResponse(r *http.Response) (*Response, error) {
return &resp, nil
}
func (r *Response) IsError() bool {
return r.StatusCode > 299
}
func (r *Response) GetLifxError() (err error) {
var (
s *LifxResponse
)
if err = json.NewDecoder(r.Body).Decode(&s); err != nil {
return nil
}
return errors.New(s.Error)
}
func (c *Client) NewRequest(method, url string, body io.Reader) (req *http.Request, err error) {
req, err = http.NewRequest(method, url, body)
if err != nil {

View File

@ -3,6 +3,8 @@ package lifx
import (
//"crypto/tls"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
)
@ -88,6 +90,25 @@ type (
}
)
func NewApiError(resp *Response) error {
var (
s *LifxResponse
err error
)
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
return err
}
return errors.New(s.Error)
}
func IsApiError(resp *Response) bool {
return resp.StatusCode > 299
}
func (s Status) Success() bool {
return s == OK
}
func (c *Client) SetState(selector string, state State) (*LifxResponse, error) {
var (
err error
@ -100,8 +121,8 @@ func (c *Client) SetState(selector string, state State) (*LifxResponse, error) {
}
defer resp.Body.Close()
if resp.IsError() {
return nil, resp.GetLifxError()
if IsApiError(resp) {
return nil, NewApiError(resp)
}
if state.Fast && resp.StatusCode == http.StatusAccepted {
@ -170,8 +191,8 @@ func (c *Client) Toggle(selector string, duration float64) (*LifxResponse, error
}
defer resp.Body.Close()
if resp.IsError() {
return nil, resp.GetLifxError()
if IsApiError(resp) {
return nil, NewApiError(resp)
}
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
@ -194,7 +215,7 @@ func (c *Client) ListLights(selector string) ([]Light, error) {
defer resp.Body.Close()
if resp.StatusCode > 299 {
return nil, resp.GetLifxError()
return nil, NewApiError(resp)
}
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {