Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
// Run with: | ||
// go run get_feeds.go -key "MY AIO KEY" | ||
|
||
import ( | ||
// provides adafruitio | ||
|
||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/adafruit/io-client-go" | ||
) | ||
|
||
func main() { | ||
var useURL string | ||
var key string | ||
|
||
flag.StringVar(&useURL, "url", "http://localhost:3002", "Adafruit IO URL") | ||
flag.StringVar(&key, "key", "", "your Adafruit IO key") | ||
|
||
flag.Parse() | ||
|
||
client := adafruitio.NewClient(key) | ||
client.BaseURL, _ = url.Parse(useURL) | ||
|
||
// Get the list of all available feeds | ||
feeds, _, err := client.Feed.All() | ||
if err != nil { | ||
fmt.Println("UNEXPECTED ERROR!", err) | ||
panic(err) | ||
} | ||
|
||
for _, feed := range feeds { | ||
sfeed, _ := json.Marshal(feed) | ||
fmt.Println(string(sfeed)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,49 @@ | ||
package adafruitio | ||
|
||
import ( | ||
"encoding/json" | ||
"path" | ||
) | ||
|
||
type FeedService struct { | ||
client *Client | ||
|
||
// the name or ID of the feed | ||
Name string | ||
} | ||
|
||
type Feed struct { | ||
ID json.Number `json:"id,omitempty"` | ||
Name string `json:"name,omitempty"` | ||
Key string `json:"key,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"` | ||
GroupID json.Number `json:"group_id,omitempty"` | ||
CreatedAt string `json:"created_at,omitempty"` | ||
UpdatedAt string `json:"updated_at,omitempty"` | ||
} | ||
|
||
func (s *FeedService) All() ([]Feed, *Response, error) { | ||
path := path.Join("api", "v1", "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, nil, err | ||
} | ||
|
||
return feeds, resp, nil | ||
} |