-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdata_service.go
206 lines (169 loc) · 4.9 KB
/
data_service.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
package adafruitio
import "fmt"
// Data are the values contained by a Feed.
type Data struct {
ID string `json:"id,omitempty"`
Value string `json:"value,omitempty"`
FeedID int `json:"feed_id,omitempty"`
FeedKey string `json:"feed_key,omitempty"`
GroupID int `json:"group_id,omitempty"`
Expiration string `json:"expiration,omitempty"`
Latitude float64 `json:"lat,omitempty"`
Longitude float64 `json:"lon,omitempty"`
Elevation float64 `json:"ele,omitempty"`
CompletedAt string `json:"completed_at,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
CreatedEpoch float64 `json:"created_epoch,omitempty"`
}
type DataFilter struct {
StartTime string `url:"start_time,omitempty"`
EndTime string `url:"end_time,omitempty"`
}
type DataService struct {
client *Client
}
// All returns all Data for the currently selected Feed. See Client.SetFeed()
// for details on selecting a Feed.
func (s *DataService) All(opt *DataFilter) ([]*Data, *Response, error) {
path, ferr := s.client.Feed.Path("/data")
if ferr != nil {
return nil, nil, ferr
}
path, oerr := addOptions(path, opt)
if oerr != nil {
return nil, nil, oerr
}
req, rerr := s.client.NewRequest("GET", path, nil)
if rerr != nil {
return nil, nil, rerr
}
// request populates Feed slice
datas := make([]*Data, 0)
resp, err := s.client.Do(req, &datas)
if err != nil {
return nil, resp, err
}
return datas, resp, nil
}
// Search has the same response format as All, but it accepts optional params
// with which your data can be queried.
func (s *DataService) Search(filter *DataFilter) ([]*Data, *Response, error) {
path, ferr := s.client.Feed.Path("/data")
if ferr != nil {
return nil, nil, ferr
}
req, rerr := s.client.NewRequest("GET", path, nil)
if rerr != nil {
return nil, nil, rerr
}
// request populates Feed slice
datas := make([]*Data, 0)
resp, err := s.client.Do(req, &datas)
if err != nil {
return nil, resp, err
}
return datas, resp, nil
}
// Get returns a single Data element, identified by the given ID parameter.
func (s *DataService) Get(id string) (*Data, *Response, error) {
path, ferr := s.client.Feed.Path(fmt.Sprintf("/data/%s", id))
if ferr != nil {
return nil, nil, ferr
}
req, rerr := s.client.NewRequest("GET", path, nil)
if rerr != nil {
return nil, nil, rerr
}
var data Data
resp, err := s.client.Do(req, &data)
if err != nil {
return nil, resp, err
}
return &data, resp, nil
}
// Update takes an ID and a Data record, updates the record idendified by ID,
// and returns a new, updated Data instance.
func (s *DataService) Update(id string, data *Data) (*Data, *Response, error) {
path, ferr := s.client.Feed.Path(fmt.Sprintf("/data/%s", id))
if ferr != nil {
return nil, nil, ferr
}
req, rerr := s.client.NewRequest("PATCH", path, data)
if rerr != nil {
return nil, nil, rerr
}
var updatedData Data
resp, err := s.client.Do(req, &updatedData)
if err != nil {
return nil, resp, err
}
return &updatedData, resp, nil
}
// Delete the Data identified by the given ID.
func (s *DataService) Delete(id string) (*Response, error) {
path, ferr := s.client.Feed.Path(fmt.Sprintf("/data/%s", id))
if ferr != nil {
return nil, ferr
}
req, rerr := s.client.NewRequest("DELETE", path, nil)
if rerr != nil {
return nil, rerr
}
resp, err := s.client.Do(req, nil)
if err != nil {
return nil, err
}
return resp, nil
}
// private method for handling the Next, Prev, and Last commands
func (s *DataService) retrieve(command string) (*Data, *Response, error) {
path, ferr := s.client.Feed.Path(fmt.Sprintf("/data/%v", command))
if ferr != nil {
return nil, nil, ferr
}
req, rerr := s.client.NewRequest("GET", path, nil)
if rerr != nil {
return nil, nil, rerr
}
var data Data
resp, err := s.client.Do(req, &data)
if err != nil {
return nil, resp, err
}
return &data, resp, nil
}
// Next returns the next Data in the stream.
func (s *DataService) Next() (*Data, *Response, error) {
return s.retrieve("next")
}
// Prev returns the previous Data in the stream.
func (s *DataService) Prev() (*Data, *Response, error) {
return s.retrieve("previous")
}
// First returns the first Data in the stream.
func (s *DataService) First() (*Data, *Response, error) {
return s.retrieve("first")
}
// Last returns the last Data in the stream.
func (s *DataService) Last() (*Data, *Response, error) {
return s.retrieve("last")
}
// Create adds a new Data value to an existing Feed.
func (s *DataService) Create(dp *Data) (*Data, *Response, error) {
path, ferr := s.client.Feed.Path("/data")
if ferr != nil {
return nil, nil, ferr
}
req, rerr := s.client.NewRequest("POST", path, dp)
if rerr != nil {
return nil, nil, rerr
}
// request populates a new datapoint
point := &Data{}
resp, err := s.client.Do(req, point)
if err != nil {
return nil, resp, err
}
return point, resp, nil
}