refactor client class again

This commit is contained in:
Ryan Cavicchioni 2020-03-06 20:55:31 -06:00
parent c3b8d84968
commit 40a7a9d741
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
4 changed files with 125 additions and 91 deletions

103
client.go
View File

@ -2,12 +2,13 @@ package lifx
import ( import (
//"crypto/tls" //"crypto/tls"
"bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url"
) )
const UserAgent = "go-lifx" const UserAgent = "go-lifx"
@ -75,44 +76,94 @@ 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) (*http.Response, error) { func (c *Client) setStateRequest(selector string, state State) (*http.Response, error) {
req, err := c.NewRequest(method, url, body) var (
if err != nil { err error
j []byte
req *http.Request
resp *http.Response
)
if j, err = json.Marshal(state); err != nil {
return nil, err return nil, err
} }
resp, err := c.Client.Do(req) if req, err = c.NewRequest("PUT", EndpointState(selector), bytes.NewBuffer(j)); err != nil {
if err != nil {
return nil, err return nil, err
} }
switch resp.StatusCode { if resp, err = c.Client.Do(req); err != nil {
case http.StatusOK: return nil, err
fallthrough
case http.StatusAccepted:
fallthrough
case http.StatusMultiStatus:
return resp, nil
}
err, ok := errorMap[resp.StatusCode]
if ok {
return resp, err
} }
return resp, nil return resp, nil
} }
func (c *Client) UnmarshalResponse(resp *http.Response, s interface{}) error { func (c *Client) setStatesRequest(selector string, states States) (*http.Response, error) {
body, err := ioutil.ReadAll(resp.Body) var (
if err != nil { err error
return err 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 req, err = c.NewRequest("PUT", EndpointStates(), bytes.NewBuffer(j)); err != nil {
if err != nil { return nil, err
return 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
} }

View File

@ -1,7 +1,9 @@
package lifx package lifx
import ( import (
"encoding/json"
"fmt" "fmt"
"net/http"
"strings" "strings"
) )
@ -88,14 +90,19 @@ func (c NamedColor) ColorString() string {
} }
func (c *Client) ValidateColor(color Color) (Color, error) { func (c *Client) ValidateColor(color Color) (Color, error) {
resp, err := c.Request("GET", EndpointColor(color.ColorString()), nil) var (
if err != nil { err error
s *HSBKColor
resp *http.Response
)
if resp, err = c.validateColor(color); err != nil {
return nil, err return nil, err
} }
fmt.Println(resp)
defer resp.Body.Close()
s := &HSBKColor{} if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
err = c.UnmarshalResponse(resp, s)
if err != nil {
return nil, err return nil, err
} }

View File

@ -20,12 +20,8 @@ var (
EndpointStates = func() string { EndpointStates = func() string {
return BuildURL(Endpoint, "/lights/states") return BuildURL(Endpoint, "/lights/states")
} }
EndpointColor = func(color string) string { EndpointColor = func() string {
u, _ := url.Parse(BuildURL(Endpoint, "/color")) return 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

@ -1,10 +1,8 @@
package lifx package lifx
import ( import (
"bytes"
//"crypto/tls" //"crypto/tls"
"encoding/json" "encoding/json"
"log"
"net/http" "net/http"
) )
@ -45,30 +43,16 @@ func (s Status) Success() bool {
return s == OK return s == OK
} }
func (c *Client) SetState(selector string, state State) ([]Result, error) { func (c *Client) SetState(selector string, state State) (*Response, error) {
var ( var (
err error err error
s *Response s *Response
j []byte
req *http.Request
resp *http.Response resp *http.Response
) )
if j, err = json.Marshal(state); err != nil { if resp, err = c.setStateRequest(selector, state); err != nil {
log.Println(err)
return nil, err 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() defer resp.Body.Close()
if state.Fast && resp.StatusCode == http.StatusAccepted { 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 { if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
log.Println(err)
return nil, 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 state.Fast = true
return c.SetState(selector, state) return c.SetState(selector, state)
} }
func (c *Client) SetStates(states States) ([]Result, error) { func (c *Client) SetStates(selector string, states States) (*Response, error) {
j, err := json.Marshal(states) var (
if err != nil { 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 return nil, err
} }
resp, err := c.Request("PUT", EndpointStates(), bytes.NewBuffer(j)) return s, nil
if err != nil {
return nil, err
}
s := &Response{}
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) (*Response, error) {
j, err := json.Marshal(&Toggle{Duration: duration}) var (
if err != nil { err error
log.Println(err) 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 return nil, err
} }
resp, err := c.Request("POST", EndpointToggle(selector), bytes.NewBuffer(j)) return s, nil
if err != nil {
return nil, err
}
s := &Response{}
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) (*Response, error) {
return c.SetState(selector, State{Power: "off"}) 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}) 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"}) return c.SetState(selector, State{Power: "on"})
} }