You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
167 lines
3.0 KiB
167 lines
3.0 KiB
package hue2
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/tls"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func NewClient(host, token string) *Client {
|
|
ch := make(chan struct{}, 5)
|
|
for i := 0; i < 3; i++ {
|
|
ch <- struct{}{}
|
|
}
|
|
|
|
return &Client{
|
|
host: host,
|
|
token: token,
|
|
ch: ch,
|
|
}
|
|
}
|
|
|
|
type Client struct {
|
|
host string
|
|
token string
|
|
ch chan struct{}
|
|
}
|
|
|
|
func (c *Client) AllResources(ctx context.Context) ([]ResourceData, error) {
|
|
res := struct {
|
|
Error interface{}
|
|
Data []ResourceData
|
|
}{}
|
|
|
|
err := c.get(ctx, "clip/v2/resource", &res)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return res.Data, nil
|
|
}
|
|
|
|
func (c *Client) Resources(ctx context.Context, kind string) ([]ResourceData, error) {
|
|
res := struct {
|
|
Error interface{}
|
|
Data []ResourceData
|
|
}{}
|
|
|
|
err := c.get(ctx, "clip/v2/resource/"+kind, &res)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return res.Data, nil
|
|
}
|
|
|
|
func (c *Client) UpdateResource(ctx context.Context, link ResourceLink, update ResourceUpdate) error {
|
|
return c.put(ctx, link.Path(), update, nil)
|
|
}
|
|
|
|
func (c *Client) get(ctx context.Context, path string, target interface{}) error {
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case <-c.ch:
|
|
defer func() {
|
|
c.ch <- struct{}{}
|
|
}()
|
|
}
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("https://%s/%s", c.host, path), nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
req.Header.Set("hue-application-key", c.token)
|
|
|
|
res, err := httpClient.Do(req.WithContext(ctx))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
if target == nil {
|
|
return nil
|
|
}
|
|
|
|
return json.NewDecoder(res.Body).Decode(&target)
|
|
}
|
|
|
|
func (c *Client) put(ctx context.Context, path string, body interface{}, target interface{}) error {
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case <-c.ch:
|
|
defer func() {
|
|
c.ch <- struct{}{}
|
|
}()
|
|
}
|
|
|
|
rb, err := reqBody(body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
req, err := http.NewRequest("PUT", fmt.Sprintf("https://%s/%s", c.host, path), rb)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
req.Header.Set("hue-application-key", c.token)
|
|
|
|
res, err := httpClient.Do(req.WithContext(ctx))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
if target == nil {
|
|
return nil
|
|
}
|
|
|
|
return json.NewDecoder(res.Body).Decode(&target)
|
|
}
|
|
|
|
func reqBody(body interface{}) (io.Reader, error) {
|
|
if body == nil {
|
|
return nil, nil
|
|
}
|
|
|
|
switch v := body.(type) {
|
|
case []byte:
|
|
return bytes.NewReader(v), nil
|
|
case string:
|
|
return strings.NewReader(v), nil
|
|
case io.Reader:
|
|
return v, nil
|
|
default:
|
|
jsonData, err := json.Marshal(v)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return bytes.NewReader(jsonData), nil
|
|
}
|
|
}
|
|
|
|
var httpClient = &http.Client{
|
|
Transport: &http.Transport{
|
|
Proxy: http.ProxyFromEnvironment,
|
|
DialContext: (&net.Dialer{
|
|
Timeout: 30 * time.Second,
|
|
KeepAlive: 30 * time.Second,
|
|
}).DialContext,
|
|
MaxIdleConns: 256,
|
|
MaxIdleConnsPerHost: 16,
|
|
IdleConnTimeout: 10 * time.Minute,
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
},
|
|
Timeout: time.Minute,
|
|
}
|