6
6
from typing import Callable
7
7
8
8
import yaml
9
- from pydantic import BaseModel
10
9
11
10
from csfunctions import ErrorResponse , Event , Request , WorkloadResponse
12
11
from csfunctions .actions import ActionUnion
13
12
from csfunctions .config import ConfigModel , FunctionModel
13
+ from csfunctions .events import EventData
14
14
from csfunctions .objects import BaseObject
15
15
from csfunctions .response import ResponseUnion
16
16
from csfunctions .service import Service
@@ -31,7 +31,7 @@ def _get_function(function_name: str, function_dir: str) -> FunctionModel:
31
31
config = _load_config (function_dir )
32
32
func = next (func for func in config .functions if func .name == function_name )
33
33
if not func :
34
- raise ValueError (f"Could not find function with name { function_name } in the environment.yaml." )
34
+ raise ValueError (f"Could not find function with name { function_name } in the environment.yaml." )
35
35
return func
36
36
37
37
@@ -53,7 +53,7 @@ def link_objects(event: Event):
53
53
e.g. document.part
54
54
"""
55
55
data = getattr (event , "data" , None )
56
- if not isinstance (data , BaseModel ):
56
+ if data is None or not isinstance (data , EventData ): # type: ignore # MyPy doesn't like PEP604
57
57
return
58
58
59
59
# we expect all objects to be passed in Event.data
@@ -81,24 +81,26 @@ def execute(function_name: str, request_body: str, function_dir: str = "src") ->
81
81
try :
82
82
request = Request (** json .loads (request_body ))
83
83
link_objects (request .event )
84
+
84
85
function_callback = get_function_callable (function_name , function_dir )
85
- service = Service (str (request .metadata .service_url ), request .metadata .service_token )
86
+ service = Service (
87
+ str (request .metadata .service_url ) if request .metadata .service_url else None , request .metadata .service_token
88
+ )
86
89
87
90
response = function_callback (request .metadata , request .event , service )
88
91
89
92
if response is None :
90
93
return ""
91
94
92
- if isinstance (response , ActionUnion ):
95
+ if isinstance (response , ActionUnion ): # type: ignore # MyPy doesn't like PEP604
93
96
# wrap returned Actions into a WorkloadResponse
94
97
response = WorkloadResponse (actions = [response ])
95
- elif isinstance (response , list ) and all (isinstance (o , ActionUnion ) for o in response ):
98
+ elif isinstance (response , list ) and all (isinstance (o , ActionUnion ) for o in response ): # type: ignore # MyPy doesn't like PEP604
96
99
# wrap list of Actions into a WorkloadResponse
97
100
response = WorkloadResponse (actions = response )
98
101
99
- if not isinstance (
100
- response , ResponseUnion
101
- ): # need to check for ResponseUnion instead of Response, because isinstance doesn't work with annotated unions
102
+ if not isinstance (response , ResponseUnion ): # type: ignore # MyPy doesn't like PEP604
103
+ # need to check for ResponseUnion instead of Response, because isinstance doesn't work with annotated unions
102
104
raise ValueError ("Function needs to return a Response object or None." )
103
105
104
106
# make sure the event_id is filled out correctly
0 commit comments