Compare commits

..

2 Commits

Author SHA1 Message Date
5bafaa35e4
remove debug code 2020-04-04 12:19:39 -05:00
cc19b87e39
add Response struct, populate rate limit struct 2020-04-04 12:18:13 -05:00
3 changed files with 131 additions and 39 deletions

121
client.go
View File

@ -9,6 +9,8 @@ import (
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
"strconv"
"time"
) )
const UserAgent = "lume" const UserAgent = "lume"
@ -34,7 +36,20 @@ type (
Warning string `json:"warning"` Warning string `json:"warning"`
} }
RateLimit struct {
Limit int
Remaining int
Reset time.Time
}
Response struct { Response struct {
StatusCode int
Header http.Header
Body io.ReadCloser
RateLimit RateLimit
}
LifxResponse struct {
Error string `json:"error"` Error string `json:"error"`
Errors []Error `json:"errors"` Errors []Error `json:"errors"`
Warnings []Warning `json:"warnings"` 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) { func (c *Client) NewRequest(method, url string, body io.Reader) (req *http.Request, err error) {
req, err = http.NewRequest(method, url, body) req, err = http.NewRequest(method, url, body)
if err != nil { if err != nil {
@ -76,12 +125,13 @@ func (c *Client) NewRequest(method, url string, body io.Reader) (req *http.Reque
return return
} }
func (c *Client) setState(selector string, state State) (*http.Response, error) { func (c *Client) setState(selector string, state State) (*Response, error) {
var ( var (
err error err error
j []byte j []byte
req *http.Request req *http.Request
resp *http.Response r *http.Response
resp *Response
) )
if j, err = json.Marshal(state); err != nil { 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 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 nil, err
} }
return resp, nil 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 ( var (
err error err error
j []byte j []byte
req *http.Request req *http.Request
resp *http.Response r *http.Response
resp *Response
) )
if j, err = json.Marshal(states); err != nil { 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 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 nil, err
} }
return resp, nil 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 ( var (
err error err error
j []byte j []byte
req *http.Request req *http.Request
resp *http.Response r *http.Response
resp *Response
) )
if j, err = json.Marshal(&Toggle{Duration: duration}); err != nil { 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 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 nil, err
} }
return resp, nil return resp, nil
} }
func (c *Client) validateColor(color Color) (*http.Response, error) { func (c *Client) validateColor(color Color) (*Response, error) {
var ( var (
err error err error
req *http.Request req *http.Request
resp *http.Response r *http.Response
resp *Response
q url.Values q url.Values
) )
@ -161,37 +229,49 @@ func (c *Client) validateColor(color Color) (*http.Response, error) {
q.Set("string", color.ColorString()) q.Set("string", color.ColorString())
req.URL.RawQuery = q.Encode() 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 nil, err
} }
return resp, nil return resp, nil
} }
func (c *Client) listLights(selector string) (*http.Response, error) { func (c *Client) listLights(selector string) (*Response, error) {
var ( var (
err error err error
req *http.Request req *http.Request
resp *http.Response r *http.Response
resp *Response
) )
if req, err = c.NewRequest("GET", EndpointListLights(selector), nil); err != nil { if req, err = c.NewRequest("GET", EndpointListLights(selector), nil); err != nil {
return nil, err 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 nil, err
} }
return resp, nil 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 ( var (
err error err error
j []byte j []byte
req *http.Request req *http.Request
resp *http.Response r *http.Response
resp *Response
) )
if j, err = json.Marshal(delta); err != nil { 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 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 nil, err
} }

View File

@ -201,13 +201,19 @@ func (c *Client) ValidateColor(color Color) (Color, error) {
var ( var (
err error err error
s *HSBKColor s *HSBKColor
resp *http.Response r *http.Response
resp *Response
) )
if resp, err = c.validateColor(color); err != nil { if resp, err = c.validateColor(color); err != nil {
return nil, err return nil, err
} }
fmt.Println(resp)
resp, err = NewResponse(r)
if err != nil {
return nil, err
}
defer resp.Body.Close() defer resp.Body.Close()
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil { if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {

View File

@ -4,6 +4,7 @@ import (
//"crypto/tls" //"crypto/tls"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"net/http" "net/http"
"time" "time"
) )
@ -89,9 +90,9 @@ type (
} }
) )
func NewApiError(resp *http.Response) error { func NewApiError(resp *Response) error {
var ( var (
s *Response s *LifxResponse
err error err error
) )
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil { 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) return errors.New(s.Error)
} }
func IsApiError(resp *http.Response) bool { func IsApiError(resp *Response) bool {
return resp.StatusCode > 299 return resp.StatusCode > 299
} }
@ -108,11 +109,11 @@ func (s Status) Success() bool {
return s == OK return s == OK
} }
func (c *Client) SetState(selector string, state State) (*Response, error) { func (c *Client) SetState(selector string, state State) (*LifxResponse, error) {
var ( var (
err error err error
s *Response s *LifxResponse
resp *http.Response resp *Response
) )
if resp, err = c.setState(selector, state); err != nil { 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 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 state.Fast = true
return c.SetState(selector, state) 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 ( var (
err error err error
s *Response s *LifxResponse
resp *http.Response resp *Response
) )
if resp, err = c.setStates(selector, states); err != nil { 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 return s, nil
} }
func (c *Client) StateDelta(selector string, delta StateDelta) (*Response, error) { func (c *Client) StateDelta(selector string, delta StateDelta) (*LifxResponse, error) {
var ( var (
err error err error
s *Response s *LifxResponse
resp *http.Response resp *Response
) )
if resp, err = c.stateDelta(selector, delta); err != nil { 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 return s, nil
} }
func (c *Client) Toggle(selector string, duration float64) (*Response, error) { func (c *Client) Toggle(selector string, duration float64) (*LifxResponse, error) {
var ( var (
err error err error
s *Response s *LifxResponse
resp *http.Response resp *Response
) )
if resp, err = c.toggle(selector, duration); err != nil { if resp, err = c.toggle(selector, duration); err != nil {
@ -205,7 +206,7 @@ func (c *Client) ListLights(selector string) ([]Light, error) {
var ( var (
err error err error
s []Light s []Light
resp *http.Response resp *Response
) )
if resp, err = c.listLights(selector); err != nil { if resp, err = c.listLights(selector); err != nil {
@ -224,7 +225,7 @@ func (c *Client) ListLights(selector string) ([]Light, error) {
return s, nil 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"}) 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}) 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"}) return c.SetState(selector, State{Power: "on"})
} }