2020-02-24 03:01:12 +00:00
|
|
|
package lifx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-02-25 06:41:29 +00:00
|
|
|
"io/ioutil"
|
2020-02-24 03:01:12 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewSession(token string) *Session {
|
|
|
|
return &Session{
|
|
|
|
token: token,
|
|
|
|
Client: &http.Client{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 06:41:29 +00:00
|
|
|
func (s *Session) NewRequest(method, url string, body io.Reader) (req *http.Request, err error) {
|
|
|
|
req, err = http.NewRequest(method, url, body)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-02-24 03:01:12 +00:00
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", s.token))
|
2020-02-25 06:41:29 +00:00
|
|
|
return
|
2020-02-24 03:01:12 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 06:41:29 +00:00
|
|
|
func (s *Session) Request(method, url string, body io.Reader) ([]Result, error) {
|
|
|
|
req, err := s.NewRequest(method, url, body)
|
|
|
|
if err != nil {
|
|
|
|
return []Result{}, err
|
|
|
|
}
|
|
|
|
|
2020-02-24 03:01:12 +00:00
|
|
|
resp, err := s.Client.Do(req)
|
|
|
|
if err != nil {
|
2020-02-25 06:41:29 +00:00
|
|
|
return []Result{}, err
|
2020-02-24 03:01:12 +00:00
|
|
|
}
|
2020-02-25 06:41:29 +00:00
|
|
|
|
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusAccepted:
|
|
|
|
return []Result{}, nil
|
|
|
|
case http.StatusMultiStatus:
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return []Result{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r := Results{}
|
|
|
|
err = json.Unmarshal(body, &r)
|
|
|
|
if err != nil {
|
|
|
|
return []Result{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.Results, nil
|
|
|
|
}
|
|
|
|
return []Result{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) SetState(selector string, state *State) ([]Result, error) {
|
|
|
|
j, err := json.Marshal(state)
|
|
|
|
if err != nil {
|
|
|
|
return []Result{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := s.Request("PUT", EndpointState(selector), bytes.NewBuffer(j))
|
|
|
|
if err != nil {
|
|
|
|
return []Result{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) PowerOff(selector string) {
|
|
|
|
s.SetState(selector, &State{Power: "off"})
|
2020-02-24 03:01:12 +00:00
|
|
|
}
|