@@ -100,7 +100,6 @@ def __init__(
100
100
client : Client ,
101
101
name : str ,
102
102
primitive_func : PrimitiveFunctionType ,
103
- func : Callable ,
104
103
):
105
104
PrimitiveFunction .__init__ (self , endpoint , client , name , primitive_func )
106
105
@@ -158,21 +157,30 @@ def build_call(
158
157
159
158
160
159
class Registry :
161
- """Registry of local functions."""
160
+ """Registry of functions."""
162
161
163
- __slots__ = ("_functions " , "_endpoint " , "_client " )
162
+ __slots__ = ("functions " , "endpoint " , "client " )
164
163
165
- def __init__ (self , endpoint : str , client : Client ):
166
- """Initialize a local function registry.
164
+ def __init__ (
165
+ self , endpoint : str , api_key : str | None = None , api_url : str | None = None
166
+ ):
167
+ """Initialize a function registry.
167
168
168
169
Args:
169
170
endpoint: URL of the endpoint that the function is accessible from.
170
- client: Client for the Dispatch API. Used to dispatch calls to
171
- local functions.
171
+
172
+ api_key: Dispatch API key to use for authentication when
173
+ dispatching calls to functions. Uses the value of the
174
+ DISPATCH_API_KEY environment variable by default.
175
+
176
+ api_url: The URL of the Dispatch API to use when dispatching calls
177
+ to functions. Uses the value of the DISPATCH_API_URL environment
178
+ variable if set, otherwise defaults to the public Dispatch API
179
+ (DEFAULT_API_URL).
172
180
"""
173
- self ._functions : Dict [str , PrimitiveFunction ] = {}
174
- self ._endpoint = endpoint
175
- self ._client = client
181
+ self .functions : Dict [str , PrimitiveFunction ] = {}
182
+ self .endpoint = endpoint
183
+ self .client = Client ( api_key = api_key , api_url = api_url )
176
184
177
185
@overload
178
186
def function (self , func : Callable [P , Coroutine [Any , Any , T ]]) -> Function [P , T ]: ...
@@ -215,9 +223,7 @@ def primitive_func(input: Input) -> Output:
215
223
primitive_func .__qualname__ = f"{ name } _primitive"
216
224
primitive_func = durable (primitive_func )
217
225
218
- wrapped_func = Function [P , T ](
219
- self ._endpoint , self ._client , name , primitive_func , func
220
- )
226
+ wrapped_func = Function [P , T ](self .endpoint , self .client , name , primitive_func )
221
227
self ._register (name , wrapped_func )
222
228
return wrapped_func
223
229
@@ -228,20 +234,20 @@ def primitive_function(
228
234
name = primitive_func .__qualname__
229
235
logger .info ("registering primitive function: %s" , name )
230
236
wrapped_func = PrimitiveFunction (
231
- self ._endpoint , self ._client , name , primitive_func
237
+ self .endpoint , self .client , name , primitive_func
232
238
)
233
239
self ._register (name , wrapped_func )
234
240
return wrapped_func
235
241
236
242
def _register (self , name : str , wrapped_func : PrimitiveFunction ):
237
- if name in self ._functions :
243
+ if name in self .functions :
238
244
raise ValueError (f"function already registered with name '{ name } '" )
239
- self ._functions [name ] = wrapped_func
245
+ self .functions [name ] = wrapped_func
240
246
241
247
def set_client (self , client : Client ):
242
- """Set the Client instance used to dispatch calls to local functions."""
243
- self ._client = client
244
- for fn in self ._functions .values ():
248
+ """Set the Client instance used to dispatch calls to registered functions."""
249
+ self .client = client
250
+ for fn in self .functions .values ():
245
251
fn ._client = client
246
252
247
253
0 commit comments