Created
February 16, 2017 21:40
-
-
Save abachman/8ed954a0dde6405f1cd4e32c3831d722 to your computer and use it in GitHub Desktop.
Two ways to get at the IO data creation API
This file contains hidden or 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
export API_KEY= | |
export USER= | |
export DATA_URL=https://io.adafruit.com/api/v2/$USER/feeds/test-feed/data | |
# accept system timestamp | |
curl -X POST -H 'Content-Type: application/json' -H "X-AIO-Key: $API_KEY" \ | |
-d '{"datum":{"value": "10"}}' \ | |
$DATA_URL | |
# single | |
curl -X POST -H 'Content-Type: application/json' -H "X-AIO-Key: $API_KEY" \ | |
-d '{"datum":{"created_at": "2016-12-31T12:00:00Z", "value": "0.0"}}' \ | |
$DATA_URL | |
# batch | |
curl -X POST -H 'Content-Type: application/json' -H "X-AIO-Key: $API_KEY" \ | |
-d '{"data":[{"created_at": "2017-01-01T12:00:00Z", "value": "1"},{"created_at": "2017-01-02T12:00:00Z", "value": "2"}]}' \ | |
$DATA_URL/batch | |
# but not the future | |
curl -X POST -H 'Content-Type: application/json' -H "X-AIO-Key: $API_KEY" \ | |
-d '{"datum":{"created_at": "2018-12-31T12:00:00Z", "value": "10"}}' \ | |
$DATA_URL |
This file contains hidden or 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
require 'httparty' # gem install httparty | |
# | |
# Usage: | |
# KEY=$$$$ USER=$$$$ ruby streams.rb | |
# | |
KEY=ENV['KEY'] | |
USER=ENV['USER'] | |
URL='https://io.adafruit.com' | |
raise "KEY should be set" if KEY.nil? | |
raise "USER should be set" if USER.nil? | |
class StreamAPI | |
include HTTParty | |
base_uri URL | |
def initialize | |
@username = USER | |
@options = { headers: { 'X-AIO-Key' => KEY } } | |
end | |
def data(feed, limit=25) | |
self.class.get("/api/v2/#{ @username }/feeds/#{ feed }/data?limit=#{limit}", @options) | |
end | |
# add a single data point | |
def add_datum(feed, datum) | |
opts = @options.merge(query: { datum: datum }) | |
self.class.post("/api/v2/#{ @username }/feeds/#{ feed }/data", opts) | |
end | |
# add a collection of data points | |
def add_data(feed, data) | |
opts = @options.merge(query: { data: data }) | |
self.class.post("/api/v2/#{ @username }/feeds/#{ feed }/data/batch", opts) | |
end | |
end | |
if __FILE__ == $0 | |
# For example... | |
stream = StreamAPI.new | |
# basic insert | |
stream.add_datum('test-feed', {value: 1}) | |
# forced created timestamp | |
stream.add_datum('test-feed', {created_at: '2017-02-01T00:00:00Z', value: 2}) | |
stream.add_datum('test-feed', {created_at: '2017-02-02T00:00:00Z', value: 3}) | |
stream.add_datum('test-feed', {created_at: '2017-02-03T00:00:00Z', value: 4}) | |
stream.add_datum('test-feed', {created_at: '2017-02-04T00:00:00Z', value: 5}) | |
# batch with forced timestamp | |
stream.add_data('test-feed', [ | |
{created_at: '2017-02-05T00:00:00Z', value: 6}, | |
{created_at: '2017-02-06T00:00:00Z', value: 7}, | |
{created_at: '2017-02-07T00:00:00Z', value: 8}, | |
{created_at: '2017-02-08T00:00:00Z', value: 9}, | |
{created_at: '2017-02-09T00:00:00Z', value: 10}, | |
{created_at: '2017-02-10T00:00:00Z', value: 11}, | |
{created_at: '2017-02-11T00:00:00Z', value: 12}, | |
{created_at: '2017-02-12T00:00:00Z', value: 13}, | |
{created_at: '2017-02-13T00:00:00Z', value: 14} | |
]) | |
puts "Data:" | |
puts stream.data('test-feed', 15).parsed_response | |
puts "---------" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment