diff --git a/setup.py b/setup.py index 973c48e..badd51d 100644 --- a/setup.py +++ b/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', diff --git a/tests.py b/tests.py index 2a7a03f..4c1706d 100644 --- a/tests.py +++ b/tests.py @@ -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) @@ -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 @@ -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( @@ -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() diff --git a/xively/__init__.py b/xively/__init__.py index a6f6cae..5946fe4 100644 --- a/xively/__init__.py +++ b/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'] diff --git a/xively/managers.py b/xively/managers.py index bae4c71..0ad77ae 100644 --- a/xively/managers.py +++ b/xively/managers.py @@ -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 @@ -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 """ @@ -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, diff --git a/xively/models.py b/xively/models.py index 33079ce..1285bb1 100644 --- a/xively/models.py +++ b/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', @@ -156,6 +156,7 @@ 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 """ @@ -163,7 +164,7 @@ class Datastream(Base): _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, @@ -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 []