From 8fa2d5d92c554f639532ab7f74eacb0b9bc18701 Mon Sep 17 00:00:00 2001 From: Sun Ning Date: Wed, 25 Apr 2012 20:53:46 +0800 Subject: [PATCH] some enhancement and bugfix --- setup.py | 2 +- slacker/client.py | 7 +++++-- slacker/protocol.py | 17 ++++++++++++----- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/setup.py b/setup.py index 0d7b994..d0050e3 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ long_desc=open('README.rst','r').read() setup(name="slacker-python", - version="0.1.0", + version="0.1.1", author="Sun Ning", author_email="sunng@about.me", description="python client of slacker RPC", diff --git a/slacker/client.py b/slacker/client.py index 434a4e0..f6a64f6 100644 --- a/slacker/client.py +++ b/slacker/client.py @@ -66,8 +66,11 @@ def call(self, fname, args): cb = conn.send(req) result = cb.get() if isinstance(result, SlackerResponse): - result.desrialize() - return result.body + if result.code == PROTOCOL_RESULT_CODE_SUCCESS: + result.desrialize() + return result.body + else: + raise RuntimeError("Error code: "+ str(result.code)) else: code = result.code raise RuntimeError("Error code: " + str(code)) diff --git a/slacker/protocol.py b/slacker/protocol.py index b1d308b..11c6421 100644 --- a/slacker/protocol.py +++ b/slacker/protocol.py @@ -11,6 +11,8 @@ PROTOCOL_CONTENT_TYPE_JSON = 1 PROTOCOL_CONTENT_TYPE_CLJ = 2 +PROTOCOL_RESULT_CODE_SUCCESS = 0 + class SlackerRequest(object): def __init__(self, ct, fname, args): self.ct = ct @@ -32,12 +34,14 @@ def __init__(self, ct, code, body): self.body = body def serialize(self): - serializer = json if self.ct == PROTOCOL_CONTENT_TYPE_JSON else clj - self.body = serializer.dumps(self.body) + if self.body is not None: + serializer = json if self.ct == PROTOCOL_CONTENT_TYPE_JSON else clj + self.body = serializer.dumps(self.body) def desrialize(self): - serializer = json if self.ct == PROTOCOL_CONTENT_TYPE_JSON else clj - self.body = serializer.loads(self.body) + if self.body is not None: + serializer = json if self.ct == PROTOCOL_CONTENT_TYPE_JSON else clj + self.body = serializer.loads(self.body) class SlackerError(object): def __init__(self, ct, code): @@ -76,7 +80,10 @@ def readResponse(fd): ## read body l = struct.unpack(">I", fd.recv(4))[0] - body = struct.unpack(str(l)+"s", fd.recv(l))[0] + if l > 0: + body = struct.unpack(str(l)+"s", fd.recv(l))[0] + else: + body = None return SlackerResponse(ct, rc, body)