m2x

package module
v0.0.0-...-a60ff72 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 24, 2014 License: MIT Imports: 5 Imported by: 0

README

m2x-go

wercker status

Go library for AT&T's M2X API. AT&T's M2X is a cloud-based data storage service and management toolset customized for the internet of things.

Version

0.3

Installation

go get github.com/jsgoecke/m2x-go

Documentation

M2X @ Godoc.org

Usage

M2X Client
package main

import (
	"github.com/jsgoecke/m2x-go"
	"log"
	"os"
)

func main() {
	// Create a client
	client := m2x.NewClient(os.Getenv("M2X_API_KEY"))

	// Create a blueprint
	blueprintData := make(map[string]string)
	blueprintData["name"] = "Go Blueprint"
	blueprintData["description"] = "A blueprint for the Go lib for M2X"
	blueprintData["visibility"] = "private"

	blueprint, errorMessage := client.CreateBlueprint(blueprintData)
	if errorMessage != nil {
		log.Println(errorMessage)
	}

	// Update a bluprint
	blueprintData["description"] = "A blueprint for the Go lib for AT&T M2X"
	errorMessage = client.UpdateBlueprint(blueprint.Id, blueprintData)

	// Create a stream
	streamData := make(map[string]interface{})
	unit := make(map[string]string)
	unit["label"] = "celcius"
	unit["symbol"] = "C"
	streamData["unit"] = unit
	errorMessage = client.UpdateFeedStream(blueprint.Feed, "temperature", streamData)
	if errorMessage != nil {
		log.Println("Error creating stream")
	}

	//Update location of the feed stream
	loc := make(map[string]interface{})
	loc["name"] = "Storage Room in Sevilla, Spain"
	loc["latitude"] = "37.383055"
	loc["longitude"] = "-5.996392"
	loc["elevation"] = "5"
	errorMessage = client.UpdateFeedLocation(blueprint.Feed, loc)
	if errorMessage != nil {
		log.Println("Error updating location")
	}

	// Create a trigger for the feed
	triggerData := make(map[string]string)
	triggerData["name"] = "foobar"
	triggerData["stream"] = "temperature"
	triggerData["condition"] = ">"
	triggerData["value"] = "30"
	triggerData["callback_url"] = "http://45bad07a.ngrok.com/streamEvent"
	triggerData["status"] = "enabled"
	_, errorMessage = client.CreateTrigger(blueprint.Feed, triggerData)
	if errorMessage != nil {
		log.Println("Error creating trigger")
	}

	// Update stream with data
	values := make(map[string]interface{})
	values["values"] = []*m2x.Value{
		{"2013-09-09T19:15:00Z", "32"},
		{"2013-09-09T19:16:00Z", "28 "},
		{"2013-09-09T19:17:00Z", "25"},
		{"2013-09-09T19:17:00Z", "40"},
	}
	errorMessage = client.UpdateFeedStreamValues(blueprint.Feed, "temperature", values)

	// Delete the blueprint
	client.DeleteBlueprint(blueprint.Id)
}
M2X Event Receiver
package main

import (
	"github.com/jsgoecke/m2x-go"
	"encoding/json"
	"github.com/codegangsta/martini"
	"io/ioutil"
	"log"
	"net/http"
)

func main() {
	m := martini.Classic()
	m.Post("/streamEvent", streamRequestHandler)
	http.ListenAndServe(":3000", m)
}

func streamRequestHandler(w http.ResponseWriter, r *http.Request) {
	body, _ := ioutil.ReadAll(r.Body)
	r.Body.Close()
	triggerEvent, err := m2x.ParseTriggerEvent(body)
	if err != nil {
		log.Println(err)
	} else {
		log.Println("Received trigger event!")
		jsonData, _ := json.MarshalIndent(triggerEvent, "", "    ")
		log.Println(string(jsonData[:]))
	}
}

Testing

Right now the tests are a combination of unit tests and functional tests. For the functional tests to run, you will need to set an environment variable 'M2X_API_KEY' with a valid key. Keep in mind that the tests will add and remove elements from your account, and if a tests fail may orphan the elements.

