lume/client.go

115 lines
2.7 KiB
Go
Raw Normal View History

2020-02-24 03:01:12 +00:00
package lifx
import (
2020-02-29 03:56:33 +00:00
//"crypto/tls"
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"
"io/ioutil"
2020-02-24 03:01:12 +00:00
"net/http"
)
2020-03-01 17:27:26 +00:00
const UserAgent = "go-lifx"
2020-02-29 21:57:32 +00:00
type (
Client struct {
accessToken string
Client *http.Client
}
Result struct {
ID string `json:"id"`
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"`
}
Response struct {
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"),
}
2020-02-29 06:43:32 +00:00
func NewClient(accessToken string) *Client {
2020-02-29 03:56:33 +00:00
tr := &http.Transport{
//TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
}
return &Client{
2020-02-29 06:43:32 +00:00
accessToken: accessToken,
Client: &http.Client{Transport: tr},
2020-02-24 03:01:12 +00:00
}
}
2020-02-29 19:05:07 +00:00
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 {
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")
2020-03-01 17:27:26 +00:00
req.Header.Add("User-Agent", UserAgent)
return
2020-02-24 03:01:12 +00:00
}
2020-03-01 05:15:39 +00:00
func (c *Client) Request(method, url string, body io.Reader) (*http.Response, error) {
2020-02-29 19:05:07 +00:00
req, err := c.NewRequest(method, url, body)
if err != nil {
2020-02-29 01:34:59 +00:00
return nil, err
}
2020-02-29 19:05:07 +00:00
resp, err := c.Client.Do(req)
2020-02-24 03:01:12 +00:00
if err != nil {
2020-02-29 01:34:59 +00:00
return nil, err
2020-02-24 03:01:12 +00:00
}
switch resp.StatusCode {
2020-03-01 05:15:39 +00:00
case http.StatusOK, http.StatusAccepted, http.StatusMultiStatus:
return resp, nil
}
2020-02-29 06:38:53 +00:00
err, ok := errorMap[resp.StatusCode]
if ok {
2020-03-01 05:15:39 +00:00
return resp, err
}
return resp, nil
}
func (c *Client) UnmarshalResponse(resp *http.Response, s interface{}) error {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(body, &s)
if err != nil {
return err
2020-02-29 06:38:53 +00:00
}
2020-03-01 05:15:39 +00:00
return nil
}