Skip to content

Commit

Permalink
add initial Feed API
Browse files Browse the repository at this point in the history
  • Loading branch information
abachman committed May 25, 2016
1 parent da3f030 commit 0f1175d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
40 changes: 40 additions & 0 deletions examples/feeds/get_feeds.go
@@ -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))
}
}
41 changes: 41 additions & 0 deletions feed_service.go
@@ -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
}

0 comments on commit 0f1175d

Please sign in to comment.