From de4a4a86430b66a9aac794d7685eb7ecc011616a Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Sat, 29 Feb 2020 23:15:39 -0600 Subject: [PATCH] restructured client --- client.go | 37 +++++++++++++++++++------------------ color.go | 23 ++++++++++++++++++++--- endpoints.go | 7 +++++++ lights.go | 37 ++++++++++++++++++++++++++++--------- 4 files changed, 74 insertions(+), 30 deletions(-) diff --git a/client.go b/client.go index fdf99bb..4e2d141 100644 --- a/client.go +++ b/client.go @@ -59,7 +59,7 @@ func (c *Client) NewRequest(method, url string, body io.Reader) (req *http.Reque return } -func (c *Client) Request(method, url string, body io.Reader) ([]Result, error) { +func (c *Client) Request(method, url string, body io.Reader) (*http.Response, error) { req, err := c.NewRequest(method, url, body) if err != nil { return nil, err @@ -71,27 +71,28 @@ func (c *Client) Request(method, url string, body io.Reader) ([]Result, error) { } switch resp.StatusCode { - case http.StatusAccepted: - return nil, nil - case http.StatusMultiStatus: - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, err - } - - r := Results{} - err = json.Unmarshal(body, &r) - if err != nil { - return nil, err - } - - return r.Results, nil + case http.StatusOK, http.StatusAccepted, http.StatusMultiStatus: + return resp, nil } err, ok := errorMap[resp.StatusCode] if ok { - return nil, err + return resp, err } - return nil, nil + return resp, nil +} + +func (c *Client) UnmarshalResponse(resp *http.Response, s interface{}) error { + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + + err = json.Unmarshal(body, &s) + if err != nil { + return err + } + + return nil } diff --git a/color.go b/color.go index 15d6cdc..f46c3e5 100644 --- a/color.go +++ b/color.go @@ -17,8 +17,10 @@ type ( } HSBKColor struct { - H, K int16 - S, B float32 + H float32 `json:"hue,omitempty"` + S float32 `json:"saturation,omitempty"` + B float32 `json:"brightness,omitempty"` + K int16 `json:"kelvin,omitempty"` } NamedColor string @@ -45,7 +47,7 @@ func (c RGBColor) Hex() string { func (c HSBKColor) ColorString() string { var s []string if c.H >= 0 { - s = append(s, fmt.Sprintf("hue:%d", c.H)) + s = append(s, fmt.Sprintf("hue:%f", c.H)) } if c.S >= 0 { s = append(s, fmt.Sprintf("saturation:%g", c.S)) @@ -66,3 +68,18 @@ func (c HSBKColor) MarshalText() ([]byte, error) { func (c NamedColor) ColorString() string { return string(c) } + +func (c *Client) ValidateColor(color Color) (Color, error) { + resp, err := c.Request("GET", EndpointColor(color.ColorString()), nil) + if err != nil { + return nil, err + } + + s := &HSBKColor{} + err = c.UnmarshalResponse(resp, s) + if err != nil { + return nil, err + } + + return s, nil +} diff --git a/endpoints.go b/endpoints.go index 2c72109..cdba259 100644 --- a/endpoints.go +++ b/endpoints.go @@ -20,6 +20,13 @@ 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() + } EndpointToggle = func(selector string) string { return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/toggle", selector)) } diff --git a/lights.go b/lights.go index e551809..fc6818c 100644 --- a/lights.go +++ b/lights.go @@ -4,7 +4,6 @@ import ( "bytes" //"crypto/tls" "encoding/json" - "fmt" ) const ( @@ -50,14 +49,22 @@ func (c *Client) SetState(selector string, state State) ([]Result, error) { return nil, err } - fmt.Println(string(j)) - - res, err := c.Request("PUT", EndpointState(selector), bytes.NewBuffer(j)) + resp, err := c.Request("PUT", EndpointState(selector), bytes.NewBuffer(j)) if err != nil { return nil, err } - return res, nil + if state.Fast { + return nil, nil + } + + s := &Results{} + err = c.UnmarshalResponse(resp, s) + if err != nil { + return nil, err + } + + return s.Results, nil } func (c *Client) FastSetState(selector string, state State) ([]Result, error) { @@ -71,12 +78,18 @@ func (c *Client) SetStates(states States) ([]Result, error) { return nil, err } - res, err := c.Request("PUT", EndpointStates(), bytes.NewBuffer(j)) + resp, err := c.Request("PUT", EndpointStates(), bytes.NewBuffer(j)) if err != nil { return nil, err } - return res, nil + s := &Results{} + err = c.UnmarshalResponse(resp, s) + if err != nil { + return nil, err + } + + return s.Results, nil } func (c *Client) Toggle(selector string, duration float64) ([]Result, error) { @@ -85,12 +98,18 @@ func (c *Client) Toggle(selector string, duration float64) ([]Result, error) { return nil, err } - res, err := c.Request("POST", EndpointToggle(selector), bytes.NewBuffer(j)) + resp, err := c.Request("POST", EndpointToggle(selector), bytes.NewBuffer(j)) if err != nil { return nil, err } - return res, nil + s := &Results{} + err = c.UnmarshalResponse(resp, s) + if err != nil { + return nil, err + } + + return s.Results, nil } func (c *Client) PowerOff(selector string) ([]Result, error) {