Skip to content
This repository has been archived by the owner on Feb 15, 2018. It is now read-only.

Commit

Permalink
Add live support
Browse files Browse the repository at this point in the history
  • Loading branch information
madninja committed Sep 19, 2016
1 parent eb30828 commit 1e6f536
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 9 deletions.
8 changes: 5 additions & 3 deletions helium_commander/commands/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from itertools import islice
from helium_commander import Client, DataPoint
from helium_commander import JSONParamType
from contextlib import closing

pass_client = click.make_pass_decorator(Client)

Expand Down Expand Up @@ -49,10 +50,11 @@ def _live(client, id, **kwargs):
mac = kwargs.pop('mac', False)
resource = cls.lookup(client, id, mac=mac)
timeseries = resource.timeseries(**kwargs)
mapping = cls.display_map(client)
mapping = DataPoint.display_map(client)
with cls.display_writer(client, mapping, **kwargs) as writer:
for data_point in timeseries.live():
writer.write_resources([data_point])
with closing(timeseries.live()) as live:
for data_point in live:
writer.write_resources([data_point], mapping)

return group

Expand Down
1 change: 1 addition & 0 deletions helium_commander/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ def display_map(cls, client):
return dict

DataPoint.display_map = classmethod(display_map)
Timeseries # Empty reference to make clear we re-export
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
author = 'Helium'
author_email = 'hello@helium.com'
install_requires = [
'helium-python>=0.0.9',
'helium-python>=0.1.0',
'future>=0.15',
'dpath>=1.4',
'futures>=3.0',
Expand Down
175 changes: 175 additions & 0 deletions tests/cassettes/tests.commands.test_sensor.test_live.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
{
"http_interactions": [
{
"recorded_at": "2016-09-19T23:23:30",
"request": {
"body": {
"encoding": "utf-8",
"string": "{\"data\": {\"type\": \"sensor\", \"attributes\": {\"name\": \"test\"}}}"
},
"headers": {
"Accept": "application/json",
"Accept-Charset": "utf-8",
"Accept-Encoding": "gzip, deflate",
"Authorization": "<AUTH_TOKEN>",
"Connection": "keep-alive",
"Content-Length": "60",
"Content-Type": "application/json",
"User-Agent": "helium-python/0.1.0"
},
"method": "POST",
"uri": "https://api.helium.com/v1/sensor"
},
"response": {
"body": {
"base64_string": "H4sIAAAAAAAEA5WQQW7EIAxF7+J1qAghdMg5ZtXRLBziqpEIicBZVKPcvaZt2mVVCcnY8P9/8IAJGWF4ADLnedyZSu0SLgQDSMdwNJApIs9rKm/z9nm+EOOpPOs8icJZ7foetbLeGWUv4aLGgL1qg59evTHGdx00wO9b9f+xOSSEIi2UuMZ/WaY9RplHHCn+Tm/3evk/YTWl6gNmQayuDSwYzm3IhEwV3ujWKe1V66+mG2R1+sl5+9zaF2GOWFgVonQKtzWz/Mbt3sC+CfKfHsL9/fBCqawZjuMD+4qeyYEBAAA=",
"encoding": null,
"string": ""
},
"headers": {
"Access-Control-Allow-Headers": "Origin, Content-Type, Accept, Authorization",
"Access-Control-Allow-Origin": "*",
"Airship-Quip": "javascript doesn't have integers",
"Airship-Trace": "b13,b12,b11,b10,b09,b08,b07,b06,b05,b04,b03,c03,c04,d04,e05,e06,f06,f07,g07,g08,h10,i12,l13,m16,n16,n11,p11",
"Content-Encoding": "gzip",
"Content-Type": "application/json",
"Date": "Mon, 19 Sep 2016 23:23:30 GMT",
"Location": "/v1/sensor/640655a0-4962-48c8-bca5-1c9df9222933",
"Server": "Warp/3.2.7",
"Transfer-Encoding": "chunked"
},
"status": {
"code": 201,
"message": "Created"
},
"url": "https://api.helium.com/v1/sensor"
}
},
{
"recorded_at": "2016-09-19T23:23:30",
"request": {
"body": {
"encoding": "utf-8",
"string": ""
},
"headers": {
"Accept": "application/json",
"Accept-Charset": "utf-8",
"Accept-Encoding": "gzip, deflate",
"Authorization": "<AUTH_TOKEN>",
"Connection": "keep-alive",
"Content-Type": "application/json",
"User-Agent": "helium-python/0.1.0"
},
"method": "GET",
"uri": "https://api.helium.com/v1/sensor"
},
"response": {
"body": {
"base64_string": "H4sIAAAA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8BvRZPJGsUAAA=",
"encoding": null,
"string": ""
},
"headers": {
"Access-Control-Allow-Headers": "Origin, Content-Type, Accept, Authorization",
"Access-Control-Allow-Origin": "*",
"Airship-Quip": "WARNING: ulimit -n is 1024",
"Airship-Trace": "b13,b12,b11,b10,b09,b08,b07,b06,b05,b04,b03,c03,c04,d04,e05,e06,f06,f07,g07,g08,h10,i12,l13,m16,n16,o16,o17,o18",
"Content-Encoding": "gzip",
"Content-Type": "application/json",
"Date": "Mon, 19 Sep 2016 23:23:30 GMT",
"Server": "Warp/3.2.7",
"Transfer-Encoding": "chunked"
},
"status": {
"code": 200,
"message": "OK"
},
"url": "https://api.helium.com/v1/sensor"
}
},
{
"recorded_at": "2016-09-19T23:23:30",
"request": {
"body": {
"encoding": "utf-8",
"string": ""
},
"headers": {
"Accept": "text/event-stream",
"Accept-Charset": "utf-8",
"Accept-Encoding": "gzip, deflate",
"Authorization": "<AUTH_TOKEN>",
"Connection": "keep-alive",
"Content-Type": "application/json",
"User-Agent": "helium-python/0.1.0"
},
"method": "GET",
"uri": "https://api.helium.com/v1/sensor/640655a0-4962-48c8-bca5-1c9df9222933/timeseries/live"
},
"response": {
"body": {
"encoding": "utf-8",
"string": "event: sensor\ndata: {\"data\":{\"attributes\":{\"value\":42,\"timestamp\":\"2016-09-18T23:57:49.477274Z\",\"port\":\"test_post\"},\"relationships\":{\"sensor\":{\"data\":{\"id\":\"_\",\"type\":\"sensor\"}}},\"id\":\"8646ef8b-6458-4267-90ce-849e78fb38c9\",\"meta\":{\"created\":\"2016-09-18T23:57:49.704603Z\"},\"type\":\"data-point\"}}\n\n\n\nevent: sensor\ndata: {\"data\":{\"attributes\":{\"value\":43,\"timestamp\":\"2016-09-18T23:57:59.245238Z\",\"port\":\"test_post\"},\"relationships\":{\"sensor\":{\"data\":{\"id\":\"_\",\"type\":\"sensor\"}}},\"id\":\"8ff950ee-4749-41b8-85a2-9b030a52637c\",\"meta\":{\"created\":\"2016-09-18T23:57:59.251219Z\"},\"type\":\"data-point\"}}\n\n"
},
"headers": {
"Access-Control-Allow-Headers": "Origin, Content-Type, Accept, Authorization",
"Access-Control-Allow-Origin": "*",
"Airship-Quip": "javascript doesn't have integers",
"Airship-Trace": "b13,b12,b11,b10,b09,b08,b07,b06,b05,b04,b03,c03,c04,d04,e05,e06,f06,f07,g07,g08,h10,i12,l13,m16,n16,o16,o17,o18",
"Content-Encoding": "utf-8",
"Content-Type": "text/event-stream",
"Date": "Mon, 19 Sep 2016 23:23:30 GMT",
"Server": "Warp/3.2.7",
"Transfer-Encoding": "chunked"
},
"status": {
"code": 200,
"message": "OK"
},
"url": "https://api.helium.com/v1/sensor/640655a0-4962-48c8-bca5-1c9df9222933/timeseries/live"
}
},
{
"recorded_at": "2016-09-19T23:23:31",
"request": {
"body": {
"encoding": "utf-8",
"string": ""
},
"headers": {
"Accept": "application/json",
"Accept-Charset": "utf-8",
"Accept-Encoding": "gzip, deflate",
"Authorization": "<AUTH_TOKEN>",
"Connection": "keep-alive",
"Content-Length": "0",
"Content-Type": "application/json",
"User-Agent": "helium-python/0.1.0"
},
"method": "DELETE",
"uri": "https://api.helium.com/v1/sensor/640655a0-4962-48c8-bca5-1c9df9222933"
},
"response": {
"body": {
"encoding": null,
"string": ""
},
"headers": {
"Access-Control-Allow-Headers": "Origin, Content-Type, Accept, Authorization",
"Access-Control-Allow-Origin": "*",
"Airship-Quip": "firm pat on the back",
"Airship-Trace": "b13,b12,b11,b10,b09,b08,b07,b06,b05,b04,b03,c03,c04,d04,e05,e06,f06,f07,g07,g08,h10,i12,l13,m16,m20,o20",
"Date": "Mon, 19 Sep 2016 23:23:30 GMT",
"Server": "Warp/3.2.7"
},
"status": {
"code": 204,
"message": "No Content"
},
"url": "https://api.helium.com/v1/sensor/640655a0-4962-48c8-bca5-1c9df9222933"
}
}
],
"recorded_with": "betamax/0.8.0"
}
4 changes: 2 additions & 2 deletions tests/commands/test_organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ def test_update(client, authorized_organization):
current_name = authorized_organization.name

output = cli_run(client, ['organization', 'update',
'--name', 'test_update_name'])
'--name', 'test_update_name'])
assert 'test_update_name' in output

