2020-02-24 03:01:12 +00:00
|
|
|
package lifx
|
|
|
|
|
|
|
|
import (
|
2020-02-29 03:56:33 +00:00
|
|
|
//"crypto/tls"
|
2020-03-07 02:55:31 +00:00
|
|
|
"bytes"
|
2020-02-24 03:01:12 +00:00
|
|
|
"encoding/json"
|
2020-02-29 06:09:06 +00:00
|
|
|
"errors"
|
2020-02-24 03:01:12 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2020-03-07 02:55:31 +00:00
|
|
|
"net/url"
|
2020-04-04 17:18:13 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2020-02-24 03:01:12 +00:00
|
|
|
)
|
|
|
|
|
2021-02-11 04:16:15 +00:00
|
|
|
const defaultUserAgent = "go-lifx"
|
2020-03-01 17:27:26 +00:00
|
|
|
|
2020-02-29 21:57:32 +00:00
|
|
|
type (
|
|
|
|
Client struct {
|
|
|
|
accessToken string
|
2021-02-11 04:16:15 +00:00
|
|
|
userAgent string
|
2020-02-29 21:57:32 +00:00
|
|
|
Client *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
Result struct {
|
2020-03-26 07:16:08 +00:00
|
|
|
Id string `json:"id"`
|
2020-02-29 21:57:32 +00:00
|
|
|
Label string `json:"label"`
|
|
|
|
Status Status `json:"status"`
|
|
|
|
}
|
2020-03-06 07:27:46 +00:00
|
|
|
|
|
|
|
Error struct {
|
|
|
|
Field string `json:"field"`
|
|
|
|
Message []string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
Warning struct {
|
|
|
|
Warning string `json:"warning"`
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:18:13 +00:00
|
|
|
RateLimit struct {
|
|
|
|
Limit int
|
|
|
|
Remaining int
|
|
|
|
Reset time.Time
|
|
|
|
}
|
|
|
|
|
2020-03-06 07:27:46 +00:00
|
|
|
Response struct {
|
2020-04-04 17:18:13 +00:00
|
|
|
StatusCode int
|
|
|
|
Header http.Header
|
|
|
|
Body io.ReadCloser
|
|
|
|
RateLimit RateLimit
|
|
|
|
}
|
|
|
|
|
|
|
|
LifxResponse struct {
|
2020-03-06 07:27:46 +00:00
|
|
|
Error string `json:"error"`
|
|
|
|
Errors []Error `json:"errors"`
|
|
|
|
Warnings []Warning `json:"warnings"`
|
|
|
|
Results []Result `json:"results"`
|
|
|
|
}
|
2020-02-29 21:57:32 +00:00
|
|
|
)
|
|
|
|
|
2020-02-29 06:38:53 +00:00
|
|
|
var errorMap = map[int]error{
|
|
|
|
http.StatusNotFound: errors.New("Selector did not match any lights"),
|
|
|
|
http.StatusUnauthorized: errors.New("Bad access token"),
|
|
|
|
http.StatusForbidden: errors.New("Bad OAuth scope"),
|
|
|
|
http.StatusUnprocessableEntity: errors.New("Missing or malformed parameters"),
|
|
|
|
http.StatusUpgradeRequired: errors.New("HTTP was used to make the request instead of HTTPS. Repeat the request using HTTPS instead"),
|
|
|
|
http.StatusTooManyRequests: errors.New("The request exceeded a rate limit"),
|
|
|
|
http.StatusInternalServerError: errors.New("Something went wrong on LIFX's end"),
|
|
|
|
http.StatusBadGateway: errors.New("Something went wrong on LIFX's end"),
|
|
|
|
http.StatusServiceUnavailable: errors.New("Something went wrong on LIFX's end"),
|
|
|
|
523: errors.New("Something went wrong on LIFX's end"),
|
|
|
|
}
|
|
|
|
|
2021-02-11 15:16:24 +00:00
|
|
|
func NewClient(accessToken string, options ...func(*Client)) *Client {
|
|
|
|
var c *Client
|
2020-02-29 03:56:33 +00:00
|
|
|
tr := &http.Transport{
|
|
|
|
//TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
|
|
|
|
}
|
2021-02-11 15:16:24 +00:00
|
|
|
|
|
|
|
c = &Client{
|
2020-02-29 06:43:32 +00:00
|
|
|
accessToken: accessToken,
|
2021-02-11 04:16:15 +00:00
|
|
|
Client: &http.Client{Transport: tr},
|
|
|
|
}
|
2021-02-11 15:16:24 +00:00
|
|
|
|
|
|
|
for _, option := range options {
|
|
|
|
option(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithUserAgent(userAgent string) func(*Client) {
|
|
|
|
return func(c *Client) {
|
|
|
|
c.userAgent = userAgent
|
|
|
|
}
|
2021-02-11 04:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewClientWithUserAgent(accessToken string, userAgent string) *Client {
|
|
|
|
tr := &http.Transport{
|
|
|
|
//TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
|
|
|
|
}
|
|
|
|
return &Client{
|
|
|
|
accessToken: accessToken,
|
|
|
|
userAgent: userAgent,
|
2020-02-29 06:43:32 +00:00
|
|
|
Client: &http.Client{Transport: tr},
|
2020-02-24 03:01:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:18:13 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-05 23:54:40 +00:00
|
|
|
func (r *Response) IsError() bool {
|
|
|
|
return r.StatusCode > 299
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) GetLifxError() (err error) {
|
|
|
|
var (
|
|
|
|
s *LifxResponse
|
|
|
|
)
|
|
|
|
if err = json.NewDecoder(r.Body).Decode(&s); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New(s.Error)
|
|
|
|
}
|
|
|
|
|
2020-02-29 19:05:07 +00:00
|
|
|
func (c *Client) NewRequest(method, url string, body io.Reader) (req *http.Request, err error) {
|
2020-02-25 06:41:29 +00:00
|
|
|
req, err = http.NewRequest(method, url, body)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-02-29 19:05:07 +00:00
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.accessToken))
|
2020-03-01 17:23:59 +00:00
|
|
|
req.Header.Add("Content-Type", "application/json")
|
2021-02-11 04:16:15 +00:00
|
|
|
req.Header.Add("User-Agent", c.userAgent)
|
2020-02-25 06:41:29 +00:00
|
|
|
return
|
2020-02-24 03:01:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 17:18:13 +00:00
|
|
|
func (c *Client) setState(selector string, state State) (*Response, error) {
|
2020-03-07 02:55:31 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
j []byte
|
|
|
|
req *http.Request
|
2020-04-04 17:18:13 +00:00
|
|
|
r *http.Response
|
|
|
|
resp *Response
|
2020-03-07 02:55:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if j, err = json.Marshal(state); err != nil {
|
2020-02-29 01:34:59 +00:00
|
|
|
return nil, err
|
2020-02-25 06:41:29 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 02:55:31 +00:00
|
|
|
if req, err = c.NewRequest("PUT", EndpointState(selector), bytes.NewBuffer(j)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:18:13 +00:00
|
|
|
if r, err = c.Client.Do(req); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err = NewResponse(r)
|
|
|
|
if err != nil {
|
2020-03-07 02:55:31 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:18:13 +00:00
|
|
|
func (c *Client) setStates(selector string, states States) (*Response, error) {
|
2020-03-07 02:55:31 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
j []byte
|
|
|
|
req *http.Request
|
2020-04-04 17:18:13 +00:00
|
|
|
r *http.Response
|
|
|
|
resp *Response
|
2020-03-07 02:55:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if j, err = json.Marshal(states); err != nil {
|
2020-02-29 01:34:59 +00:00
|
|
|
return nil, err
|
2020-02-24 03:01:12 +00:00
|
|
|
}
|
2020-02-25 06:41:29 +00:00
|
|
|
|
2020-03-07 02:55:31 +00:00
|
|
|
if req, err = c.NewRequest("PUT", EndpointStates(), bytes.NewBuffer(j)); err != nil {
|
|
|
|
return nil, err
|
2020-02-25 06:41:29 +00:00
|
|
|
}
|
2020-02-29 06:38:53 +00:00
|
|
|
|
2020-04-04 17:18:13 +00:00
|
|
|
if r, err = c.Client.Do(req); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err = NewResponse(r)
|
|
|
|
if err != nil {
|
2020-03-07 02:55:31 +00:00
|
|
|
return nil, err
|
2020-03-01 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:18:13 +00:00
|
|
|
func (c *Client) toggle(selector string, duration float64) (*Response, error) {
|
2020-03-07 02:55:31 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
j []byte
|
|
|
|
req *http.Request
|
2020-04-04 17:18:13 +00:00
|
|
|
r *http.Response
|
|
|
|
resp *Response
|
2020-03-07 02:55:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if j, err = json.Marshal(&Toggle{Duration: duration}); err != nil {
|
|
|
|
return nil, err
|
2020-03-01 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 02:55:31 +00:00
|
|
|
if req, err = c.NewRequest("POST", EndpointToggle(selector), bytes.NewBuffer(j)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:18:13 +00:00
|
|
|
if r, err = c.Client.Do(req); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err = NewResponse(r)
|
|
|
|
if err != nil {
|
2020-03-07 02:55:31 +00:00
|
|
|
return nil, err
|
2020-02-29 06:38:53 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 02:55:31 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:18:13 +00:00
|
|
|
func (c *Client) validateColor(color Color) (*Response, error) {
|
2020-03-07 02:55:31 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
req *http.Request
|
2020-04-04 17:18:13 +00:00
|
|
|
r *http.Response
|
|
|
|
resp *Response
|
2020-03-07 02:55:31 +00:00
|
|
|
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()
|
|
|
|
|
2020-04-04 17:18:13 +00:00
|
|
|
if r, err = c.Client.Do(req); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err = NewResponse(r)
|
|
|
|
if err != nil {
|
2020-03-07 02:55:31 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
2020-02-25 06:41:29 +00:00
|
|
|
}
|
2020-03-07 05:00:21 +00:00
|
|
|
|
2020-04-04 17:18:13 +00:00
|
|
|
func (c *Client) listLights(selector string) (*Response, error) {
|
2020-03-07 05:00:21 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
req *http.Request
|
2020-04-04 17:18:13 +00:00
|
|
|
r *http.Response
|
|
|
|
resp *Response
|
2020-03-07 05:00:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if req, err = c.NewRequest("GET", EndpointListLights(selector), nil); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:18:13 +00:00
|
|
|
if r, err = c.Client.Do(req); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err = NewResponse(r)
|
|
|
|
if err != nil {
|
2020-03-07 05:00:21 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
2020-03-08 02:02:37 +00:00
|
|
|
|
2020-04-04 17:18:13 +00:00
|
|
|
func (c *Client) stateDelta(selector string, delta StateDelta) (*Response, error) {
|
2020-03-08 02:02:37 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
j []byte
|
|
|
|
req *http.Request
|
2020-04-04 17:18:13 +00:00
|
|
|
r *http.Response
|
|
|
|
resp *Response
|
2020-03-08 02:02:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if j, err = json.Marshal(delta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if req, err = c.NewRequest("POST", EndpointStateDelta(selector), bytes.NewBuffer(j)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:18:13 +00:00
|
|
|
if r, err = c.Client.Do(req); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err = NewResponse(r)
|
|
|
|
if err != nil {
|
2020-03-08 02:02:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|