cd m2x-go
go test
Test Coverage

http://gocover.io/github.com/jsgoecke/m2x-go

Lint

http://go-lint.appspot.com/github.com/jsgoecke/m2x-go

Todo

1. Mock out net/http requests in order to move functional tests to unit tests.

License

MIT

Documentation

Index

Constants

View Source
const (
	// UserAgent represents the value of the HTTP user agent
	UserAgent = "M2X/1 (Go net/http)"
	// Version of the library
	Version = "0.3"
)

Variables

View Source
var APIKey string

APIKey is the key for the API

Functions

func ParseTriggerEvent

func ParseTriggerEvent(data []byte) (map[string]interface{}, error)

ParseTriggerEvent parses the JSON for an event returned by a trigger Due to inconsistent types returned, using a Map (http://forum-m2x.att.com/47j-triggers-not-firing-but-work-on-test#post14953) instead of a struct. If resolved, may change back later.

triggerEvent := m2x.ParseTriggerEvent(body)

JSON POSTed:

		{
   		"feed_id":"12345",
   		"stream":"call",
   		"trigger_name":"OffCall",
   		"trigger_description":"call < 1",
   		"condition":"<",
   		"threshold":"1",
   		"value":"0",
   		"at":"2014-01-13T14:35:23Z"
		}

Types

type Batch

type Batch struct {
	ID          string     `json:"id"`
	Name        string     `json:"name"`
	Description string     `json:"description"`
	Visibility  string     `json:"visibility"`
	Serial      string     `json:"serial"`
	Status      string     `json:"status"`
	Feed        string     `json:"feed"`
	URL         string     `json:"url"`
	Key         string     `json:"key"`
	Tags        []string   `json:"tags"`
	Created     string     `json:"created"`
	Updated     string     `json:"updated"`
	Datasources Datasource `json:"datasources"`
}

Batch represents a single batch

type Batches

type Batches struct {
	Batches     []Batch `json:"batches"`
	Total       int     `json:"total"`
	Pages       int     `json:"pages"`
	Limit       int     `json:"limit"`
	CurrentPage int     `json:"current_page"`
}

Batches represents a collection of batches

type Blueprint

type Blueprint struct {
	ID          string     `json:"id"`
	Name        string     `json:"name"`
	Description string     `json:"description"`
	Visibility  string     `json:"visibility"`
	Serial      string     `json:"serial"`
	Status      string     `json:"status"`
	Feed        string     `json:"feed"`
	URL         string     `json:"url"`
	Key         string     `json:"key"`
	Tags        []string   `json:"tags"`
	Created     string     `json:"created"`
	Updated     string     `json:"updated"`
	Datasources Datasource `json:"datasources"`
}

Blueprint represents a single blueprint

type Blueprints

type Blueprints struct {
	Blueprints  []Blueprint `json:"blueprints"`
	Total       int         `json:"total"`
	Pages       int         `json:"pages"`
	Limit       int         `json:"limit"`
	CurrentPage int         `json:"current_page"`
}

Blueprints represents a collection of blueprints (https://m2x.att.com/developer/documentation/datasource)

type Client

type Client struct {
	APIBase string
	Headers map[string]string
}

Client represents a client for the M2X API (https://m2x.att.com/developer/documentation/overview)

func NewClient

func NewClient(apiKey string) *Client

NewClient creates a NewClient for the M2X API

client := NewClient("<API-KEY>")

func (*Client) Batch

func (c *Client) Batch(id string) (*Batch, *ErrorMessage)

Batch gets a batch

batch, err := client.Batch("1234")

func (*Client) Batches

func (c *Client) Batches() (*Batches, *ErrorMessage)

Batches gets a list of batches

batches, err := client.Batches()

func (*Client) Blueprint

func (c *Client) Blueprint(id string) (*Blueprint, *ErrorMessage)

Blueprint gets a blueprint

blueprint, err := client.Blueprint("1234")

func (*Client) Blueprints

func (c *Client) Blueprints() (*Blueprints, *ErrorMessage)

Blueprints gets a list of blueprints

blueprints, err := client.Blueprints()

func (*Client) CreateBatch

func (c *Client) CreateBatch(batch map[string]string) (*Batch, *ErrorMessage)

CreateBatch creates a new batch

batchData := make(map[string]string)
batchData["name"] = "Go Batch"
batchData["description"] = "A batch for the Go lib for M2X"
batchData["visibility"] = "private"
batch, err := client.CreateBatch(batch)

func (*Client) CreateBlueprint

func (c *Client) CreateBlueprint(blueprint map[string]string) (*Blueprint, *ErrorMessage)

CreateBlueprint creates a new blueprint

blueprintData := make(map[string]string)
blueprintData["name"] = "Go Blueprint"
blueprintData["description"] = "A blueprint for the Go lib for M2X"
blueprintData["visibility"] = "private"
blueprint, err := client.CreateBlueprint(blueprintData)

func (*Client) CreateKey

func (c *Client) CreateKey(key map[string]interface{}) (*Key, *ErrorMessage)

CreateKey creates a key

keyData := make(map[string]interface{})
name := "Go Created Key"
keyData["name"] = name
keyData["permissions"] = [...]string{"GET", "PUT"}
key, err := client.CreateKey(keyData)

func (*Client) CreateTrigger

func (c *Client) CreateTrigger(resource string, trigger map[string]string) (*Trigger, *ErrorMessage)

CreateTrigger creates a trigger on a feed stream

triggerData := make(map[string]string)
triggerData["name"] = "foobar"
triggerData["stream"] = "temperature"
triggerData["condition"] = ">"
triggerData["value"] = "30"
triggerData["callback_url"] = "http://45bad07a.ngrok.com/streamEvent"
triggerData["status"] = "enabled"
trigger, err := client.CreateTrigger(blueprint.Feed, triggerData)

func (*Client) DeleteBatch

func (c *Client) DeleteBatch(id string) (*Batch, *ErrorMessage)

DeleteBatch deletes a batch

err := client.DeleteBatch(batch.ID)

func (*Client) DeleteBlueprint

func (c *Client) DeleteBlueprint(id string) *ErrorMessage

DeleteBlueprint deletes a blueprint

err := client.DeleteBlueprint(blueprint.ID)

func (*Client) DeleteKey

func (c *Client) DeleteKey(id string) *ErrorMessage

DeleteKey deletes a key

err := client.DeleteKey("1234")

func (*Client) DeleteTrigger

func (c *Client) DeleteTrigger(resource string, id string) *ErrorMessage

DeleteTrigger deletes a trigger from a feed stream

err := client.DeleteTrigger("/feeds/1234", "1235")

func (*Client) Feed

func (c *Client) Feed(resource string) (*Feed, *ErrorMessage)

Feed gets a feed

feed, err := client.Feed("/feeds/1234")

func (*Client) FeedLocation

func (c *Client) FeedLocation(resource string) (*Location, *ErrorMessage)

FeedLocation gets a feed location

feed, err := client.FeedLocation("/feeds/1234")

func (*Client) FeedStream

func (c *Client) FeedStream(resource string, name string) (*Stream, *ErrorMessage)

FeedStream list a feed stream

stream, err := client.FeedStream("/feeds/1234", "temperature")

func (*Client) FeedStreamValues

func (c *Client) FeedStreamValues(resource string, name string) (*Values, *ErrorMessage)

FeedStreamValues list the feeds stream values

values, err := client.FeedStreamValues("/feeds/1234", "temperature")

func (*Client) Feeds

func (c *Client) Feeds() (*Feeds, *ErrorMessage)

Feeds gets a list of feeds

feeds, err := client.Feeds()

func (*Client) Key

func (c *Client) Key(id string) (*Key, *ErrorMessage)

Key gets a list of blueprints from the /key resource

key, err := client.Key()

func (*Client) Keys

func (c *Client) Keys() (*Blueprints, *ErrorMessage)

Keys gets a list of keys from the /keys resource

keys, err := client.Keys()

func (*Client) RequestLog

func (c *Client) RequestLog(resource string) (*Requests, *ErrorMessage)

RequestLog requests a log

requests, err := RequestLog("/feeds/1234")

func (*Client) Status

func (c *Client) Status() (*Status, error)

Status gets the status of the M2X client

result, err := client.Status()

func (*Client) TestTrigger

func (c *Client) TestTrigger(resource string, name string) *ErrorMessage

TestTrigger tests a trigger

err := client.TestTrigger("/feeds/1234", "foobar")

func (*Client) Trigger

func (c *Client) Trigger(resource string, id string) (*Trigger, *ErrorMessage)

Trigger lists a trigger on a feed stream

trigger, err := client.Trigger("/feeds/1234", "1235")

func (*Client) Triggers

func (c *Client) Triggers(resource string) (*Triggers, *ErrorMessage)

Triggers lists a collection of triggers on a feed stream

triggers, err := client.Triggers()

func (*Client) UpdateBatch

func (c *Client) UpdateBatch(id string, updateData map[string]string) *ErrorMessage

UpdateBatch updates a batch

batchData["description"] = "A batch for the Go lib for AT&T M2X"
err := client.UpdateBatch(batch.ID, batchData)

func (*Client) UpdateBlueprint

func (c *Client) UpdateBlueprint(id string, updateData map[string]string) *ErrorMessage

UpdateBlueprint updates a blueprint

blueprintData["description"] = "A blueprint for the Go lib for AT&T M2X"
err := client.UpdateBlueprint(blueprint.ID, blueprintData)

func (*Client) UpdateFeedLocation

func (c *Client) UpdateFeedLocation(resource string, updateData map[string]interface{}) *ErrorMessage

UpdateFeedLocation create/Update a feed location

loc := make(map[string]interface{})
loc["name"] = "Storage Room in Sevilla, Spain"
loc["latitude"] = "37.383055"
loc["longitude"] = "-5.996392"
loc["elevation"] = "5"
err := client.UpdateFeedLocation("/feeds/1234", loc)

func (*Client) UpdateFeedStream

func (c *Client) UpdateFeedStream(resource string, name string, updateData map[string]interface{}) *ErrorMessage

UpdateFeedStream update a feed stream

streamData := make(map[string]interface{})
unit := make(map[string]string)
unit["label"] = "celcius"
unit["symbol"] = "C"
streamData["unit"] = unit
err := client.UpdateFeedStream("/feeds/1234", "temperature", streamData)

func (*Client) UpdateFeedStreamValues

func (c *Client) UpdateFeedStreamValues(resource string, name string, updateData map[string]interface{}) *ErrorMessage

UpdateFeedStreamValues update feeds stream values

values := make(map[string]interface{})
values["values"] = []*m2x.Value{
	{"2013-09-09T19:15:00Z", "32"},
	{"2013-09-09T19:16:00Z", "28 "},
	{"2013-09-09T19:17:00Z", "25"},
	{"2013-09-09T19:17:00Z", "40"},
}
err := client.UpdateFeedStreamValues("/feeds/1234", "temperature", values)

func (*Client) UpdateKey

func (c *Client) UpdateKey(id string, updateData map[string]interface{}) *ErrorMessage

UpdateKey updates a key

keyData["name"] = "Go key"
err := client.UpdateKey("/feeds/1234", keyData)

func (*Client) UpdateTrigger

func (c *Client) UpdateTrigger(resource string, id string, updateData map[string]string) *ErrorMessage

UpdateTrigger updates a trigger on a feed stream

triggerData["callback_url"] = "http://host.com/streamEvent"
triggerData["status"] = "disabled"
err := client.UpdateTrigger("/feeds/1234", "1235", triggerData)

type Datasource

type Datasource struct {
	Total        int `json:"total"`
	Registered   int `json:"registered"`
	Unregistered int `json:"unregistered"`
}

Datasource represents a single datasource

type Error

type Error struct {
	Name []string `json:"name"`
}

Error is the simple error message

type ErrorMessage

type ErrorMessage struct {
	Message    string `json:"message"`
	StatusCode int
	Errors     Error `json:"errors"`
	Error      error
}

ErrorMessage represents an API error message

type Feed

type Feed struct {
	ID          string    `json:"id"`
	Name        string    `json:"name"`
	Description string    `json:"description"`
	Visibility  string    `json:"visibility"`
	Status      string    `json:"status"`
	Type        string    `json:"type"`
	Tags        []string  `json:"tags"`
	URL         string    `json:"url"`
	Key         string    `json:"key"`
	Created     string    `json:"created"`
	Updated     string    `json:"updated"`
	Location    Location  `json:"location"`
	Streams     []Stream  `json:"streams"`
	Triggers    []Trigger `json:"triggers"`
}

Feed represents an individual feed

type Feeds

type Feeds struct {
	Feeds       []Feed `json:"feeds"`
	Total       int    `json:"total"`
	Pages       int    `json:"pages"`
	Limit       int    `json:"limit"`
	CurrentPage int    `json:"current_page"`
}

Feeds represents a collection of feeds resource (https://m2x.att.com/developer/documentation/feed)

type Key

type Key struct {
	ID          string   `json:"id"`
	Name        string   `json:"name"`
	Key         string   `json:"key"`
	Master      bool     `json:"master"`
	Feed        string   `json:"feed"`
	Stream      string   `json:"stream"`
	ExpiresAt   string   `json:"expires_at"`
	Expired     string   `json:"expired"`
	Permissions []string `json:"permissions"`
}

Key represents a single Key

type Keys

type Keys struct {
	Keys        []Key `json:"keys"`
	Total       int   `json:"total"`
	Pages       int   `json:"pages"`
	Limit       int   `json:"limit"`
	CurrentPage int   `json:"current_page"`
}

Keys represents a Keys response from the M2X API (https://m2x.att.com/developer/documentation/keys)

type Location

type Location struct {
	Name      string     `json:"name"`
	Latitude  string     `json:"latitude"`
	Longitude string     `json:"longitude"`
	Elevation string     `json:"elevation"`
	Waypoints []Waypoint `json:"waypoints"`
}

Location represents a location

type Request

type Request struct {
	At     string `json:"at"`
	Status int    `json:"status"`
	Method string `json:"method"`
	Path   string `json:"path"`
}

Request represents a request

type Requests

type Requests struct {
	Requests []Request
}

Requests represents a collection of request

type Status

type Status struct {
	API      string `json:"api"`
	Triggers string `json:"triggers"`
}

Status represents a status returned by the /status resource

type Stream

type Stream struct {
	Name    string      `json:"name"`
	Value   interface{} `json:"value"`
	Min     interface{} `json:"min"`
	Max     interface{} `json:"max"`
	Unit    Unit        `json:"unit"`
	URL     string      `json:"url"`
	Created string      `json:"created"`
	Updated string      `json:"updated"`
}

Stream represents a tream

type Trigger

type Trigger struct {
	ID          string `json:"id"`
	Name        string `json:"name"`
	Stream      string `json:"stream"`
	Condition   string `json:"condition"`
	Value       string `json:"value"`
	CallbackURL string `json:"callback_url"`
	URL         string `json:"url"`
	Status      string `json:"status"`
	Created     string `json:"created"`
	Updated     string `json:"updated"`
}

Trigger represents a trigger

type Triggers

type Triggers struct {
	Triggers []Trigger
}

Triggers represents a collection of triggers (https://m2x.att.com/developer/documentation/feed#List-Triggers)

type Unit

type Unit struct {
	Label  string `json:"label"`
	Symbol string `json:"symbol"`
}

Unit represents a request

type Value

type Value struct {
	At    string `json:"at"`
	Value string `json:"value"`
}

Value represents a value

type Values

type Values struct {
	Start  string `json:"start"`
	End    string `json:"end"`
	Limit  int    `json:"limit"`
	Values []Value
}

Values represents a collection of values

type Waypoint

type Waypoint struct {
	Timestamp string `json:"timestamp"`
	Latitude  string `json:"latitude"`
	Longitude string `json:"longitude"`
	Elevation string `json:"elevation"`
}

Waypoint represents a waypoint

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL