1010 def my_function():
1111 return "Hello World!"
1212
13- @dispatch.entrypoint
13+ @dispatch.function
1414 def entrypoint():
1515 my_function()
1616
1717 def handler(event, context):
18- dispatch.handle(event, context)
18+ dispatch.handle(event, context, entrypoint="entrypoint" )
1919 """
2020
2121import base64
22- import logging
2322import json
23+ import logging
24+ from typing import Optional
25+
26+ from awslambdaric .lambda_context import LambdaContext
2427
2528from dispatch .function import Registry
2629from dispatch .proto import Input
@@ -33,43 +36,52 @@ def handler(event, context):
3336class Dispatch (Registry ):
3437 def __init__ (
3538 self ,
36- api_key : str | None = None ,
37- api_url : str | None = None ,
39+ api_key : Optional [ str ] = None ,
40+ api_url : Optional [ str ] = None ,
3841 ):
3942 """Initializes a Dispatch Lambda handler.
4043
4144 Args:
42- api_key: Dispatch API key to use for authentication. Uses the value of
43- the DISPATCH_API_KEY environment variable by default.
45+ api_key: Dispatch API key to use for authentication. Uses the value
46+ of the DISPATCH_API_KEY environment variable by default.
4447
4548 api_url: The URL of the Dispatch API to use. Uses the value of the
4649 DISPATCH_API_URL environment variable if set, otherwise
4750 defaults to the public Dispatch API (DEFAULT_API_URL).
4851
4952 """
5053
51- # The endpoint (AWS Lambda ARN) is set when handling the first request.
52- super ().__init__ (endpoint = None , api_key = api_key , api_url = api_url )
54+ super ().__init__ (endpoint = "not configured" , api_key = api_key , api_url = api_url )
5355
54- def handle (self , event , context ):
55- # Use the context to determine the ARN of the Lambda function.
56- self .endpoint = context .invoked_function_arn
57-
58- logger .debug ("Dispatch handler invoked for %s with event: %s" , self .endpoint , event )
56+ def handle (
57+ self , event : str , context : LambdaContext , entrypoint : Optional [str ] = None
58+ ):
59+ # The ARN is not none until the first invocation of the Lambda function.
60+ # We override the endpoint of all registered functions before any execution.
61+ if context .invoked_function_arn :
62+ self .endpoint = context .invoked_function_arn
63+ self .override_endpoint (self .endpoint )
5964
6065 if not event :
6166 raise ValueError ("event is required" )
6267
6368 try :
6469 raw = base64 .b64decode (event )
6570 except Exception as e :
66- raise ValueError (f "event is not base64 encoded: { e } " )
71+ raise ValueError ("event is not base64 encoded" ) from e
6772
6873 req = function_pb .RunRequest .FromString (raw )
69- print (req )
70- if not req .function :
71- req .function = "entrypoint"
72- # FIXME raise ValueError("function is required")
74+
75+ function : Optional [str ] = req .function if req .function else entrypoint
76+ if not function :
77+ raise ValueError ("function is required" )
78+
79+ logger .debug (
80+ "Dispatch handler invoked for %s function %s with runRequest: %s" ,
81+ self .endpoint ,
82+ function ,
83+ req ,
84+ )
7385
7486 try :
7587 func = self .functions [req .function ]
@@ -117,6 +129,6 @@ def handle(self, event, context):
117129 )
118130
119131 logger .debug ("finished handling run request with status %s" , status .name )
120- resp = response .SerializeToString ()
121- resp = base64 .b64encode (resp ).decode ("utf-8" )
122- return bytes (json .dumps (resp ), "utf-8" )
132+ respBytes = response .SerializeToString ()
133+ respStr = base64 .b64encode (respBytes ).decode ("utf-8" )
134+ return bytes (json .dumps (respStr ), "utf-8" )
0 commit comments