Add fixes for basic stream api support

Fixed api typo 'distinquished'
Added proper json serialization for datetime and timerange

Change-Id: I6af8bb15b5e9e65e7c6965cd80e1483b3df04b99
This commit is contained in:
Monsyne Dragon 2014-12-02 15:54:15 +00:00
parent 7e0c4dd829
commit e8eaf34662
5 changed files with 50 additions and 9 deletions

5
.gitreview Normal file
View File

@ -0,0 +1,5 @@
[gerrit]
host=review.openstack.org
port=29418
project=stackforge/stacktach-quincy.git

35
quincy/jsonutil.py Normal file
View File

@ -0,0 +1,35 @@
import json
import datetime
import timex
class ObjectEncoder(json.JSONEncoder):
ENCODE_MAP = {datetime.datetime: "datetime",
timex.TimeRange: "timex.TimeRange",
timex.Timestamp: "timex.Timestamp"}
def default(self, obj):
if type(obj) in self.ENCODE_MAP:
typename = self.ENCODE_MAP[type(obj)]
encoder = getattr(self, '_encode_' + typename.replace('.', '_').lower())
return encoder(obj, typename)
return super(ObjectEncoder, self).default(obj)
def _encode_datetime(self, obj, name):
return {'__type__' : name,
'datetime': obj.isoformat()}
def _encode_timex_timestamp(self, obj, name):
return {'__type__' : name,
'timestamp': obj.timestamp.isoformat()}
def _encode_timex_timerange(self, obj, name):
return {'__type__' : name,
'begin': obj.begin.isoformat(),
'end': obj.end.isoformat()}
def dumps(obj, **kw):
kw['cls'] = ObjectEncoder
return json.dumps(obj, **kw)

View File

@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from quincy import jsonutil
from dateutil import parser
@ -31,7 +31,7 @@ class StreamCollection(common.FalconBase):
# younger_than
# state
# trigger_name
# distinquishing_traits - find stream by dtrait values.
# distinguishing_traits - find stream by dtrait values.
#
# Actions on a Stream:
# details - get full details on stream (including events &
@ -42,7 +42,7 @@ class StreamCollection(common.FalconBase):
younger_than = req.get_param('younger_than')
state = req.get_param('state')
trigger = req.get_param('trigger_name')
traits = req.get_param('distinquishing_traits')
traits = req.get_param('distinguishing_traits')
if older_than:
older_than = parser.parse(older_than)
@ -54,15 +54,15 @@ class StreamCollection(common.FalconBase):
younger_than=younger_than,
state=state,
trigger_name=trigger,
distinquishing_traits=traits)
resp.body = json.dumps(streams)
distinguishing_traits=traits)
resp.body = jsonutil.dumps(streams)
class StreamItem(common.FalconBase):
def on_get(self, req, resp, stream_id, action=None):
details = req.get_param('details')
streams = self.impl.get_stream(stream_id, details)
resp.body = json.dumps(streams)
resp.body = jsonutil.dumps(streams)
def on_delete(self, req, resp, stream_id):
self.impl.delete_stream(stream_id)

View File

@ -23,14 +23,14 @@ class Stream(object):
self.stream_id = stream_id
self.trigger_name = trigger_name
self.state = state
self.distinquishing_traits = []
self.distinguishing_traits = []
def to_dict(self):
return {"last_updated": str(self.last_updated),
"stream_id": self.stream_id,
"trigger_name": self.trigger_name,
"state": self.state,
"distinquishing_traits": self.distinquishing_traits}
"distinguishing_traits": self.distinguishing_traits}
class Impl(object):
@ -44,7 +44,7 @@ class Impl(object):
younger_than
state
trigger_name
distinquishing_traits
distinguishing_traits
"""
x = [Stream(str(uuid.uuid4()), "EOD-Exists", "Collecting"),
Stream(str(uuid.uuid4()), "EOD-Exists", "Error"),

View File

@ -1,3 +1,4 @@
python-dateutil
falcon
simport >= 0.0.dev0
timex