Skip to content

Commit

Permalink
Merge branch 'master' into error_codes
Browse files Browse the repository at this point in the history
  • Loading branch information
pavan-maddula authored Jan 23, 2019
2 parents cd48346 + 77b8e64 commit b853e13
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
5 changes: 3 additions & 2 deletions kwikapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
43 changes: 43 additions & 0 deletions kwikapi/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -180,6 +222,7 @@ def get_mime_type():
MessagePackProtocol,
PickleProtocol,
NumpyProtocol,
RawProtocol,
])

DEFAULT_PROTOCOL = JsonProtocol.get_name()

0 comments on commit b853e13

Please sign in to comment.