10
10
11
11
from azure .functions .decorators .blob import BlobTrigger , BlobInput , BlobOutput
12
12
from azure .functions .decorators .core import Binding , Trigger , DataType , \
13
- AuthLevel , SCRIPT_FILE_NAME , Cardinality , AccessRights
13
+ AuthLevel , SCRIPT_FILE_NAME , Cardinality , AccessRights , Setting
14
14
from azure .functions .decorators .cosmosdb import CosmosDBTrigger , \
15
15
CosmosDBOutput , CosmosDBInput , CosmosDBTriggerV3 , CosmosDBInputV3 , \
16
16
CosmosDBOutputV3
29
29
parse_iterable_param_to_enums , StringifyEnumJsonEncoder
30
30
from azure .functions .http import HttpRequest
31
31
from .generic import GenericInputBinding , GenericTrigger , GenericOutputBinding
32
+ from .retry_policy import RetryPolicy
33
+ from .function_name import FunctionName
32
34
from .warmup import WarmUpTrigger
33
35
from .._http_asgi import AsgiMiddleware
34
36
from .._http_wsgi import WsgiMiddleware , Context
@@ -45,11 +47,17 @@ def __init__(self, func: Callable[..., Any], script_file: str):
45
47
46
48
:param func: User defined python function instance.
47
49
:param script_file: File name indexed by worker to find function.
50
+ :param trigger: The trigger object of the function.
51
+ :param bindings: The list of binding objects of a function.
52
+ :param settings: The list of setting objects of a function.
53
+ :param http_type: Http function type.
54
+ :param is_http_function: Whether the function is a http function.
48
55
"""
49
56
self ._name : str = func .__name__
50
57
self ._func = func
51
58
self ._trigger : Optional [Trigger ] = None
52
59
self ._bindings : List [Binding ] = []
60
+ self ._settings : List [Setting ] = []
53
61
self .function_script_file = script_file
54
62
self .http_type = 'function'
55
63
self ._is_http_function = False
@@ -83,14 +91,12 @@ def add_trigger(self, trigger: Trigger) -> None:
83
91
# function.json is complete
84
92
self ._bindings .append (trigger )
85
93
86
- def set_function_name (self , function_name : Optional [str ] = None ) -> None :
87
- """Set or update the name for the function if :param:`function_name`
88
- is not None. If not set, function name will default to python
89
- function name.
90
- :param function_name: Name the function set to.
94
+ def add_setting (self , setting : Setting ) -> None :
95
+ """Add a setting instance to the function.
96
+
97
+ :param setting: The setting object to add
91
98
"""
92
- if function_name :
93
- self ._name = function_name
99
+ self ._settings .append (setting )
94
100
95
101
def set_http_type (self , http_type : str ) -> None :
96
102
"""Set or update the http type for the function if :param:`http_type`
@@ -116,6 +122,36 @@ def get_bindings(self) -> List[Binding]:
116
122
"""
117
123
return self ._bindings
118
124
125
+ def get_setting (self , setting_name : str ) -> Optional [Setting ]:
126
+ """Get a specific setting attached to the function.
127
+
128
+ :param setting_name: The name of the setting to search for.
129
+ :return: The setting attached to the function (or None if not found).
130
+ """
131
+ for setting in self ._settings :
132
+ if setting .setting_name == setting_name :
133
+ return setting
134
+ return None
135
+
136
+ def get_settings_dict (self , setting_name ) -> Optional [Dict ]:
137
+ """Get a dictionary representation of a setting.
138
+
139
+ :param: setting_name: The name of the setting to search for.
140
+ :return: The dictionary representation of the setting (or None if not
141
+ found).
142
+ """
143
+ setting = self .get_setting (setting_name )
144
+ return setting .get_dict_repr () if setting else None
145
+
146
+ def get_function_name (self ) -> Optional [str ]:
147
+ """Get the name of the function.
148
+ :return: The name of the function.
149
+ """
150
+ function_name_setting = \
151
+ self .get_setting ("function_name" )
152
+ return function_name_setting .get_settings_value ("function_name" ) \
153
+ if function_name_setting else self ._name
154
+
119
155
def get_raw_bindings (self ) -> List [str ]:
120
156
return [json .dumps (b .get_dict_repr (), cls = StringifyEnumJsonEncoder )
121
157
for b in self ._bindings ]
@@ -145,13 +181,6 @@ def get_user_function(self) -> Callable[..., Any]:
145
181
"""
146
182
return self ._func
147
183
148
- def get_function_name (self ) -> str :
149
- """Get the function name.
150
-
151
- :return: Function name.
152
- """
153
- return self ._name
154
-
155
184
def get_function_json (self ) -> str :
156
185
"""Get the json stringified form of function.
157
186
@@ -170,11 +199,6 @@ def __init__(self, func, function_script_file):
170
199
def __call__ (self , * args , ** kwargs ):
171
200
pass
172
201
173
- def configure_function_name (self , function_name : str ) -> 'FunctionBuilder' :
174
- self ._function .set_function_name (function_name )
175
-
176
- return self
177
-
178
202
def configure_http_type (self , http_type : str ) -> 'FunctionBuilder' :
179
203
self ._function .set_http_type (http_type )
180
204
@@ -188,6 +212,10 @@ def add_binding(self, binding: Binding) -> 'FunctionBuilder':
188
212
self ._function .add_binding (binding = binding )
189
213
return self
190
214
215
+ def add_setting (self , setting : Setting ) -> 'FunctionBuilder' :
216
+ self ._function .add_setting (setting = setting )
217
+ return self
218
+
191
219
def _validate_function (self ,
192
220
auth_level : Optional [AuthLevel ] = None ) -> None :
193
221
"""
@@ -288,23 +316,6 @@ def decorator(func):
288
316
289
317
return decorator
290
318
291
- def function_name (self , name : str ) -> Callable [..., Any ]:
292
- """Set name of the :class:`Function` object.
293
-
294
- :param name: Name of the function.
295
- :return: Decorator function.
296
- """
297
-
298
- @self ._configure_function_builder
299
- def wrap (fb ):
300
- def decorator ():
301
- fb .configure_function_name (name )
302
- return fb
303
-
304
- return decorator ()
305
-
306
- return wrap
307
-
308
319
def http_type (self , http_type : str ) -> Callable [..., Any ]:
309
320
"""Set http type of the :class:`Function` object.
310
321
@@ -1938,6 +1949,80 @@ def decorator():
1938
1949
return wrap
1939
1950
1940
1951
1952
+ class SettingsApi (DecoratorApi , ABC ):
1953
+ """Interface to extend for using existing settings decorator in
1954
+ functions."""
1955
+
1956
+ def function_name (self , name : str ,
1957
+ setting_extra_fields : Dict [str , Any ] = {},
1958
+ ) -> Callable [..., Any ]:
1959
+ """Optional: Sets name of the :class:`Function` object. If not set,
1960
+ it will default to the name of the method name.
1961
+
1962
+ :param name: Name of the function.
1963
+ :param setting_extra_fields: Keyword arguments for specifying
1964
+ additional setting fields
1965
+ :return: Decorator function.
1966
+ """
1967
+
1968
+ @self ._configure_function_builder
1969
+ def wrap (fb ):
1970
+ def decorator ():
1971
+ fb .add_setting (setting = FunctionName (
1972
+ function_name = name ,
1973
+ ** setting_extra_fields ))
1974
+ return fb
1975
+
1976
+ return decorator ()
1977
+
1978
+ return wrap
1979
+
1980
+ def retry (self ,
1981
+ strategy : str ,
1982
+ max_retry_count : str ,
1983
+ delay_interval : Optional [str ] = None ,
1984
+ minimum_interval : Optional [str ] = None ,
1985
+ maximum_interval : Optional [str ] = None ,
1986
+ setting_extra_fields : Dict [str , Any ] = {},
1987
+ ) -> Callable [..., Any ]:
1988
+ """The retry decorator adds :class:`RetryPolicy` to the function
1989
+ settings object for building :class:`Function` object used in worker
1990
+ function indexing model. This is equivalent to defining RetryPolicy
1991
+ in the function.json which enables function to retry on failure.
1992
+ All optional fields will be given default value by function host when
1993
+ they are parsed by function host.
1994
+
1995
+ Ref: https://aka.ms/azure_functions_retries
1996
+
1997
+ :param strategy: The retry strategy to use.
1998
+ :param max_retry_count: The maximum number of retry attempts.
1999
+ :param delay_interval: The delay interval between retry attempts.
2000
+ :param minimum_interval: The minimum delay interval between retry
2001
+ attempts.
2002
+ :param maximum_interval: The maximum delay interval between retry
2003
+ attempts.
2004
+ :param setting_extra_fields: Keyword arguments for specifying
2005
+ additional setting fields.
2006
+ :return: Decorator function.
2007
+ """
2008
+
2009
+ @self ._configure_function_builder
2010
+ def wrap (fb ):
2011
+ def decorator ():
2012
+ fb .add_setting (setting = RetryPolicy (
2013
+ strategy = strategy ,
2014
+ max_retry_count = max_retry_count ,
2015
+ minimum_interval = minimum_interval ,
2016
+ maximum_interval = maximum_interval ,
2017
+ delay_interval = delay_interval ,
2018
+ ** setting_extra_fields ))
2019
+ return fb
2020
+
2021
+ return decorator ()
2022
+
2023
+ return wrap
2024
+
2025
+
1941
2026
class FunctionRegister (DecoratorApi , HttpFunctionsAuthLevelMixin , ABC ):
1942
2027
def __init__ (self , auth_level : Union [AuthLevel , str ], * args , ** kwargs ):
1943
2028
"""Interface for declaring top level function app class which will
@@ -1987,7 +2072,7 @@ def register_functions(self, function_container: DecoratorApi) -> None:
1987
2072
register_blueprint = register_functions
1988
2073
1989
2074
1990
- class FunctionApp (FunctionRegister , TriggerApi , BindingApi ):
2075
+ class FunctionApp (FunctionRegister , TriggerApi , BindingApi , SettingsApi ):
1991
2076
"""FunctionApp object used by worker function indexing model captures
1992
2077
user defined functions and metadata.
1993
2078
0 commit comments