-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfeed_service.go
142 lines (116 loc) · 3.51 KB
/
feed_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
package adafruitio
import (
"fmt"
"path"
)
type FeedService struct {
// CurrentFeed is the Feed used for all Data access.
CurrentFeed *Feed
client *Client
}
// Path generates a Feed-specific path with the given suffix.
func (s *FeedService) Path(suffix string) (string, error) {
ferr := s.client.checkFeed()
if ferr != nil {
return "", ferr
}
return path.Join(fmt.Sprintf("feeds/%v", s.CurrentFeed.Key), suffix), nil
}
type Owner struct {
ID int `json:"id,omitempty"`
Username string `json:"username,omitempty"`
}
type Feed struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Key string `json:"key,omitempty"`
Username string `json:"username,omitempty"`
Owner *Owner `json:"owner,omitempty"`
Description string `json:"description,omitempty"`
UnitType string `json:"unit_type,omitempty"`
UnitSymbol string `json:"unit_symbol,omitempty"`
History bool `json:"history,omitempty"`
Visibility string `json:"visibility,omitempty"`
License string `json:"license,omitempty"`
Enabled bool `json:"enabled,omitempty"`
LastValue string `json:"last_value,omitempty"`
Status string `json:"status,omitempty"`
StatusNotify bool `json:"status_notify,omitempty"`
StatusTimeout int `json:"status_timeout,omitempty"`
Shared bool `json:"is_shared,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
}
// All lists all available feeds.
func (s *FeedService) All() ([]*Feed, *Response, error) {
path := "feeds"
req, rerr := s.client.NewRequest("GET", path, nil)
if rerr != nil {
return nil, nil, rerr
}
// request populates Feed slice
feeds := make([]*Feed, 0)
resp, err := s.client.Do(req, &feeds)
if err != nil {
return nil, resp, err
}
return feeds, resp, nil
}
// Get returns the Feed record identified by the given parameter. Parameter can
// be the Feed's Name, Key, or ID.
func (s *FeedService) Get(key string) (*Feed, *Response, error) {
path := fmt.Sprintf("feeds/%s", key)
req, rerr := s.client.NewRequest("GET", path, nil)
if rerr != nil {
return nil, nil, rerr
}
var feed Feed
resp, err := s.client.Do(req, &feed)
if err != nil {
return nil, resp, err
}
return &feed, resp, nil
}
// Create takes a Feed record, creates it, and returns the updated record or an error.
func (s *FeedService) Create(feed *Feed) (*Feed, *Response, error) {
path := "feeds"
req, rerr := s.client.NewRequest("POST", path, feed)
if rerr != nil {
return nil, nil, rerr
}
resp, err := s.client.Do(req, feed)
if err != nil {
return nil, resp, err
}
return feed, resp, nil
}
// Update takes an ID and a Feed record, updates it, and returns an updated
// record instance or an error.
//
// Only the Feed Name and Description can be modified.
func (s *FeedService) Update(key string, feed *Feed) (*Feed, *Response, error) {
path := fmt.Sprintf("feeds/%s", key)
req, rerr := s.client.NewRequest("PATCH", path, feed)
if rerr != nil {
return nil, nil, rerr
}
var updatedFeed Feed
resp, err := s.client.Do(req, &updatedFeed)
if err != nil {
return nil, resp, err
}
return &updatedFeed, resp, nil
}
// Delete the Feed identified by the given ID.
func (s *FeedService) Delete(key string) (*Response, error) {
path := fmt.Sprintf("feeds/%s", key)
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
}