-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.go
95 lines (79 loc) · 2.48 KB
/
group.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
package ecobee
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/sherif-fanous/go-ecobee/objects"
"io"
"io/ioutil"
"net/url"
)
const (
groupEndpoint = "group"
)
// Group retrieves the grouping data for the thermostats registered to
// the particular user.
//
// For more information see: https://www.ecobee.com/home/developer/api/documentation/v1/operations/get-group.shtml
func (c *Client) Group(ctx context.Context, selection *objects.Selection) (*GroupSuccessResponse, error) {
data, err := json.Marshal(struct {
Selection *objects.Selection `json:"selection,omitempty"`
}{
Selection: selection,
})
if err != nil {
return nil, fmt.Errorf("%s: %w", groupEndpoint, err)
}
queryParameters := url.Values{}
queryParameters.Set("format", "json")
queryParameters.Set("body", string(data))
resp, err := c.get(ctx, fmt.Sprintf("%s%d/%s", c.apiBaseURL, c.apiVersion, groupEndpoint), queryParameters, nil)
if err != nil {
return nil, fmt.Errorf("%s: %w", groupEndpoint, err)
}
defer func() {
if resp != nil {
_, _ = io.Copy(ioutil.Discard, resp.Body)
_ = resp.Body.Close()
}
}()
groupResponse := GroupSuccessResponse{}
if err := processAPIResponse(groupEndpoint, resp, &groupResponse); err != nil {
return nil, err
}
return &groupResponse, nil
}
// UpdateGroup updates the grouping data for the thermostats registered to
// the particular user.
//
// For more information see: https://www.ecobee.com/home/developer/api/documentation/v1/operations/post-group-update.shtml
func (c *Client) UpdateGroup(ctx context.Context, selection *objects.Selection, groups []objects.Group) (*GroupSuccessResponse, error) {
data, err := json.Marshal(struct {
Selection *objects.Selection `json:"selection,omitempty"`
Groups []objects.Group `json:"groups,omitempty"`
}{
Selection: selection,
Groups: groups,
})
if err != nil {
return nil, fmt.Errorf("%s: %w", groupEndpoint, err)
}
queryParameters := url.Values{}
queryParameters.Set("format", "json")
resp, err := c.post(ctx, fmt.Sprintf("%s%d/%s", c.apiBaseURL, c.apiVersion, groupEndpoint), queryParameters, nil, bytes.NewBuffer(data))
if err != nil {
return nil, fmt.Errorf("%s: %w", groupEndpoint, err)
}
defer func() {
if resp != nil {
_, _ = io.Copy(ioutil.Discard, resp.Body)
_ = resp.Body.Close()
}
}()
updateGroupResponse := GroupSuccessResponse{}
if err := processAPIResponse(groupEndpoint, resp, &updateGroupResponse); err != nil {
return nil, err
}
return &updateGroupResponse, nil
}