Compare commits
No commits in common. "5bafaa35e4dd73f4d0ec9df8a96c2b5ddc141fc1" and "f8e9dbc7164def136d9b62132751d7afe0579610" have entirely different histories.
5bafaa35e4
...
f8e9dbc716
121
client.go
121
client.go
@ -9,8 +9,6 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
const UserAgent = "lume"
|
||||
@ -36,20 +34,7 @@ type (
|
||||
Warning string `json:"warning"`
|
||||
}
|
||||
|
||||
RateLimit struct {
|
||||
Limit int
|
||||
Remaining int
|
||||
Reset time.Time
|
||||
}
|
||||
|
||||
Response struct {
|
||||
StatusCode int
|
||||
Header http.Header
|
||||
Body io.ReadCloser
|
||||
RateLimit RateLimit
|
||||
}
|
||||
|
||||
LifxResponse struct {
|
||||
Error string `json:"error"`
|
||||
Errors []Error `json:"errors"`
|
||||
Warnings []Warning `json:"warnings"`
|
||||
@ -80,40 +65,6 @@ func NewClient(accessToken string) *Client {
|
||||
}
|
||||
}
|
||||
|
||||
func NewResponse(r *http.Response) (*Response, error) {
|
||||
resp := Response{
|
||||
StatusCode: r.StatusCode,
|
||||
Header: r.Header,
|
||||
Body: r.Body,
|
||||
}
|
||||
|
||||
if t := r.Header.Get("X-RateLimit-Limit"); t != "" {
|
||||
if n, err := strconv.ParseInt(t, 10, 32); err == nil {
|
||||
resp.RateLimit.Limit = int(n)
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if t := r.Header.Get("X-RateLimit-Remaining"); t != "" {
|
||||
if n, err := strconv.ParseInt(t, 10, 32); err == nil {
|
||||
resp.RateLimit.Remaining = int(n)
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if t := r.Header.Get("X-RateLimit-Reset"); t != "" {
|
||||
if n, err := strconv.ParseInt(t, 10, 32); err == nil {
|
||||
resp.RateLimit.Reset = time.Unix(n, 0)
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
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 {
|
||||
@ -125,13 +76,12 @@ func (c *Client) NewRequest(method, url string, body io.Reader) (req *http.Reque
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) setState(selector string, state State) (*Response, error) {
|
||||
func (c *Client) setState(selector string, state State) (*http.Response, error) {
|
||||
var (
|
||||
err error
|
||||
j []byte
|
||||
req *http.Request
|
||||
r *http.Response
|
||||
resp *Response
|
||||
resp *http.Response
|
||||
)
|
||||
|
||||
if j, err = json.Marshal(state); err != nil {
|
||||
@ -142,25 +92,19 @@ func (c *Client) setState(selector string, state State) (*Response, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if r, err = c.Client.Do(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err = NewResponse(r)
|
||||
if err != nil {
|
||||
if resp, err = c.Client.Do(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) setStates(selector string, states States) (*Response, error) {
|
||||
func (c *Client) setStates(selector string, states States) (*http.Response, error) {
|
||||
var (
|
||||
err error
|
||||
j []byte
|
||||
req *http.Request
|
||||
r *http.Response
|
||||
resp *Response
|
||||
resp *http.Response
|
||||
)
|
||||
|
||||
if j, err = json.Marshal(states); err != nil {
|
||||
@ -171,25 +115,19 @@ func (c *Client) setStates(selector string, states States) (*Response, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if r, err = c.Client.Do(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err = NewResponse(r)
|
||||
if err != nil {
|
||||
if resp, err = c.Client.Do(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) toggle(selector string, duration float64) (*Response, error) {
|
||||
func (c *Client) toggle(selector string, duration float64) (*http.Response, error) {
|
||||
var (
|
||||
err error
|
||||
j []byte
|
||||
req *http.Request
|
||||
r *http.Response
|
||||
resp *Response
|
||||
resp *http.Response
|
||||
)
|
||||
|
||||
if j, err = json.Marshal(&Toggle{Duration: duration}); err != nil {
|
||||
@ -200,24 +138,18 @@ func (c *Client) toggle(selector string, duration float64) (*Response, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if r, err = c.Client.Do(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err = NewResponse(r)
|
||||
if err != nil {
|
||||
if resp, err = c.Client.Do(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) validateColor(color Color) (*Response, error) {
|
||||
func (c *Client) validateColor(color Color) (*http.Response, error) {
|
||||
var (
|
||||
err error
|
||||
req *http.Request
|
||||
r *http.Response
|
||||
resp *Response
|
||||
resp *http.Response
|
||||
q url.Values
|
||||
)
|
||||
|
||||
@ -229,49 +161,37 @@ func (c *Client) validateColor(color Color) (*Response, error) {
|
||||
q.Set("string", color.ColorString())
|
||||
req.URL.RawQuery = q.Encode()
|
||||
|
||||
if r, err = c.Client.Do(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err = NewResponse(r)
|
||||
if err != nil {
|
||||
if resp, err = c.Client.Do(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) listLights(selector string) (*Response, error) {
|
||||
func (c *Client) listLights(selector string) (*http.Response, error) {
|
||||
var (
|
||||
err error
|
||||
req *http.Request
|
||||
r *http.Response
|
||||
resp *Response
|
||||
resp *http.Response
|
||||
)
|
||||
|
||||
if req, err = c.NewRequest("GET", EndpointListLights(selector), nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if r, err = c.Client.Do(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err = NewResponse(r)
|
||||
if err != nil {
|
||||
if resp, err = c.Client.Do(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) stateDelta(selector string, delta StateDelta) (*Response, error) {
|
||||
func (c *Client) stateDelta(selector string, delta StateDelta) (*http.Response, error) {
|
||||
var (
|
||||
err error
|
||||
j []byte
|
||||
req *http.Request
|
||||
r *http.Response
|
||||
resp *Response
|
||||
resp *http.Response
|
||||
)
|
||||
|
||||
if j, err = json.Marshal(delta); err != nil {
|
||||
@ -282,12 +202,7 @@ func (c *Client) stateDelta(selector string, delta StateDelta) (*Response, error
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if r, err = c.Client.Do(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err = NewResponse(r)
|
||||
if err != nil {
|
||||
if resp, err = c.Client.Do(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
10
color.go
10
color.go
@ -201,19 +201,13 @@ func (c *Client) ValidateColor(color Color) (Color, error) {
|
||||
var (
|
||||
err error
|
||||
s *HSBKColor
|
||||
r *http.Response
|
||||
resp *Response
|
||||
resp *http.Response
|
||||
)
|
||||
|
||||
if resp, err = c.validateColor(color); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err = NewResponse(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Println(resp)
|
||||
defer resp.Body.Close()
|
||||
|
||||
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
|
||||
|
39
lights.go
39
lights.go
@ -4,7 +4,6 @@ import (
|
||||
//"crypto/tls"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
@ -90,9 +89,9 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
func NewApiError(resp *Response) error {
|
||||
func NewApiError(resp *http.Response) error {
|
||||
var (
|
||||
s *LifxResponse
|
||||
s *Response
|
||||
err error
|
||||
)
|
||||
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
|
||||
@ -101,7 +100,7 @@ func NewApiError(resp *Response) error {
|
||||
return errors.New(s.Error)
|
||||
}
|
||||
|
||||
func IsApiError(resp *Response) bool {
|
||||
func IsApiError(resp *http.Response) bool {
|
||||
return resp.StatusCode > 299
|
||||
}
|
||||
|
||||
@ -109,11 +108,11 @@ func (s Status) Success() bool {
|
||||
return s == OK
|
||||
}
|
||||
|
||||
func (c *Client) SetState(selector string, state State) (*LifxResponse, error) {
|
||||
func (c *Client) SetState(selector string, state State) (*Response, error) {
|
||||
var (
|
||||
err error
|
||||
s *LifxResponse
|
||||
resp *Response
|
||||
s *Response
|
||||
resp *http.Response
|
||||
)
|
||||
|
||||
if resp, err = c.setState(selector, state); err != nil {
|
||||
@ -136,16 +135,16 @@ func (c *Client) SetState(selector string, state State) (*LifxResponse, error) {
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (c *Client) FastSetState(selector string, state State) (*LifxResponse, error) {
|
||||
func (c *Client) FastSetState(selector string, state State) (*Response, error) {
|
||||
state.Fast = true
|
||||
return c.SetState(selector, state)
|
||||
}
|
||||
|
||||
func (c *Client) SetStates(selector string, states States) (*LifxResponse, error) {
|
||||
func (c *Client) SetStates(selector string, states States) (*Response, error) {
|
||||
var (
|
||||
err error
|
||||
s *LifxResponse
|
||||
resp *Response
|
||||
s *Response
|
||||
resp *http.Response
|
||||
)
|
||||
|
||||
if resp, err = c.setStates(selector, states); err != nil {
|
||||
@ -160,11 +159,11 @@ func (c *Client) SetStates(selector string, states States) (*LifxResponse, error
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (c *Client) StateDelta(selector string, delta StateDelta) (*LifxResponse, error) {
|
||||
func (c *Client) StateDelta(selector string, delta StateDelta) (*Response, error) {
|
||||
var (
|
||||
err error
|
||||
s *LifxResponse
|
||||
resp *Response
|
||||
s *Response
|
||||
resp *http.Response
|
||||
)
|
||||
|
||||
if resp, err = c.stateDelta(selector, delta); err != nil {
|
||||
@ -179,11 +178,11 @@ func (c *Client) StateDelta(selector string, delta StateDelta) (*LifxResponse, e
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (c *Client) Toggle(selector string, duration float64) (*LifxResponse, error) {
|
||||
func (c *Client) Toggle(selector string, duration float64) (*Response, error) {
|
||||
var (
|
||||
err error
|
||||
s *LifxResponse
|
||||
resp *Response
|
||||
s *Response
|
||||
resp *http.Response
|
||||
)
|
||||
|
||||
if resp, err = c.toggle(selector, duration); err != nil {
|
||||
@ -206,7 +205,7 @@ func (c *Client) ListLights(selector string) ([]Light, error) {
|
||||
var (
|
||||
err error
|
||||
s []Light
|
||||
resp *Response
|
||||
resp *http.Response
|
||||
)
|
||||
|
||||
if resp, err = c.listLights(selector); err != nil {
|
||||
@ -225,7 +224,7 @@ func (c *Client) ListLights(selector string) ([]Light, error) {
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (c *Client) PowerOff(selector string) (*LifxResponse, error) {
|
||||
func (c *Client) PowerOff(selector string) (*Response, error) {
|
||||
return c.SetState(selector, State{Power: "off"})
|
||||
}
|
||||
|
||||
@ -233,7 +232,7 @@ func (c *Client) FastPowerOff(selector string) {
|
||||
c.SetState(selector, State{Power: "off", Fast: true})
|
||||
}
|
||||
|
||||
func (c *Client) PowerOn(selector string) (*LifxResponse, error) {
|
||||
func (c *Client) PowerOn(selector string) (*Response, error) {
|
||||
return c.SetState(selector, State{Power: "on"})
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user