-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponses.go
654 lines (526 loc) · 18 KB
/
responses.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
package ecobee
import (
"encoding/json"
"errors"
"fmt"
"github.com/sherif-fanous/go-ecobee/objects"
"io"
"net/http"
)
type apiStatusResponse struct {
status *objects.Status
}
// APIStatusResponse describes responses returned by the ecobee server that
// only contain the Status object
type APIStatusResponse struct {
apiStatusResponse
}
// String implements the fmt.Stringer interface. It returns a string
// representing an indented JSON encoding of the response.
func (a *APIStatusResponse) String() string {
temp := struct {
Status *objects.Status `json:""`
}{
Status: a.status,
}
data, _ := json.MarshalIndent(&temp, "", " ")
return string(data)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (a *APIStatusResponse) UnmarshalJSON(data []byte) error {
temp := struct {
Status *objects.Status `json:"status"`
}{}
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
a.status = temp.Status
return nil
}
// Status returns the response's status.
func (a *APIStatusResponse) Status() *objects.Status {
return a.status
}
type authorizationErrorResponse struct {
errorType string
errorDescription string
errorURI string
}
// AuthorizationErrorResponse describes the error response returned by the
// ecobee server while authorizing.
type AuthorizationErrorResponse struct {
authorizationErrorResponse
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (e *AuthorizationErrorResponse) UnmarshalJSON(data []byte) error {
temp := struct {
ErrorType string `json:"error"`
ErrorDescription string `json:"error_description"`
ErrorURI string `json:"error_uri"`
}{}
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
e.errorType = temp.ErrorType
e.errorDescription = temp.ErrorDescription
e.errorURI = temp.ErrorURI
return nil
}
// ErrorType returns the response's error type.
func (e *AuthorizationErrorResponse) ErrorType() string {
return e.errorType
}
// ErrorDescription returns the response's error description.
func (e *AuthorizationErrorResponse) ErrorDescription() string {
return e.errorDescription
}
// ErrorURI returns the response's error URI.
func (e *AuthorizationErrorResponse) ErrorURI() string {
return e.errorURI
}
type groupSuccessResponse struct {
groups []objects.Group
status *objects.Status
}
// GroupSuccessResponse describes the response returned by the ecobee
// server while retrieving or updating groups.
type GroupSuccessResponse struct {
groupSuccessResponse
}
// String implements the fmt.Stringer interface. It returns a string
// representing an indented JSON encoding of the response.
func (g *GroupSuccessResponse) String() string {
temp := struct {
Groups []objects.Group `json:""`
Status *objects.Status `json:""`
}{
Groups: g.groups,
Status: g.status,
}
data, _ := json.MarshalIndent(&temp, "", " ")
return string(data)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (g *GroupSuccessResponse) UnmarshalJSON(data []byte) error {
temp := struct {
Groups []objects.Group `json:"groups,omitempty"`
Status *objects.Status `json:"status,omitempty"`
}{}
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
g.groups = temp.Groups
g.status = temp.Status
return nil
}
// Groups returns the response's groups.
func (g *GroupSuccessResponse) Groups() []objects.Group {
groups := make([]objects.Group, len(g.groups), len(g.groups))
copy(groups, g.groups)
return groups
}
// Status returns the response's status.
func (g *GroupSuccessResponse) Status() *objects.Status {
return g.status
}
type meterReportSuccessResponse struct {
reportList []objects.MeterReport
status *objects.Status
}
// MeterReportSuccessResponse describes the success response returned by the
// ecobee server while retrieving a meter report.
type MeterReportSuccessResponse struct {
meterReportSuccessResponse
}
// String implements the fmt.Stringer interface. It returns a string
// representing an indented JSON encoding of the response.
func (m *MeterReportSuccessResponse) String() string {
temp := struct {
ReportList []objects.MeterReport `json:""`
Status *objects.Status `json:""`
}{
ReportList: m.reportList,
Status: m.status,
}
data, _ := json.MarshalIndent(&temp, "", " ")
return string(data)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (m *MeterReportSuccessResponse) UnmarshalJSON(data []byte) error {
temp := struct {
ReportList []objects.MeterReport `json:"reportList,omitempty"`
Status *objects.Status `json:"status,omitempty"`
}{}
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
m.reportList = temp.ReportList
m.status = temp.Status
return nil
}
// ReportList returns the response's status report list.
func (m *MeterReportSuccessResponse) ReportList() []objects.MeterReport {
reportList := make([]objects.MeterReport, len(m.reportList), len(m.reportList))
copy(reportList, m.reportList)
return reportList
}
// Status returns the response's status.
func (m *MeterReportSuccessResponse) Status() *objects.Status {
return m.status
}
type pinAuthorizationSuccessResponse struct {
authorizationToken string
expiresIn int
pin string
pollingInterval int
scope Scope
}
// PINAuthorizationSuccessResponse describes the success response returned by
// the ecobee server while requesting the PIN.
type PINAuthorizationSuccessResponse struct {
pinAuthorizationSuccessResponse
}
// String implements the fmt.Stringer interface. It returns a string
// representing an indented JSON encoding of the response.
func (a *PINAuthorizationSuccessResponse) String() string {
temp := struct {
AuthorizationToken string `json:""`
ExpiresIn int `json:""`
PIN string `json:""`
PollingInterval int `json:""`
Scope Scope `json:""`
}{
AuthorizationToken: a.authorizationToken,
ExpiresIn: a.expiresIn,
PIN: a.pin,
PollingInterval: a.pollingInterval,
Scope: a.scope,
}
data, _ := json.MarshalIndent(&temp, "", " ")
return string(data)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (a *PINAuthorizationSuccessResponse) UnmarshalJSON(data []byte) error {
temp := struct {
AuthorizationToken string `json:"code"`
ExpiresIn int `json:"expires_in"`
PIN string `json:"ecobeePin"`
PollingInterval int `json:"interval"`
Scope Scope `json:"scope"`
}{}
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
a.authorizationToken = temp.AuthorizationToken
a.expiresIn = temp.ExpiresIn
a.pin = temp.PIN
a.pollingInterval = temp.PollingInterval
a.scope = temp.Scope
return nil
}
// AuthorizationToken returns the response's authorization token.
func (a *PINAuthorizationSuccessResponse) AuthorizationToken() string {
return a.authorizationToken
}
// ExpiresIn returns the response's expires in.
func (a *PINAuthorizationSuccessResponse) ExpiresIn() int {
return a.expiresIn
}
// PIN returns the response's PIN.
func (a *PINAuthorizationSuccessResponse) PIN() string {
return a.pin
}
// PollingInterval returns the response's polling interval.
func (a *PINAuthorizationSuccessResponse) PollingInterval() int {
return a.pollingInterval
}
// Scope returns the response's scope.
func (a *PINAuthorizationSuccessResponse) Scope() Scope {
return a.scope
}
type runtimeReportSuccessResponse struct {
reportList []objects.RuntimeReport
sensorList []objects.RuntimeSensorReport
status *objects.Status
}
// RuntimeReportSuccessResponse describes the success response returned by the
// ecobee server while retrieving a runtime report.
type RuntimeReportSuccessResponse struct {
runtimeReportSuccessResponse
}
// String implements the fmt.Stringer interface. It returns a string
// representing an indented JSON encoding of the response.
func (m *RuntimeReportSuccessResponse) String() string {
temp := struct {
ReportList []objects.RuntimeReport `json:""`
SensorList []objects.RuntimeSensorReport `json:""`
Status *objects.Status `json:""`
}{
ReportList: m.reportList,
SensorList: m.sensorList,
Status: m.status,
}
data, _ := json.MarshalIndent(&temp, "", " ")
return string(data)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (m *RuntimeReportSuccessResponse) UnmarshalJSON(data []byte) error {
temp := struct {
ReportList []objects.RuntimeReport `json:"reportList,omitempty"`
SensorList []objects.RuntimeSensorReport `json:"sensorList,omitempty"`
Status *objects.Status `json:"status,omitempty"`
}{}
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
m.reportList = temp.ReportList
m.sensorList = temp.SensorList
m.status = temp.Status
return nil
}
// ReportList returns the response's status report list.
func (m *RuntimeReportSuccessResponse) ReportList() []objects.RuntimeReport {
reportList := make([]objects.RuntimeReport, len(m.reportList), len(m.reportList))
copy(reportList, m.reportList)
return reportList
}
// SensorList returns the response's status sensor list.
func (m *RuntimeReportSuccessResponse) SensorList() []objects.RuntimeSensorReport {
sensorList := make([]objects.RuntimeSensorReport, len(m.sensorList), len(m.sensorList))
copy(sensorList, m.sensorList)
return sensorList
}
// Status returns the response's status.
func (m *RuntimeReportSuccessResponse) Status() *objects.Status {
return m.status
}
type thermostatSuccessResponse struct {
page *objects.Page
thermostatList []objects.Thermostat
status *objects.Status
}
// ThermostatSuccessResponse describes the success response returned by the
// ecobee server while retrieving thermostat data.
type ThermostatSuccessResponse struct {
thermostatSuccessResponse
}
// String implements the fmt.Stringer interface. It returns a string
// representing an indented JSON encoding of the response.
func (t *ThermostatSuccessResponse) String() string {
temp := struct {
Page *objects.Page `json:""`
ThermostatList []objects.Thermostat `json:""`
Status *objects.Status `json:""`
}{
Page: t.page,
ThermostatList: t.thermostatList,
Status: t.status,
}
data, _ := json.MarshalIndent(&temp, "", " ")
return string(data)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (t *ThermostatSuccessResponse) UnmarshalJSON(data []byte) error {
temp := struct {
Page *objects.Page `json:"page,omitempty"`
ThermostatList []objects.Thermostat `json:"thermostatList,omitempty"`
Status *objects.Status `json:"status,omitempty"`
}{}
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
t.page = temp.Page
t.thermostatList = temp.ThermostatList
t.status = temp.Status
return nil
}
// Page returns the response's page.
func (t *ThermostatSuccessResponse) Page() *objects.Page {
return t.page
}
// ThermostatList returns the response's thermostat list.
func (t *ThermostatSuccessResponse) ThermostatList() []objects.Thermostat {
thermostatList := make([]objects.Thermostat, len(t.thermostatList), len(t.thermostatList))
copy(thermostatList, t.thermostatList)
return thermostatList
}
// Status returns the response's status.
func (t *ThermostatSuccessResponse) Status() *objects.Status {
return t.status
}
type thermostatSummarySuccessResponse struct {
revisionList []string
thermostatCount *int
statusList []string
status *objects.Status
}
// ThermostatSummarySuccessResponse describes the success response returned by
// the ecobee server while retrieving thermostat configuration and state
// revisions.
type ThermostatSummarySuccessResponse struct {
thermostatSummarySuccessResponse
}
// String implements the fmt.Stringer interface. It returns a string
// representing an indented JSON encoding of the response.
func (t *ThermostatSummarySuccessResponse) String() string {
temp := struct {
RevisionList []string `json:""`
ThermostatCount *int `json:""`
StatusList []string `json:""`
Status *objects.Status `json:""`
}{
RevisionList: t.revisionList,
ThermostatCount: t.thermostatCount,
StatusList: t.statusList,
Status: t.status,
}
data, _ := json.MarshalIndent(&temp, "", " ")
return string(data)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (t *ThermostatSummarySuccessResponse) UnmarshalJSON(data []byte) error {
temp := struct {
RevisionList []string `json:"revisionList,omitempty"`
ThermostatCount *int `json:"thermostatCount,omitempty"`
StatusList []string `json:"statusList,omitempty"`
Status *objects.Status `json:"status,omitempty"`
}{}
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
t.revisionList = temp.RevisionList
t.thermostatCount = temp.ThermostatCount
t.statusList = temp.StatusList
t.status = temp.Status
return nil
}
// RevisionList returns the response's revision list.
func (t *ThermostatSummarySuccessResponse) RevisionList() []string {
revisionList := make([]string, len(t.revisionList), len(t.revisionList))
copy(revisionList, t.revisionList)
return revisionList
}
// ThermostatCount returns the response's thermostat count.
func (t *ThermostatSummarySuccessResponse) ThermostatCount() int {
return *(t.thermostatCount)
}
// StatusList returns the response's status list.
func (t *ThermostatSummarySuccessResponse) StatusList() []string {
statusList := make([]string, len(t.statusList), len(t.statusList))
copy(statusList, t.statusList)
return statusList
}
// Status returns the response's status.
func (t *ThermostatSummarySuccessResponse) Status() *objects.Status {
return t.status
}
type tokensSuccessResponse struct {
accessToken string
tokenType string
expiresIn int
refreshToken string
scope Scope
}
// TokensSuccessResponse describes the success response returned by the ecobee
// server while requesting tokens.
type TokensSuccessResponse struct {
tokensSuccessResponse
}
// String implements the fmt.Stringer interface. It returns a string
// representing an indented JSON encoding of the response.
func (t *TokensSuccessResponse) String() string {
temp := struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
Scope Scope `json:"scope"`
}{
AccessToken: t.accessToken,
TokenType: t.tokenType,
ExpiresIn: t.expiresIn,
RefreshToken: t.refreshToken,
Scope: t.scope,
}
data, _ := json.MarshalIndent(&temp, "", " ")
return string(data)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (t *TokensSuccessResponse) UnmarshalJSON(data []byte) error {
temp := struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
Scope Scope `json:"scope"`
}{}
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
t.accessToken = temp.AccessToken
t.tokenType = temp.TokenType
t.expiresIn = temp.ExpiresIn
t.refreshToken = temp.RefreshToken
t.scope = temp.Scope
return nil
}
// AccessToken returns the response's access token.
func (t *TokensSuccessResponse) AccessToken() string {
return t.accessToken
}
// TokenType returns the response's token type.
func (t *TokensSuccessResponse) TokenType() string {
return t.tokenType
}
// ExpiresIn returns the response's expires in.
func (t *TokensSuccessResponse) ExpiresIn() int {
return t.expiresIn
}
// RefreshToken returns the response's refresh token.
func (t *TokensSuccessResponse) RefreshToken() string {
return t.refreshToken
}
// Scope returns the response's scope.
func (t *tokensSuccessResponse) Scope() Scope {
return t.scope
}
func processAPIResponse(endpoint string, resp *http.Response, responseObject interface{}) error {
if resp.StatusCode != http.StatusOK {
errorResponse := APIStatusResponse{}
switch err := json.NewDecoder(resp.Body).Decode(&errorResponse); {
case err == io.EOF:
return fmt.Errorf("%s: %s %q: %s", endpoint, resp.Request.Method, resp.Request.URL.String(), resp.Status)
case err != nil:
var e *json.SyntaxError
if errors.As(err, &e) {
return fmt.Errorf("%s: %s %q: %s", endpoint, resp.Request.Method, resp.Request.URL.String(), resp.Status)
}
return fmt.Errorf("%s: %s %q: %s: %w", endpoint, resp.Request.Method, resp.Request.URL.String(), resp.Status, err)
}
return &APIError{errorString: fmt.Sprintf("%s: %s %q: %s: code %d: %s", endpoint, resp.Request.Method, resp.Request.URL.String(), resp.Status, *errorResponse.status.Code, *errorResponse.status.Message)}
}
if err := json.NewDecoder(resp.Body).Decode(responseObject); err != nil {
return fmt.Errorf("%s: %s %q: %s: %w", endpoint, resp.Request.Method, resp.Request.URL.String(), resp.Status, err)
}
return nil
}
func processAuthorizationResponse(endpoint string, resp *http.Response, responseObject interface{}) error {
if resp.StatusCode != http.StatusOK {
errorResponse := AuthorizationErrorResponse{}
switch err := json.NewDecoder(resp.Body).Decode(&errorResponse); {
case err == io.EOF:
return fmt.Errorf("%s: %s %q: %s", endpoint, resp.Request.Method, resp.Request.URL.String(), resp.Status)
case err != nil:
var e *json.SyntaxError
if errors.As(err, &e) {
return fmt.Errorf("%s: %s %q: %s", endpoint, resp.Request.Method, resp.Request.URL.String(), resp.Status)
}
return fmt.Errorf("%s: %s %q: %s: %w", endpoint, resp.Request.Method, resp.Request.URL.String(), resp.Status, err)
}
return &AuthorizationError{errorString: fmt.Sprintf("%s: %s %q: %s: %s: %s: %s", endpoint, resp.Request.Method, resp.Request.URL.String(), resp.Status, errorResponse.errorType, errorResponse.errorDescription, errorResponse.errorURI)}
}
if err := json.NewDecoder(resp.Body).Decode(responseObject); err != nil {
return fmt.Errorf("%s: %s %q: %s: %w", endpoint, resp.Request.Method, resp.Request.URL.String(), resp.Status, err)
}
return nil
}