Skip to content

Commit 0d685a4

Browse files
committed
Add features and fix bugs
1 parent 8975fb7 commit 0d685a4

File tree

2 files changed

+165
-31
lines changed

2 files changed

+165
-31
lines changed

packages/tencent-cloud-sdk-serverless-functions/tencent/cloud/serverless/functions/__init__.py

Lines changed: 161 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,92 @@ def easy_invoke(self,
403403
return (self._get_event_loop().run_until_complete(self.invoke_async(region_id,
404404
namespace_name, function_name, function_event, function_version, True))['request_id'])
405405

406+
async def routine_invoke_async(self,
407+
region_id: str,
408+
namespace_name: str,
409+
function_name: str,
410+
routine_name: str,
411+
routine_parameter: dict = None,
412+
function_version: str = None,
413+
function_async: bool = False
414+
) -> object:
415+
'''
416+
Invoke given the specified routine of the cloud function.
417+
418+
Args:
419+
region_id: Data center unique identifier.
420+
namespace_name: Namespace name.
421+
function_name: Serverless cloud function name.
422+
routine_name: Routine name for serverless cloud functions.
423+
routine_parameter: Routine parameters for serverless cloud functions.
424+
function_version: Serverless cloud function version name.
425+
function_async: Make Serverless cloud function asynchronous invoke.
426+
427+
Returns:
428+
Returns the actual return value of the given routine for the
429+
given serverless cloud function.
430+
431+
Raises:
432+
ValueError: Parameter values are not as expected.
433+
InvokeError: Invoke Cloud Function failed.
434+
ActionError: Invoke Cloud Function error.
435+
'''
436+
437+
if not routine_name or not isinstance(routine_name, str):
438+
raise ValueError('<routine_name> value invalid')
439+
440+
if routine_parameter and not isinstance(routine_parameter, dict):
441+
raise ValueError('<routine_parameter> value invalid')
442+
443+
return await self.easy_invoke_async(region_id, namespace_name,
444+
function_name, {
445+
'protocol': {
446+
'version': 1,
447+
'payload': base64.b64encode(json.dumps(
448+
{
449+
'routine_name': routine_name,
450+
'routine_parameter': routine_parameter
451+
}
452+
).encode()).decode()
453+
}
454+
}, function_version, function_async)
455+
456+
def routine_invoke(self,
457+
region_id: str,
458+
namespace_name: str,
459+
function_name: str,
460+
routine_name: str,
461+
routine_parameter: dict = None,
462+
function_version: str = None,
463+
function_async: bool = False
464+
) -> object:
465+
'''
466+
Invoke given the specified routine of the cloud function.
467+
468+
Args:
469+
region_id: Data center unique identifier.
470+
namespace_name: Namespace name.
471+
function_name: Serverless cloud function name.
472+
routine_name: Routine name for serverless cloud functions.
473+
routine_parameter: Routine parameters for serverless cloud functions.
474+
function_version: Serverless cloud function version name.
475+
function_async: Make Serverless cloud function asynchronous invoke.
476+
477+
Returns:
478+
Returns the actual return value of the given routine for the
479+
given serverless cloud function.
480+
481+
Raises:
482+
ValueError: Parameter values are not as expected.
483+
InvokeError: Invoke Cloud Function failed.
484+
ActionError: Invoke Cloud Function error.
485+
'''
486+
487+
return self._get_event_loop().run_until_complete(self.routine_invoke_async(
488+
region_id, namespace_name, function_name, routine_name,
489+
routine_parameter, function_version, function_async
490+
))
491+
406492
async def select_function_async(self,
407493
region_id: str,
408494
namespace_name: str,
@@ -453,6 +539,64 @@ def select_function(self,
453539
return (lambda **function_event: self.easy_invoke(region_id,
454540
namespace_name, function_name, function_event, function_version, function_async))
455541

542+
async def select_routine_async(self,
543+
region_id: str,
544+
namespace_name: str,
545+
function_name: str,
546+
routine_name: str,
547+
function_version: str = None,
548+
function_async: bool = False
549+
):
550+
'''
551+
Creates a Python native asynchronous function for a
552+
given routine given a serverless cloud function.
553+
554+
Args:
555+
region_id: Data center unique identifier.
556+
namespace_name: Namespace name.
557+
function_name: Serverless cloud function name.
558+
routine_name: Routine name for serverless cloud functions.
559+
function_version: Serverless cloud function version name.
560+
function_async: Make Serverless cloud function asynchronous invoke.
561+
562+
Returns:
563+
Returns a Python native asynchronous function instance.
564+
'''
565+
566+
return (lambda **routine_parameter: self.routine_invoke_async(
567+
region_id, namespace_name, function_name, routine_name,
568+
routine_parameter, function_version, function_async
569+
))
570+
571+
def select_routine(self,
572+
region_id: str,
573+
namespace_name: str,
574+
function_name: str,
575+
routine_name: str,
576+
function_version: str = None,
577+
function_async: bool = False
578+
):
579+
'''
580+
Creates a Python native synchronous function for a
581+
given routine given a serverless cloud function.
582+
583+
Args:
584+
region_id: Data center unique identifier.
585+
namespace_name: Namespace name.
586+
function_name: Serverless cloud function name.
587+
routine_name: Routine name for serverless cloud functions.
588+
function_version: Serverless cloud function version name.
589+
function_async: Make Serverless cloud function asynchronous invoke.
590+
591+
Returns:
592+
Returns a Python native synchronous function instance.
593+
'''
594+
595+
return (lambda **routine_parameter: self.routine_invoke(
596+
region_id, namespace_name, function_name, routine_name,
597+
routine_parameter, function_version, function_async
598+
))
599+
456600
def bind_function(self,
457601
region_id: str,
458602
namespace_name: str,
@@ -531,16 +675,16 @@ def bind_routine(self,
531675
routine_name: str = None
532676
) -> object:
533677
'''
534-
Create a Python native synchronous or asynchronous function's
535-
integrated invoke binding for a given Cloud Function.
678+
Binds a given Python native synchronous or asynchronous function
679+
to a given routine for a given serverless cloud function.
536680
537681
Args:
538-
region_id: Unique identifier of the data center campus.
539-
namespace_name: Name of the owning namespace.
540-
function_name: Cloud Function name.
541-
function_version: Cloud Function version.
542-
function_async: Make Cloud Function asynchronous invoke.
543-
routine_name: Integration invoke routine name.
682+
region_id: Data center unique identifier.
683+
namespace_name: Namespace name of serverless cloud function.
684+
function_name: Serverless cloud function name.
685+
function_version: Serverless cloud function version name.
686+
function_async: Make Serverless Cloud Function asynchronous invoke.
687+
routine_name: Routine name for serverless cloud functions.
544688
545689
Raises:
546690
ValueError: Parameter values are not as expected.
@@ -559,32 +703,22 @@ def decorator_handler(
559703
if not inspect.isfunction(bound_function):
560704
raise ValueError('invalid binding object type')
561705

706+
method_instance: object = (self.routine_invoke_async if
707+
inspect.iscoroutinefunction(bound_function) else self.routine_invoke)
708+
562709
def invoke_handler(*args, **kwargs):
563-
routine_args: dict = dict()
710+
routine_parameter: dict = dict()
564711
parameter_names: list = inspect.getfullargspec(bound_function).args
565712

566713
for index, value in enumerate(args):
567-
routine_args[parameter_names[index]] = value
714+
routine_parameter[parameter_names[index]] = value
568715

569716
for name in kwargs:
570-
routine_args[name] = kwargs[name]
571-
572-
function_event: dict = {
573-
'protocol': {
574-
'version': 1,
575-
'payload': base64.b64encode(json.dumps(
576-
{
577-
'routine_name': (routine_name
578-
if routine_name else bound_function.__name__),
579-
'routine_args': routine_args
580-
}
581-
).encode()).decode()
582-
}
583-
}
717+
routine_parameter[name] = kwargs[name]
584718

585-
return (self.easy_invoke_async if inspect.iscoroutinefunction(bound_function)
586-
else self.easy_invoke)(region_id, namespace_name, function_name,
587-
function_event, function_version, function_async)
719+
return method_instance(region_id, namespace_name, function_name,
720+
routine_name if routine_name else bound_function.__name__,
721+
routine_parameter, function_version, function_async)
588722

589723
return invoke_handler
590724

packages/tencent-cloud-sdk-serverless-functions/tencent/cloud/serverless/functions/integrate.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ def routine(self,
4646
bound_function: object
4747
) -> object:
4848
'''
49-
Binds a given Python synchronization function to the
50-
current integration invoke dispatcher instance.
49+
Bind a Python synchronous function instance to the
50+
integration invoke dispatcher.
5151
5252
The bound object must be a function, otherwise the
5353
behavior is undefined.
@@ -108,7 +108,7 @@ def handler(self,
108108

109109
try:
110110
routine_name: str = protocol_context['routine_name']
111-
routine_args: dict = protocol_context['routine_args']
111+
routine_parameter: dict = protocol_context['routine_parameter']
112112
except KeyError as error:
113113
raise ValueError('protocol context missing field: ' + str(error))
114114

@@ -117,4 +117,4 @@ def handler(self,
117117
except KeyError:
118118
raise errors.NotFoundError('no such function: ' + str(routine_name))
119119

120-
return function_instance(**routine_args)
120+
return function_instance(**routine_parameter)

0 commit comments

Comments
 (0)