output = cli_run(client, ['organization', 'update',
'--name', current_name])
'--name', current_name])
assert current_name in output
18 changes: 15 additions & 3 deletions tests/commands/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def test_create_delete(client):


def test_timeseries(client, tmp_sensor):
output = cli_run(client, ['sensor', 'timeseries',
'list', tmp_sensor.short_id])
output = cli_run(client, ['sensor', 'timeseries', 'list',
tmp_sensor.short_id])
assert output is not None

output = cli_run(client, ['sensor', 'timeseries', 'post',
Expand All @@ -36,10 +36,22 @@ def test_timeseries(client, tmp_sensor):
with runner.isolated_filesystem():
file = path.local('test.csv')
cli_run(client, ['--format', 'csv', '--output', str(file),
'sensor', 'timeseries', 'list', tmp_sensor.short_id,
'sensor', 'timeseries', 'list',
tmp_sensor.short_id,
'--count', '100'],
runner=runner)

output = file.read()
assert 'test_post' in output
assert '22' in output


def test_live(client, tmp_sensor):
# We're faking the cassette for a live session pretty hard
# here. The cassette was manually edited to reflect the
# event/text-stream data in a single request to work around
# betamax's problems with dealing with live sockets.
output = cli_run(client, ['--format', 'csv',
'sensor', 'timeseries', 'live',
tmp_sensor.short_id])
assert output.count('test_post') == 2

0 comments on commit 1e6f536

Please sign in to comment.