restructured client
This commit is contained in:
parent
f4013e5672
commit
de4a4a8643
37
client.go
37
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
|
||||
}
|
||||
|
23
color.go
23
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
|
||||
}
|
||||
|
@ -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))
|
||||
}
|
||||
|
37
lights.go
37
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) {
|
||||
|
Loading…
Reference in New Issue
Block a user