17
17
import ujson
18
18
import os
19
19
import traceback
20
+ import types
20
21
21
22
from fdk import context
22
23
from fdk import errors
23
24
from fdk import headers
24
25
from fdk import response
25
26
26
27
28
+ def handle_callable (ctx , handle_func , data = None ,
29
+ loop : asyncio .AbstractEventLoop = None ):
30
+ r = handle_func (ctx , data = data , loop = loop )
31
+
32
+ if isinstance (r , types .CoroutineType ):
33
+ print ("function appeared to be a coroutine, awaiting..." ,
34
+ file = sys .stderr , flush = True )
35
+ return loop .run_until_complete (r )
36
+
37
+ return r
38
+
39
+
27
40
def from_request (handle_func , incoming_request , loop = None ):
28
41
print ("request parsed" , file = sys .stderr , flush = True )
29
42
json_headers = headers .GoLikeHeaders (
@@ -39,11 +52,10 @@ def from_request(handle_func, incoming_request, loop=None):
39
52
config = os .environ , headers = json_headers )
40
53
41
54
print ("context allocated" , file = sys .stderr , flush = True )
42
-
43
55
print ("starting the function" , file = sys .stderr , flush = True )
44
56
print (incoming_request .get ("body" ), file = sys .stderr , flush = True )
45
- response_data = handle_func (
46
- ctx , data = incoming_request .get ("body" ), loop = loop )
57
+ response_data = handle_callable (
58
+ ctx , handle_func , data = incoming_request .get ("body" ), loop = loop )
47
59
48
60
if isinstance (response_data , response .RawResponse ):
49
61
return response_data
0 commit comments