restructured client

This commit is contained in:
Ryan Cavicchioni 2020-02-29 23:15:39 -06:00
parent f4013e5672
commit de4a4a8643
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
4 changed files with 74 additions and 30 deletions

View File

@ -59,7 +59,7 @@ func (c *Client) NewRequest(method, url string, body io.Reader) (req *http.Reque
return 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) req, err := c.NewRequest(method, url, body)
if err != nil { if err != nil {
return nil, err return nil, err
@ -71,27 +71,28 @@ func (c *Client) Request(method, url string, body io.Reader) ([]Result, error) {
} }
switch resp.StatusCode { switch resp.StatusCode {
case http.StatusAccepted: case http.StatusOK, http.StatusAccepted, http.StatusMultiStatus:
return nil, nil return resp, 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
} }
err, ok := errorMap[resp.StatusCode] err, ok := errorMap[resp.StatusCode]
if ok { 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
} }

View File

@ -17,8 +17,10 @@ type (
} }
HSBKColor struct { HSBKColor struct {
H, K int16 H float32 `json:"hue,omitempty"`
S, B float32 S float32 `json:"saturation,omitempty"`
B float32 `json:"brightness,omitempty"`
K int16 `json:"kelvin,omitempty"`
} }
NamedColor string NamedColor string
@ -45,7 +47,7 @@ func (c RGBColor) Hex() string {
func (c HSBKColor) ColorString() string { func (c HSBKColor) ColorString() string {
var s []string var s []string
if c.H >= 0 { 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 { if c.S >= 0 {
s = append(s, fmt.Sprintf("saturation:%g", c.S)) s = append(s, fmt.Sprintf("saturation:%g", c.S))
@ -66,3 +68,18 @@ func (c HSBKColor) MarshalText() ([]byte, error) {
func (c NamedColor) ColorString() string { func (c NamedColor) ColorString() string {
return string(c) 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
}

View File

@ -20,6 +20,13 @@ var (
EndpointStates = func() string { EndpointStates = func() string {
return BuildURL(Endpoint, "/lights/states") 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 { EndpointToggle = func(selector string) string {
return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/toggle", selector)) return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/toggle", selector))
} }

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
//"crypto/tls" //"crypto/tls"
"encoding/json" "encoding/json"
"fmt"
) )
const ( const (
@ -50,14 +49,22 @@ func (c *Client) SetState(selector string, state State) ([]Result, error) {
return nil, err return nil, err
} }
fmt.Println(string(j)) resp, err := c.Request("PUT", EndpointState(selector), bytes.NewBuffer(j))
res, err := c.Request("PUT", EndpointState(selector), bytes.NewBuffer(j))
if err != nil { if err != nil {
return nil, err 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) { 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 return nil, err
} }
res, err := c.Request("PUT", EndpointStates(), bytes.NewBuffer(j)) resp, err := c.Request("PUT", EndpointStates(), bytes.NewBuffer(j))
if err != nil { if err != nil {
return nil, err 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) { 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 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 { if err != nil {
return nil, err 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) { func (c *Client) PowerOff(selector string) ([]Result, error) {