diff --git a/azure/functions/__init__.py b/azure/functions/__init__.py index 1f1e77aa..a02f101c 100644 --- a/azure/functions/__init__.py +++ b/azure/functions/__init__.py @@ -12,7 +12,6 @@ AsgiFunctionApp, WsgiFunctionApp, ExternalHttpFunctionApp) from ._durable_functions import OrchestrationContext, EntityContext -from .decorators.dapr.dapr_function_app import DaprFunctionApp, DaprBlueprint from .decorators.function_app import (FunctionRegister, TriggerApi, BindingApi, SettingsApi) from .extension import (ExtensionMeta, FunctionExtensionException, @@ -95,9 +94,7 @@ 'AuthLevel', 'Cardinality', 'AccessRights', - 'HttpMethod', - 'DaprFunctionApp', - 'DaprBlueprint' + 'HttpMethod' ) __version__ = '1.18.0b2' diff --git a/azure/functions/decorators/__init__.py b/azure/functions/decorators/__init__.py index 83b81eb4..90ff9ac6 100644 --- a/azure/functions/decorators/__init__.py +++ b/azure/functions/decorators/__init__.py @@ -5,7 +5,6 @@ AuthLevel, Blueprint, ExternalHttpFunctionApp, AsgiFunctionApp, \ WsgiFunctionApp, FunctionRegister, TriggerApi, BindingApi, SettingsApi from .http import HttpMethod -from .dapr.dapr_function_app import DaprFunctionApp, DaprBlueprint __all__ = [ 'FunctionApp', @@ -23,7 +22,5 @@ 'AuthLevel', 'Cardinality', 'AccessRights', - 'HttpMethod', - 'DaprFunctionApp', - 'DaprBlueprint' + 'HttpMethod' ] diff --git a/azure/functions/decorators/dapr/dapr.py b/azure/functions/decorators/dapr.py similarity index 100% rename from azure/functions/decorators/dapr/dapr.py rename to azure/functions/decorators/dapr.py diff --git a/azure/functions/decorators/dapr/dapr_function_app.py b/azure/functions/decorators/dapr/dapr_function_app.py deleted file mode 100644 index 65a2189c..00000000 --- a/azure/functions/decorators/dapr/dapr_function_app.py +++ /dev/null @@ -1,522 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -from abc import ABC -from typing import Any, Callable, Optional, Union -from azure.functions.decorators.core import DataType, AuthLevel -from azure.functions.decorators.utils import parse_singular_param_to_enum -from azure.functions.decorators.function_app import BindingApi, Blueprint, \ - FunctionRegister, SettingsApi, TriggerApi -from azure.functions.decorators.dapr.dapr import DaprBindingOutput, \ - DaprBindingTrigger, DaprInvokeOutput, DaprPublishOutput, \ - DaprSecretInput, DaprServiceInvocationTrigger, DaprStateInput, \ - DaprStateOutput, DaprTopicTrigger - - -class DaprTriggerApi(TriggerApi, ABC): - - def dapr_service_invocation_trigger(self, - arg_name: str, - method_name: str, - data_type: Optional[ - Union[DataType, str]] = None, - **kwargs: Any) -> Callable[..., Any]: - """The dapr_service_invocation_trigger decorator adds - :class:`DaprServiceInvocationTrigger` - to the :class:`FunctionBuilder` object - for building :class:`Function` object used in worker function - indexing model. This is equivalent to defining - DaprServiceInvocationTrigger - in the function.json which enables function to be triggered when new - service invocation occurs through Dapr. - All optional fields will be given default value by function host when - they are parsed by function host. - - Ref: https://aka.ms/azure-function-dapr-trigger-service-invocation - - :param arg_name: The name of the variable that represents - :param method_name: The name of the method on a remote Dapr App. - If not specified, the name of the function is used as the method name. - :param data_type: Defines how Functions runtime should treat the - parameter value. - :param kwargs: Keyword arguments for specifying additional binding - fields to include in the binding json. - - :return: Decorator function. - """ - - @self._configure_function_builder - def wrap(fb): - def decorator(): - fb.add_trigger( - trigger=DaprServiceInvocationTrigger( - name=arg_name, - method_name=method_name, - data_type=parse_singular_param_to_enum(data_type, - DataType), - **kwargs)) - return fb - - return decorator() - - return wrap - - def dapr_binding_trigger(self, - arg_name: str, - binding_name: str, - data_type: Optional[ - Union[DataType, str]] = None, - **kwargs: Any) -> Callable[..., Any]: - """The dapr_binding_trigger decorator adds - :class:`DaprBindingTrigger` - to the :class:`FunctionBuilder` object - for building :class:`Function` object used in worker function - indexing model. This is equivalent to defining DaprBindingTrigger - in the function.json which enables function to be triggered - on Dapr input binding. - All optional fields will be given default value by function host when - they are parsed by function host. - - Ref: https://aka.ms/azure-function-dapr-trigger-binding - - :param arg_name: The name of the variable that represents - :param binding_name: The name of the Dapr trigger. - If not specified, the name of the function is used as the trigger name. - :param data_type: Defines how Functions runtime should treat the - parameter value. - :param kwargs: Keyword arguments for specifying additional binding - fields to include in the binding json. - - :return: Decorator function. - """ - - @self._configure_function_builder - def wrap(fb): - def decorator(): - fb.add_trigger( - trigger=DaprBindingTrigger( - name=arg_name, - binding_name=binding_name, - data_type=parse_singular_param_to_enum(data_type, - DataType), - **kwargs)) - return fb - - return decorator() - - return wrap - - def dapr_topic_trigger(self, - arg_name: str, - pub_sub_name: str, - topic: str, - route: Optional[str] = None, - data_type: Optional[ - Union[DataType, str]] = None, - **kwargs: Any) -> Callable[..., Any]: - """The dapr_topic_trigger decorator adds - :class:`DaprTopicTrigger` - to the :class:`FunctionBuilder` object - for building :class:`Function` object used in worker function - indexing model. This is equivalent to defining DaprTopicTrigger - in the function.json which enables function to be triggered when new - message(s) are sent to the Dapr pubsub. - All optional fields will be given default value by function host when - they are parsed by function host. - - Ref: https://aka.ms/azure-function-dapr-trigger-topic - - :param arg_name: The name of the variable that represents - :param pub_sub_name: The pub/sub name. - :param topic: The topic. If unspecified the function name will be used. - :param route: The route for the trigger. If unspecified - the topic name will be used. - :param data_type: Defines how Functions runtime should treat the - parameter value. - :param kwargs: Keyword arguments for specifying additional binding - fields to include in the binding json. - - :return: Decorator function. - """ - # TODO: This is a temporary check, it should be removed once route - # issue is fixed at python worker. - # Currently, python worker treats route as HttpTrigger attribute and - # expects value for route. Route could be nil for dapr topic trigger. - if not route: - route = topic - - @self._configure_function_builder - def wrap(fb): - def decorator(): - fb.add_trigger( - trigger=DaprTopicTrigger( - name=arg_name, - pub_sub_name=pub_sub_name, - topic=topic, - route=route, - data_type=parse_singular_param_to_enum(data_type, - DataType), - **kwargs)) - return fb - - return decorator() - - return wrap - - -class DaprBindingApi(BindingApi, ABC): - - def dapr_state_input(self, - arg_name: str, - state_store: str, - key: str, - dapr_address: Optional[str] = None, - data_type: Optional[ - Union[DataType, str]] = None, - **kwargs) \ - -> Callable[..., Any]: - """The dapr_state_input decorator adds - :class:`DaprStateInput` to the :class:`FunctionBuilder` object - for building :class:`Function` object used in worker function - indexing model. This is equivalent to defining DaprStateInput - in the function.json which enables function to read state from - underlying state store component. - All optional fields will be given default value by function host when - they are parsed by function host. - - Ref: https://aka.ms/azure-function-dapr-state-input-binding - - :param arg_name: The name of the variable that represents DaprState - input object in function code. - :param state_store: State store containing the state. - :param key: The name of the key. - :param dapr_address: Dapr address, it is optional field, by default - it will be set to http://localhost:{daprHttpPort}. - :param data_type: Defines how Functions runtime should treat the - parameter value. - :param kwargs: Keyword arguments for specifying additional binding - fields to include in the binding json. - - :return: Decorator function. - """ - - @self._configure_function_builder - def wrap(fb): - def decorator(): - fb.add_binding( - binding=DaprStateInput( - name=arg_name, - state_store=state_store, - key=key, - dapr_address=dapr_address, - data_type=parse_singular_param_to_enum(data_type, - DataType), - **kwargs)) - return fb - - return decorator() - - return wrap - - def dapr_secret_input(self, - arg_name: str, - secret_store_name: str, - key: str, - metadata: str, - dapr_address: Optional[str] = None, - data_type: Optional[ - Union[DataType, str]] = None, - **kwargs) \ - -> Callable[..., Any]: - """The dapr_secret_input decorator adds - :class:`DaprSecretInput` to the :class:`FunctionBuilder` object - for building :class:`Function` object used in worker function - indexing model. This is equivalent to defining DaprSecretInput - in the function.json which enables function to read secret from - underlying secret store component. - All optional fields will be given default value by function host when - they are parsed by function host. - - Ref: https://aka.ms/azure-function-dapr-secret-input-binding - - :param arg_name: The name of the variable that represents DaprState - input object in function code. - :param secret_store_name: The name of the secret store to - get the secret from. - :param key: The key identifying the name of the secret to get. - :param metadata: An array of metadata properties in the form - "key1=value1&key2=value2". - :param dapr_address: Dapr address, it is optional field, by default - it will be set to http://localhost:{daprHttpPort}. - :param data_type: Defines how Functions runtime should treat the - parameter value. - :param kwargs: Keyword arguments for specifying additional binding - fields to include in the binding json. - - :return: Decorator function. - """ - - @self._configure_function_builder - def wrap(fb): - def decorator(): - fb.add_binding( - binding=DaprSecretInput( - name=arg_name, - secret_store_name=secret_store_name, - key=key, - metadata=metadata, - dapr_address=dapr_address, - data_type=parse_singular_param_to_enum(data_type, - DataType), - **kwargs)) - return fb - - return decorator() - - return wrap - - def dapr_state_output(self, - arg_name: str, - state_store: str, - key: str, - dapr_address: Optional[str] = None, - data_type: Optional[ - Union[DataType, str]] = None, - **kwargs) \ - -> Callable[..., Any]: - """The dapr_state_output decorator adds - :class:`DaprStateOutput` to the :class:`FunctionBuilder` object - for building :class:`Function` object used in worker function - indexing model. - This is equivalent to defining DaprStateOutput - in the function.json which enables function to write to the dapr - state store. - All optional fields will be given default value by function host when - they are parsed by function host. - - Ref: https://aka.ms/azure-function-dapr-state-output-binding - - :param arg_name: The name of the variable that represents DaprState - output object in function code. - :param arg_name: The name of the variable that represents DaprState - input object in function code. - :param state_store: State store containing the state for keys. - :param key: The name of the key. - :param dapr_address: Dapr address, it is optional field, by default - it will be set to http://localhost:{daprHttpPort}. - :param data_type: Defines how Functions runtime should treat the - parameter value - :param kwargs: Keyword arguments for specifying additional binding - fields to include in the binding json - - :return: Decorator function. - """ - - @self._configure_function_builder - def wrap(fb): - def decorator(): - fb.add_binding( - binding=DaprStateOutput( - name=arg_name, - state_store=state_store, - key=key, - dapr_address=dapr_address, - data_type=parse_singular_param_to_enum(data_type, - DataType), - **kwargs)) - return fb - - return decorator() - - return wrap - - def dapr_invoke_output(self, - arg_name: str, - app_id: str, - method_name: str, - http_verb: str, - dapr_address: Optional[str] = None, - data_type: Optional[ - Union[DataType, str]] = None, - **kwargs) \ - -> Callable[..., Any]: - """The dapr_invoke_output decorator adds - :class:`DaprInvokeOutput` to the :class:`FunctionBuilder` object - for building :class:`Function` object used in worker function - indexing model. - This is equivalent to defining DaprInvokeOutput - in the function.json which enables function to invoke another Dapr App. - All optional fields will be given default value by function host when - they are parsed by function host. - - Ref: https://aka.ms/azure-function-dapr-invoke-output-binding - - :param arg_name: The name of the variable that represents DaprState - output object in function code. - :param arg_name: The name of the variable that represents DaprState - input object in function code. - :param app_id: The dapr app name to invoke. - :param method_name: The method name of the app to invoke. - :param http_verb: The http verb of the app to invoke. - :param dapr_address: Dapr address, it is optional field, by default - it will be set to http://localhost:{daprHttpPort}. - :param data_type: Defines how Functions runtime should treat the - parameter value - :param kwargs: Keyword arguments for specifying additional binding - fields to include in the binding json - - :return: Decorator function. - """ - - @self._configure_function_builder - def wrap(fb): - def decorator(): - fb.add_binding( - binding=DaprInvokeOutput( - name=arg_name, - app_id=app_id, - method_name=method_name, - http_verb=http_verb, - dapr_address=dapr_address, - data_type=parse_singular_param_to_enum(data_type, - DataType), - **kwargs)) - return fb - - return decorator() - - return wrap - - def dapr_publish_output(self, - arg_name: str, - pub_sub_name: str, - topic: str, - dapr_address: Optional[str] = None, - data_type: Optional[ - Union[DataType, str]] = None, - **kwargs) \ - -> Callable[..., Any]: - """The dapr_publish_output decorator adds - :class:`DaprPublishOutput` to the :class:`FunctionBuilder` object - for building :class:`Function` object used in worker function - indexing model. - This is equivalent to defining DaprPublishOutput - in the function.json which enables function to publish topic. - All optional fields will be given default value by function host when - they are parsed by function host. - - Ref: https://aka.ms/azure-function-dapr-publish-output-binding - - :param arg_name: The name of the variable that represents DaprState - output object in function code. - :param arg_name: The name of the variable that represents DaprState - input object in function code. - :param pub_sub_name: The pub/sub name to publish to. - :param topic: The name of the topic to publish to. - :param dapr_address: Dapr address, it is optional field, by default - ßit will be set to http://localhost:{daprHttpPort}. - :param data_type: Defines how Functions runtime should treat the - parameter value - :param kwargs: Keyword arguments for specifying additional binding - fields to include in the binding json - - :return: Decorator function. - """ - - @self._configure_function_builder - def wrap(fb): - def decorator(): - fb.add_binding( - binding=DaprPublishOutput( - name=arg_name, - pub_sub_name=pub_sub_name, - topic=topic, - dapr_address=dapr_address, - data_type=parse_singular_param_to_enum(data_type, - DataType), - **kwargs)) - return fb - - return decorator() - - return wrap - - def dapr_binding_output(self, - arg_name: str, - binding_name: str, - operation: str, - dapr_address: Optional[str] = None, - data_type: Optional[ - Union[DataType, str]] = None, - **kwargs) \ - -> Callable[..., Any]: - """The dapr_binding_output decorator adds - :class:`DaprBindingOutput` to the :class:`FunctionBuilder` object - for building :class:`Function` object used in worker function - indexing model. - This is equivalent to defining DaprBindingOutput - in the function.json which enables function to send a value to - a Dapr output binding. - All optional fields will be given default value by function host when - they are parsed by function host. - - Ref: https://aka.ms/azure-function-dapr-binding-output-binding - - :param arg_name: The name of the variable that represents DaprState - output object in function code. - :param arg_name: The name of the variable that represents DaprState - input object in function code. - :param binding_name: The configured name of the binding. - :param operation: The configured operation. - :param dapr_address: Dapr address, it is optional field, by default - it will be set to http://localhost:{daprHttpPort}. - :param data_type: Defines how Functions runtime should treat the - parameter value - :param kwargs: Keyword arguments for specifying additional binding - fields to include in the binding json - - :return: Decorator function. - """ - - @self._configure_function_builder - def wrap(fb): - def decorator(): - fb.add_binding( - binding=DaprBindingOutput( - name=arg_name, - binding_name=binding_name, - operation=operation, - dapr_address=dapr_address, - data_type=parse_singular_param_to_enum(data_type, - DataType), - **kwargs)) - return fb - - return decorator() - - return wrap - - -class DaprBlueprint(Blueprint, DaprTriggerApi, DaprBindingApi): - """Functions container class where all the functions - loaded in it can be registered in :class:`FunctionRegister` subclasses - but itself can not be indexed directly. The class contains all existing - supported trigger and binding decorator functions. - """ - pass - - -class DaprFunctionApp(FunctionRegister, DaprTriggerApi, - DaprBindingApi, SettingsApi): - """DaprFunctionApp object used for Dapr bindings and triggers. - - Ref: https://aka.ms/dapr-bindings-triggers - """ - def __init__(self, - http_auth_level: Union[AuthLevel, str] = AuthLevel.FUNCTION): - """Constructor of :class:`DaprFunctionApp` object. - - :param http_auth_level: Determines what keys, if any, need to be - present - on the request in order to invoke the function. - """ - super().__init__(auth_level=http_auth_level) diff --git a/azure/functions/decorators/function_app.py b/azure/functions/decorators/function_app.py index 3af406dc..95efeb6b 100644 --- a/azure/functions/decorators/function_app.py +++ b/azure/functions/decorators/function_app.py @@ -14,6 +14,10 @@ from azure.functions.decorators.cosmosdb import CosmosDBTrigger, \ CosmosDBOutput, CosmosDBInput, CosmosDBTriggerV3, CosmosDBInputV3, \ CosmosDBOutputV3 +from azure.functions.decorators.dapr import DaprBindingOutput, \ + DaprBindingTrigger, DaprInvokeOutput, DaprPublishOutput, \ + DaprSecretInput, DaprServiceInvocationTrigger, DaprStateInput, \ + DaprStateOutput, DaprTopicTrigger from azure.functions.decorators.eventgrid import EventGridTrigger, \ EventGridOutput from azure.functions.decorators.eventhub import EventHubTrigger, EventHubOutput @@ -1169,6 +1173,154 @@ def decorator(): return wrap + def dapr_service_invocation_trigger(self, + arg_name: str, + method_name: str, + data_type: Optional[ + Union[DataType, str]] = None, + **kwargs: Any) -> Callable[..., Any]: + """The dapr_service_invocation_trigger decorator adds + :class:`DaprServiceInvocationTrigger` + to the :class:`FunctionBuilder` object + for building :class:`Function` object used in worker function + indexing model. This is equivalent to defining + DaprServiceInvocationTrigger + in the function.json which enables function to be triggered when new + service invocation occurs through Dapr. + All optional fields will be given default value by function host when + they are parsed by function host. + + Ref: https://aka.ms/azure-function-dapr-trigger-service-invocation + + :param arg_name: The name of the variable that represents + :param method_name: The name of the method on a remote Dapr App. + If not specified, the name of the function is used as the method name. + :param data_type: Defines how Functions runtime should treat the + parameter value. + :param kwargs: Keyword arguments for specifying additional binding + fields to include in the binding json. + + :return: Decorator function. + """ + + @self._configure_function_builder + def wrap(fb): + def decorator(): + fb.add_trigger( + trigger=DaprServiceInvocationTrigger( + name=arg_name, + method_name=method_name, + data_type=parse_singular_param_to_enum(data_type, + DataType), + **kwargs)) + return fb + + return decorator() + + return wrap + + def dapr_binding_trigger(self, + arg_name: str, + binding_name: str, + data_type: Optional[ + Union[DataType, str]] = None, + **kwargs: Any) -> Callable[..., Any]: + """The dapr_binding_trigger decorator adds + :class:`DaprBindingTrigger` + to the :class:`FunctionBuilder` object + for building :class:`Function` object used in worker function + indexing model. This is equivalent to defining DaprBindingTrigger + in the function.json which enables function to be triggered + on Dapr input binding. + All optional fields will be given default value by function host when + they are parsed by function host. + + Ref: https://aka.ms/azure-function-dapr-trigger-binding + + :param arg_name: The name of the variable that represents + :param binding_name: The name of the Dapr trigger. + If not specified, the name of the function is used as the trigger name. + :param data_type: Defines how Functions runtime should treat the + parameter value. + :param kwargs: Keyword arguments for specifying additional binding + fields to include in the binding json. + + :return: Decorator function. + """ + + @self._configure_function_builder + def wrap(fb): + def decorator(): + fb.add_trigger( + trigger=DaprBindingTrigger( + name=arg_name, + binding_name=binding_name, + data_type=parse_singular_param_to_enum(data_type, + DataType), + **kwargs)) + return fb + + return decorator() + + return wrap + + def dapr_topic_trigger(self, + arg_name: str, + pub_sub_name: str, + topic: str, + route: Optional[str] = None, + data_type: Optional[ + Union[DataType, str]] = None, + **kwargs: Any) -> Callable[..., Any]: + """The dapr_topic_trigger decorator adds + :class:`DaprTopicTrigger` + to the :class:`FunctionBuilder` object + for building :class:`Function` object used in worker function + indexing model. This is equivalent to defining DaprTopicTrigger + in the function.json which enables function to be triggered when new + message(s) are sent to the Dapr pubsub. + All optional fields will be given default value by function host when + they are parsed by function host. + + Ref: https://aka.ms/azure-function-dapr-trigger-topic + + :param arg_name: The name of the variable that represents + :param pub_sub_name: The pub/sub name. + :param topic: The topic. If unspecified the function name will be used. + :param route: The route for the trigger. If unspecified + the topic name will be used. + :param data_type: Defines how Functions runtime should treat the + parameter value. + :param kwargs: Keyword arguments for specifying additional binding + fields to include in the binding json. + + :return: Decorator function. + """ + # TODO: This is a temporary check, it should be removed once route + # issue is fixed at python worker. + # Currently, python worker treats route as HttpTrigger attribute and + # expects value for route. Route could be nil for dapr topic trigger. + if not route: + route = topic + + @self._configure_function_builder + def wrap(fb): + def decorator(): + fb.add_trigger( + trigger=DaprTopicTrigger( + name=arg_name, + pub_sub_name=pub_sub_name, + topic=topic, + route=route, + data_type=parse_singular_param_to_enum(data_type, + DataType), + **kwargs)) + return fb + + return decorator() + + return wrap + class BindingApi(DecoratorApi, ABC): """Interface to extend for using existing binding decorator functions.""" @@ -2113,6 +2265,336 @@ def decorator(): return wrap + def dapr_state_input(self, + arg_name: str, + state_store: str, + key: str, + dapr_address: Optional[str] = None, + data_type: Optional[ + Union[DataType, str]] = None, + **kwargs) \ + -> Callable[..., Any]: + """The dapr_state_input decorator adds + :class:`DaprStateInput` to the :class:`FunctionBuilder` object + for building :class:`Function` object used in worker function + indexing model. This is equivalent to defining DaprStateInput + in the function.json which enables function to read state from + underlying state store component. + All optional fields will be given default value by function host when + they are parsed by function host. + + Ref: https://aka.ms/azure-function-dapr-state-input-binding + + :param arg_name: The name of the variable that represents DaprState + input object in function code. + :param state_store: State store containing the state. + :param key: The name of the key. + :param dapr_address: Dapr address, it is optional field, by default + it will be set to http://localhost:{daprHttpPort}. + :param data_type: Defines how Functions runtime should treat the + parameter value. + :param kwargs: Keyword arguments for specifying additional binding + fields to include in the binding json. + + :return: Decorator function. + """ + + @self._configure_function_builder + def wrap(fb): + def decorator(): + fb.add_binding( + binding=DaprStateInput( + name=arg_name, + state_store=state_store, + key=key, + dapr_address=dapr_address, + data_type=parse_singular_param_to_enum(data_type, + DataType), + **kwargs)) + return fb + + return decorator() + + return wrap + + def dapr_secret_input(self, + arg_name: str, + secret_store_name: str, + key: str, + metadata: str, + dapr_address: Optional[str] = None, + data_type: Optional[ + Union[DataType, str]] = None, + **kwargs) \ + -> Callable[..., Any]: + """The dapr_secret_input decorator adds + :class:`DaprSecretInput` to the :class:`FunctionBuilder` object + for building :class:`Function` object used in worker function + indexing model. This is equivalent to defining DaprSecretInput + in the function.json which enables function to read secret from + underlying secret store component. + All optional fields will be given default value by function host when + they are parsed by function host. + + Ref: https://aka.ms/azure-function-dapr-secret-input-binding + + :param arg_name: The name of the variable that represents DaprState + input object in function code. + :param secret_store_name: The name of the secret store to + get the secret from. + :param key: The key identifying the name of the secret to get. + :param metadata: An array of metadata properties in the form + "key1=value1&key2=value2". + :param dapr_address: Dapr address, it is optional field, by default + it will be set to http://localhost:{daprHttpPort}. + :param data_type: Defines how Functions runtime should treat the + parameter value. + :param kwargs: Keyword arguments for specifying additional binding + fields to include in the binding json. + + :return: Decorator function. + """ + + @self._configure_function_builder + def wrap(fb): + def decorator(): + fb.add_binding( + binding=DaprSecretInput( + name=arg_name, + secret_store_name=secret_store_name, + key=key, + metadata=metadata, + dapr_address=dapr_address, + data_type=parse_singular_param_to_enum(data_type, + DataType), + **kwargs)) + return fb + + return decorator() + + return wrap + + def dapr_state_output(self, + arg_name: str, + state_store: str, + key: str, + dapr_address: Optional[str] = None, + data_type: Optional[ + Union[DataType, str]] = None, + **kwargs) \ + -> Callable[..., Any]: + """The dapr_state_output decorator adds + :class:`DaprStateOutput` to the :class:`FunctionBuilder` object + for building :class:`Function` object used in worker function + indexing model. + This is equivalent to defining DaprStateOutput + in the function.json which enables function to write to the dapr + state store. + All optional fields will be given default value by function host when + they are parsed by function host. + + Ref: https://aka.ms/azure-function-dapr-state-output-binding + + :param arg_name: The name of the variable that represents DaprState + output object in function code. + :param arg_name: The name of the variable that represents DaprState + input object in function code. + :param state_store: State store containing the state for keys. + :param key: The name of the key. + :param dapr_address: Dapr address, it is optional field, by default + it will be set to http://localhost:{daprHttpPort}. + :param data_type: Defines how Functions runtime should treat the + parameter value + :param kwargs: Keyword arguments for specifying additional binding + fields to include in the binding json + + :return: Decorator function. + """ + + @self._configure_function_builder + def wrap(fb): + def decorator(): + fb.add_binding( + binding=DaprStateOutput( + name=arg_name, + state_store=state_store, + key=key, + dapr_address=dapr_address, + data_type=parse_singular_param_to_enum(data_type, + DataType), + **kwargs)) + return fb + + return decorator() + + return wrap + + def dapr_invoke_output(self, + arg_name: str, + app_id: str, + method_name: str, + http_verb: str, + dapr_address: Optional[str] = None, + data_type: Optional[ + Union[DataType, str]] = None, + **kwargs) \ + -> Callable[..., Any]: + """The dapr_invoke_output decorator adds + :class:`DaprInvokeOutput` to the :class:`FunctionBuilder` object + for building :class:`Function` object used in worker function + indexing model. + This is equivalent to defining DaprInvokeOutput + in the function.json which enables function to invoke another Dapr App. + All optional fields will be given default value by function host when + they are parsed by function host. + + Ref: https://aka.ms/azure-function-dapr-invoke-output-binding + + :param arg_name: The name of the variable that represents DaprState + output object in function code. + :param arg_name: The name of the variable that represents DaprState + input object in function code. + :param app_id: The dapr app name to invoke. + :param method_name: The method name of the app to invoke. + :param http_verb: The http verb of the app to invoke. + :param dapr_address: Dapr address, it is optional field, by default + it will be set to http://localhost:{daprHttpPort}. + :param data_type: Defines how Functions runtime should treat the + parameter value + :param kwargs: Keyword arguments for specifying additional binding + fields to include in the binding json + + :return: Decorator function. + """ + + @self._configure_function_builder + def wrap(fb): + def decorator(): + fb.add_binding( + binding=DaprInvokeOutput( + name=arg_name, + app_id=app_id, + method_name=method_name, + http_verb=http_verb, + dapr_address=dapr_address, + data_type=parse_singular_param_to_enum(data_type, + DataType), + **kwargs)) + return fb + + return decorator() + + return wrap + + def dapr_publish_output(self, + arg_name: str, + pub_sub_name: str, + topic: str, + dapr_address: Optional[str] = None, + data_type: Optional[ + Union[DataType, str]] = None, + **kwargs) \ + -> Callable[..., Any]: + """The dapr_publish_output decorator adds + :class:`DaprPublishOutput` to the :class:`FunctionBuilder` object + for building :class:`Function` object used in worker function + indexing model. + This is equivalent to defining DaprPublishOutput + in the function.json which enables function to publish topic. + All optional fields will be given default value by function host when + they are parsed by function host. + + Ref: https://aka.ms/azure-function-dapr-publish-output-binding + + :param arg_name: The name of the variable that represents DaprState + output object in function code. + :param arg_name: The name of the variable that represents DaprState + input object in function code. + :param pub_sub_name: The pub/sub name to publish to. + :param topic: The name of the topic to publish to. + :param dapr_address: Dapr address, it is optional field, by default + ßit will be set to http://localhost:{daprHttpPort}. + :param data_type: Defines how Functions runtime should treat the + parameter value + :param kwargs: Keyword arguments for specifying additional binding + fields to include in the binding json + + :return: Decorator function. + """ + + @self._configure_function_builder + def wrap(fb): + def decorator(): + fb.add_binding( + binding=DaprPublishOutput( + name=arg_name, + pub_sub_name=pub_sub_name, + topic=topic, + dapr_address=dapr_address, + data_type=parse_singular_param_to_enum(data_type, + DataType), + **kwargs)) + return fb + + return decorator() + + return wrap + + def dapr_binding_output(self, + arg_name: str, + binding_name: str, + operation: str, + dapr_address: Optional[str] = None, + data_type: Optional[ + Union[DataType, str]] = None, + **kwargs) \ + -> Callable[..., Any]: + """The dapr_binding_output decorator adds + :class:`DaprBindingOutput` to the :class:`FunctionBuilder` object + for building :class:`Function` object used in worker function + indexing model. + This is equivalent to defining DaprBindingOutput + in the function.json which enables function to send a value to + a Dapr output binding. + All optional fields will be given default value by function host when + they are parsed by function host. + + Ref: https://aka.ms/azure-function-dapr-binding-output-binding + + :param arg_name: The name of the variable that represents DaprState + output object in function code. + :param arg_name: The name of the variable that represents DaprState + input object in function code. + :param binding_name: The configured name of the binding. + :param operation: The configured operation. + :param dapr_address: Dapr address, it is optional field, by default + it will be set to http://localhost:{daprHttpPort}. + :param data_type: Defines how Functions runtime should treat the + parameter value + :param kwargs: Keyword arguments for specifying additional binding + fields to include in the binding json + + :return: Decorator function. + """ + + @self._configure_function_builder + def wrap(fb): + def decorator(): + fb.add_binding( + binding=DaprBindingOutput( + name=arg_name, + binding_name=binding_name, + operation=operation, + dapr_address=dapr_address, + data_type=parse_singular_param_to_enum(data_type, + DataType), + **kwargs)) + return fb + + return decorator() + + return wrap + class SettingsApi(DecoratorApi, ABC): """Interface to extend for using existing settings decorator in diff --git a/tests/decorators/test_dapr.py b/tests/decorators/test_dapr.py index e4cd14b7..d64287b3 100644 --- a/tests/decorators/test_dapr.py +++ b/tests/decorators/test_dapr.py @@ -2,19 +2,17 @@ # Licensed under the MIT License. import unittest -from azure.functions.decorators.core import SCRIPT_FILE_NAME, AuthLevel, \ - BindingDirection -from azure.functions.decorators.dapr.dapr_function_app import DaprBlueprint, \ - DaprFunctionApp +from azure.functions.decorators.core import BindingDirection from azure.functions.decorators.constants import DAPR_BINDING, \ DAPR_BINDING_TRIGGER, DAPR_INVOKE, DAPR_PUBLISH, DAPR_SECRET, \ DAPR_SERVICE_INVOCATION_TRIGGER, DAPR_STATE, DAPR_TOPIC_TRIGGER +from azure.functions.decorators.function_app import FunctionApp from tests.decorators.testutils import assert_json class TestDapr(unittest.TestCase): def setUp(self): - self.dapr_func_app = DaprFunctionApp() + self.func_app = FunctionApp() def _get_user_function(self, app): funcs = app.get_functions() @@ -22,7 +20,7 @@ def _get_user_function(self, app): return funcs[0] def test_dapr_service_invocation_trigger_default_args(self): - app = self.dapr_func_app + app = self.func_app @app.dapr_service_invocation_trigger(arg_name="req", method_name="dummy_method_name") @@ -48,7 +46,7 @@ def dummy(): ) def test_dapr_binding_trigger_default_args(self): - app = self.dapr_func_app + app = self.func_app @app.dapr_binding_trigger(arg_name="req", binding_name="dummy_binding_name") @@ -69,7 +67,7 @@ def dummy(): }) def test_dapr_topic_trigger_default_args(self): - app = self.dapr_func_app + app = self.func_app @app.dapr_topic_trigger(arg_name="req", pub_sub_name="dummy_pub_sub_name", @@ -94,7 +92,7 @@ def dummy(): }) def test_dapr_state_input_binding(self): - app = self.dapr_func_app + app = self.func_app @app.dapr_service_invocation_trigger(arg_name="req", method_name="dummy") @@ -119,7 +117,7 @@ def dummy(): }) def test_dapr_secret_input_binding(self): - app = self.dapr_func_app + app = self.func_app @app.dapr_service_invocation_trigger(arg_name="req", method_name="dummy") @@ -146,7 +144,7 @@ def dummy(): }) def test_dapr_state_output_binding(self): - app = self.dapr_func_app + app = self.func_app @app.dapr_service_invocation_trigger(arg_name="req", method_name="dummy") @@ -171,7 +169,7 @@ def dummy(): }) def test_dapr_invoke_output_binding(self): - app = self.dapr_func_app + app = self.func_app @app.dapr_service_invocation_trigger(arg_name="req", method_name="dummy") @@ -198,7 +196,7 @@ def dummy(): }) def test_dapr_publish_output_binding(self): - app = self.dapr_func_app + app = self.func_app @app.dapr_service_invocation_trigger(arg_name="req", method_name="dummy") @@ -223,7 +221,7 @@ def dummy(): }) def test_dapr_binding_output_binding(self): - app = self.dapr_func_app + app = self.func_app @app.dapr_service_invocation_trigger(arg_name="req", method_name="dummy") @@ -246,31 +244,3 @@ def dummy(): "bindingName": "dummy_binding_name", "operation": "dummy_operation" }) - - def test_register_dapr_blueprint(self): - bp = DaprBlueprint() - - @bp.schedule(arg_name="name", schedule="10****") - def hello(name: str): - return "hello" - - app = DaprFunctionApp() - app.register_blueprint(bp) - - self.assertEqual(len(app.get_functions()), 1) - self.assertEqual(app.auth_level, AuthLevel.FUNCTION) - self.assertEqual(app.app_script_file, SCRIPT_FILE_NAME) - - def test_register_dapr_blueprint_app_auth_level(self): - bp = DaprBlueprint() - - @bp.route("name") - def hello(name: str): - return "hello" - - app = DaprFunctionApp(http_auth_level=AuthLevel.ANONYMOUS) - app.register_blueprint(bp) - - self.assertEqual(len(app.get_functions()), 1) - self.assertEqual(app.get_functions()[0].get_trigger().auth_level, - AuthLevel.ANONYMOUS)