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