@@ -43,6 +43,18 @@ def load(self):
4343 print ("PIPE_INIT_COMPLETE" )
4444 sys .stdout .flush ()
4545
46+ def apply (self , payload ):
47+ try :
48+ if self .load_result :
49+ apply_result = self .apply_func (payload , self .load_result )
50+ else :
51+ apply_result = self .apply_func (payload )
52+ response_obj = self .format_response (apply_result )
53+ return response_obj
54+ except Exception as e :
55+ response_obj = self .create_exception (e )
56+ return response_obj
57+
4658 def format_data (self , request ):
4759 if request ["content_type" ] in ["text" , "json" ]:
4860 data = request ["data" ]
@@ -114,27 +126,15 @@ def create_exception(self, exception, loading_exception=False):
114126 return response
115127
116128 def process_loop (self ):
117- response_obj = ""
118129 for line in sys .stdin :
119- try :
120- request = json .loads (line )
121- formatted_input = self .format_data (request )
122- if self .load_result :
123- apply_result = self .apply_func (formatted_input , self .load_result )
124- else :
125- apply_result = self .apply_func (formatted_input )
126- response_obj = self .format_response (apply_result )
127- except Exception as e :
128- response_obj = self .create_exception (e )
129- finally :
130- self .write_to_pipe (response_obj )
130+ request = json .loads (line )
131+ formatted_input = self .format_data (request )
132+ result = self .apply (formatted_input )
133+ self .write_to_pipe (result )
131134
132135 def process_local (self , local_payload , pprint ):
133- if self .load_result :
134- apply_result = self .apply_func (local_payload , self .load_result )
135- else :
136- apply_result = self .apply_func (local_payload )
137- pprint (self .format_response (apply_result ))
136+ result = self .apply (local_payload )
137+ self .write_to_pipe (result , pprint = pprint )
138138
139139 def init (self , local_payload = None , pprint = print ):
140140 self .load ()
0 commit comments