Skip to content
This repository has been archived by the owner on Aug 4, 2020. It is now read-only.

Commit

Permalink
Merge pull request #9 from smulube/datastream_timestamps
Browse files Browse the repository at this point in the history
Allow setting datastream timestamps on create or update
  • Loading branch information
errordeveloper committed Jun 12, 2013
2 parents bbc7418 + cc78eff commit 4757960
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
2 changes: 1 addition & 1 deletion setup.py
@@ -1,7 +1,7 @@
from setuptools import setup

setup(name='xively-python',
version='0.1.0-rc0',
version='0.1.0-rc1',
description="Xively API wrapper",
long_description="This is the official pythonic wrapper library for the Xively V2 API.",
url='http://github.com/xively/xively-python',
Expand Down
48 changes: 47 additions & 1 deletion tests.py
Expand Up @@ -314,8 +314,16 @@ def setUp(self):
self.feed = self._create_feed(id=7021, title="Rother")

def test_create_datastream(self):
datastream = xively.Datastream(id="energy")
datastream = xively.Datastream(id="energy", current_value="123")
self.assertEqual(datastream.id, "energy")
self.assertEqual(datastream.current_value, "123")

def test_create_datastream_with_timestamp(self):
now = datetime.now()
datastream = xively.Datastream(id="energy", current_value="123", at=now)
self.assertEqual(datastream.id, "energy")
self.assertEqual(datastream.current_value, "123")
self.assertEqual(datastream.at, now)

def test_update_datastream(self):
datastream = self._create_datastream(id="energy", current_value=211)
Expand All @@ -327,6 +335,19 @@ def test_update_datastream(self):
payload = json.loads(self.request.call_args[1]['data'])
self.assertEqual(payload['current_value'], 294)

def test_update_datastream_with_timestamp(self):
now = datetime.now()
datastream = self._create_datastream(id="energy", current_value=211)
datastream.current_value = 294
datastream.at = now
datastream.update()
self.assertEqual(
self.request.call_args[0],
('PUT', 'http://api.xively.com/v2/feeds/7021/datastreams/energy'))
payload = json.loads(self.request.call_args[1]['data'])
self.assertEqual(payload['current_value'], 294)
self.assertEqual(payload['at'], now.isoformat() + 'Z')

def test_update_datastream_fields(self):
datastream = self._create_datastream(id="energy", current_value=211)
datastream.current_value = 294
Expand Down Expand Up @@ -360,6 +381,21 @@ def test_create_datastream(self):
self.assertEqual(datastream.current_value, 34000)
self.assertEqual(datastream.unit.symbol, 'l/s')

def test_create_datastream_with_timestamp(self):
now = datetime.now()
datastream = self.feed.datastreams.create(
id="flow",
current_value=34000,
unit=xively.Unit(symbol='l/s'),
at=now)
self.assertEqual(
self.request.call_args[0],
('POST', 'http://api.xively.com/v2/feeds/7021/datastreams'))
self.assertEqual(datastream.id, "flow")
self.assertEqual(datastream.current_value, 34000)
self.assertEqual(datastream.unit.symbol, 'l/s')
self.assertEqual(datastream.at, now)

def test_update_datastream(self):
self.feed.datastreams.update('energy', current_value=294)
self.assertEqual(
Expand All @@ -368,6 +404,16 @@ def test_update_datastream(self):
payload = json.loads(self.request.call_args[1]['data'])
self.assertEqual(payload['current_value'], 294)

def test_update_datastream_with_timestamp(self):
now = datetime.now()
self.feed.datastreams.update('energy', current_value=294, at=now)
self.assertEqual(
self.request.call_args[0],
('PUT', 'http://api.xively.com/v2/feeds/7021/datastreams/energy'))
payload = json.loads(self.request.call_args[1]['data'])
self.assertEqual(payload['current_value'], 294)
self.assertEqual(payload['at'], now.isoformat() + 'Z')

def test_list_datastreams(self):
self.response.raw = BytesIO(fixtures.GET_FEED_JSON)
datastreams = self.feed.datastreams.list()
Expand Down
2 changes: 1 addition & 1 deletion xively/__init__.py
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-

__title__ = 'xively-python'
__version__ = '0.1.0'
__version__ = '0.1.0-rc1'

__all__ = ['Client', 'XivelyAPIClient', 'Datapoint', 'Datastream', 'Feed', 'Key',
'Location', 'Permission', 'Resource', 'Trigger', 'Unit', 'Waypoint']
Expand Down
6 changes: 4 additions & 2 deletions xively/managers.py
Expand Up @@ -386,7 +386,7 @@ def _datastreams(self):
return self.parent._data.setdefault('datastreams', [])

def create(self, id, current_value=None, tags=None, unit=None,
min_value=None, max_value=None):
min_value=None, max_value=None, at=None):
"""Creates a new datastream on a feed.
:param id: The ID of the datastream
Expand All @@ -395,6 +395,7 @@ def create(self, id, current_value=None, tags=None, unit=None,
:param unit: The :class:`.Unit` for this datastream
:param min_value: The minimum value since the last reset
:param max_value: The maximum value since the last reset
:param at: The timestamp of the current value
:returns: A :class:`.Datastream` object
"""
Expand All @@ -404,7 +405,8 @@ def create(self, id, current_value=None, tags=None, unit=None,
tags=tags,
unit=unit,
min_value=min_value,
max_value=max_value)
max_value=max_value,
at=at)
datastream = self._coerce_datastream(datastream_data)
data = {
'version': self.parent.version,
Expand Down
6 changes: 4 additions & 2 deletions xively/models.py
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-

__title__ = 'xively-python'
__version__ = '0.1.0'
__version__ = '0.1.0-rc1'


__all__ = ['Feed', 'Datastream', 'Datapoint', 'Location', 'Waypoint',
Expand Down Expand Up @@ -156,14 +156,15 @@ class Datastream(Base):
:param min_value: The minimum value since the last reset
:param max_value: The maximum value since the last reset
:param current_value: The current value of the datastream
:param at: The timestamp of the current value
:param datapoints: A collection of timestamped values
"""

_datapoints_manager = None

def __init__(self, id, tags=None, unit=None, min_value=None,
max_value=None, current_value=None, datapoints=None):
max_value=None, current_value=None, datapoints=None, at=None):
"""Creates a new datastream object locally."""
self._data = {
'id': id,
Expand All @@ -172,6 +173,7 @@ def __init__(self, id, tags=None, unit=None, min_value=None,
'min_value': min_value,
'max_value': max_value,
'current_value': current_value,
'at': at,
}
self.datapoints = datapoints or []

Expand Down

0 comments on commit 4757960

Please sign in to comment.