Skip to content

Commit b853e13

Browse files
author
pavan-maddula
authored
Merge branch 'master' into error_codes
2 parents cd48346 + 77b8e64 commit b853e13

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

kwikapi/api.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,10 +504,11 @@ def handle_request(self, request):
504504

505505
# Serialize the response
506506
if request.fn.__func__.func_info['gives_stream']:
507-
result = self._wrap_stream(request, result)
507+
result = self._wrap_stream(request, result) if protocol.should_wrap() else result
508508
n, t = response.write(result, protocol, stream=True)
509509
else:
510-
n, t = response.write(dict(success=True, result=result), protocol)
510+
result = dict(success=True, result=result) if protocol.should_wrap() else result
511+
n, t = response.write(result, protocol)
511512

512513
request.log.info('kwikapi.handle_request',
513514
function=rinfo.function, namespace=rinfo.namespace,

kwikapi/protocols.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ def get_record_separator(self):
3535
def get_mime_type(self):
3636
pass
3737

38+
@staticmethod
39+
def should_wrap():
40+
'''
41+
While returning the response the,
42+
kwikapi will wrap the response as -
43+
{success: value, result: value}
44+
45+
This method, can used in above situation,
46+
if no wrapping is required,
47+
override this method in the protocol class.
48+
'''
49+
return True
50+
3851
class JsonProtocol(BaseProtocol):
3952

4053
@staticmethod
@@ -118,6 +131,35 @@ def get_record_separator(cls):
118131
def get_mime_type():
119132
return 'application/pickle'
120133

134+
class RawProtocol(BaseProtocol):
135+
@staticmethod
136+
def get_name():
137+
return 'raw'
138+
139+
@staticmethod
140+
def serialize(data):
141+
return data
142+
143+
@staticmethod
144+
def deserialize(data):
145+
return data
146+
147+
@classmethod
148+
def deserialize_stream(cls, data):
149+
return data
150+
151+
@classmethod
152+
def get_record_separator(cls):
153+
return b''
154+
155+
@staticmethod
156+
def get_mime_type():
157+
return 'application/octet-stream'
158+
159+
@classmethod
160+
def should_wrap(cls):
161+
return False
162+
121163
class NumpyProtocol(BaseProtocol):
122164

123165
@staticmethod
@@ -180,6 +222,7 @@ def get_mime_type():
180222
MessagePackProtocol,
181223
PickleProtocol,
182224
NumpyProtocol,
225+
RawProtocol,
183226
])
184227

185228
DEFAULT_PROTOCOL = JsonProtocol.get_name()

0 commit comments

Comments
 (0)