-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
278 lines (229 loc) · 6.11 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package hue
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/bombsimon/logrusr"
"github.com/go-logr/logr"
"github.com/sirupsen/logrus"
)
const (
defaultBasePath = "api/"
userAgent = "go-hue"
discoveryUrl = "https://discovery.meethue.com/"
)
type Response struct {
*http.Response
}
// ApiResponse that Hue returns
type ApiResponse struct {
Success map[string]interface{} `json:"success,omitempty"`
Error *ApiError `json:"error,omitempty"`
}
// ErrorResponse that Hue returns
type ApiError struct {
Type int `json:"type"`
Address string `json:"address"`
Description string `json:"description"`
}
type discoverResponse struct {
ID string `json:"id"`
Host string `json:"internalipaddress"`
}
type createUserRequest struct {
DeviceType string `json:"devicetype"`
}
type Client struct {
client *http.Client
baseURL *url.URL
userAgent string
clientId string // username for hue bridge
logger logr.Logger
common service
Lights *LightService
Groups *GroupService
}
type service struct {
client *Client
}
type ClientOptions struct {
HttpClient *http.Client
LogLevel logrus.Level
}
// Discover gets hue bridge host address
func Discover() (string, error) {
req, err := http.NewRequest(http.MethodGet, discoveryUrl, nil)
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
discoveryResponses := new([]discoverResponse)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", errors.New("invalid status code returned")
}
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
err = json.Unmarshal(bytes, discoveryResponses)
if err != nil {
return "", err
}
if discoveryResponses == nil || len(*discoveryResponses) == 0 {
return "", errors.New("no bridge found on your network")
}
// Use the first bridge on the network
return (*discoveryResponses)[0].Host, nil
}
func newClient(host string, opts *ClientOptions) (*Client, error) {
var httpClient *http.Client
if opts == nil || opts.HttpClient == nil {
httpClient = &http.Client{} // Create default client
} else {
httpClient = opts.HttpClient
}
u, err := url.Parse(host)
if err != nil {
return nil, err
}
c := &Client{client: httpClient, baseURL: u, userAgent: userAgent}
c.logger = logrusr.NewLogger(logrus.New())
c.common.client = c
c.Lights = (*LightService)(&c.common)
c.Groups = (*GroupService)(&c.common)
return c, nil
}
func NewClient(host, clientId string, opts *ClientOptions) *Client {
c, err := newClient(fmt.Sprintf("http://%v/api/", host), opts)
if err != nil {
c.logger.Error(err, "Couldn't create client")
return nil
}
c.clientId = clientId
return c
}
// CreateUser creates local user on the bridge and returns authenticated client instance
// Don't forget to press bridge button otherwise it will fail
func CreateUser(host, deviceType string, opts *ClientOptions) (*Client, error) {
buf := &bytes.Buffer{}
err := json.NewEncoder(buf).Encode(&createUserRequest{DeviceType: deviceType})
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("http://%s/api", host), buf)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
apiResponses := new([]ApiResponse)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.New("invalid status code returned")
}
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(bytes, apiResponses)
if err != nil {
return nil, err
}
if apiResponses == nil || len(*apiResponses) == 0 || (*apiResponses)[0].Error != nil {
return nil, errors.New((*apiResponses)[0].Error.Description)
}
clientId := (*apiResponses)[0].Success["username"].(string)
return NewClient(host, clientId, opts), nil
}
// GetHost returns ip address of hue bridge
func (c *Client) GetHost() string {
return c.baseURL.Host
}
// GetClientID returns clientID of current client
func (c *Client) GetClientID() string {
return c.clientId
}
func (c *Client) newRequest(method, url string, payload interface{}) (*http.Request, error) {
if !strings.HasSuffix(c.baseURL.Path, "/") {
return nil, fmt.Errorf("BaseURL must have a trailing slash, but %q does not", c.baseURL)
}
u, err := c.baseURL.Parse(url)
if err != nil {
return nil, err
}
var buf io.ReadWriter
if payload != nil {
buf = &bytes.Buffer{}
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
err := enc.Encode(payload)
if err != nil {
return nil, err
}
}
req, err := http.NewRequest(method, u.String(), buf)
if err != nil {
return nil, err
}
if payload != nil {
req.Header.Set("Content-Type", "application/json")
}
req.Header.Set("Accept", "application/json")
if c.userAgent != "" {
req.Header.Set("User-Agent", c.userAgent)
}
return req, nil
}
func (c *Client) do(ctx context.Context, req *http.Request, v interface{}) (*Response, error) {
if ctx == nil {
return nil, errors.New("context must be non-nil")
}
req = req.WithContext(ctx)
resp, err := c.client.Do(req)
if err != nil {
return &Response{Response: resp}, err
}
defer resp.Body.Close()
switch v := v.(type) {
case nil:
case io.Writer:
_, err = io.Copy(v, resp.Body)
default:
decErr := json.NewDecoder(resp.Body).Decode(v)
if decErr == io.EOF {
decErr = nil // ignore EOF errors caused by empty response body
}
if decErr != nil {
err = decErr
}
}
return &Response{Response: resp}, err
}
func (c *Client) path(service string, params ...string) string {
if c.clientId == "" {
c.logger.Info("clientId is missing")
}
if len(params) == 0 {
return fmt.Sprintf("%v/%v", c.clientId, service)
} else if len(params) == 1 {
return fmt.Sprintf("%v/%v/%v", c.clientId, service, params[0])
}
return fmt.Sprintf("%v/%v/%v/%v", c.clientId, service, params[0], params[1])
}