diff --git a/kwikapi/api.py b/kwikapi/api.py index d5bd079..4b0f101 100644 --- a/kwikapi/api.py +++ b/kwikapi/api.py @@ -504,10 +504,11 @@ def handle_request(self, request): # Serialize the response if request.fn.__func__.func_info['gives_stream']: - result = self._wrap_stream(request, result) + result = self._wrap_stream(request, result) if protocol.should_wrap() else result n, t = response.write(result, protocol, stream=True) else: - n, t = response.write(dict(success=True, result=result), protocol) + result = dict(success=True, result=result) if protocol.should_wrap() else result + n, t = response.write(result, protocol) request.log.info('kwikapi.handle_request', function=rinfo.function, namespace=rinfo.namespace, diff --git a/kwikapi/protocols.py b/kwikapi/protocols.py index f5953ab..b50ca26 100644 --- a/kwikapi/protocols.py +++ b/kwikapi/protocols.py @@ -35,6 +35,19 @@ def get_record_separator(self): def get_mime_type(self): pass + @staticmethod + def should_wrap(): + ''' + While returning the response the, + kwikapi will wrap the response as - + {success: value, result: value} + + This method, can used in above situation, + if no wrapping is required, + override this method in the protocol class. + ''' + return True + class JsonProtocol(BaseProtocol): @staticmethod @@ -118,6 +131,35 @@ def get_record_separator(cls): def get_mime_type(): return 'application/pickle' +class RawProtocol(BaseProtocol): + @staticmethod + def get_name(): + return 'raw' + + @staticmethod + def serialize(data): + return data + + @staticmethod + def deserialize(data): + return data + + @classmethod + def deserialize_stream(cls, data): + return data + + @classmethod + def get_record_separator(cls): + return b'' + + @staticmethod + def get_mime_type(): + return 'application/octet-stream' + + @classmethod + def should_wrap(cls): + return False + class NumpyProtocol(BaseProtocol): @staticmethod @@ -180,6 +222,7 @@ def get_mime_type(): MessagePackProtocol, PickleProtocol, NumpyProtocol, + RawProtocol, ]) DEFAULT_PROTOCOL = JsonProtocol.get_name()