Skip to content

Commit 4ef4b25

Browse files
peterstone2017YunchuWang
authored andcommitted
provide examples of extra fields in route decorator, refactor is_http_trigger method
1 parent c09cfe4 commit 4ef4b25

File tree

5 files changed

+37
-28
lines changed

5 files changed

+37
-28
lines changed

azure/functions/decorators/core.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33
from abc import ABC, abstractmethod
4-
from typing import Dict, Optional
4+
from typing import Dict, Optional, Type
55

6-
from azure.functions.decorators.utils import to_camel_case, \
6+
from .utils import to_camel_case, \
77
ABCBuildDictMeta, StringifyEnum
88

99
SCRIPT_FILE_NAME = "function_app.py"
@@ -145,3 +145,8 @@ class OutputBinding(Binding, ABC, metaclass=ABCBuildDictMeta):
145145
def __init__(self, name, data_type) -> None:
146146
super().__init__(direction=BindingDirection.OUT,
147147
name=name, data_type=data_type)
148+
149+
150+
def is_supported_trigger_type(trigger: Trigger, trigger_type: Type[Trigger]):
151+
return isinstance(trigger, trigger_type) or \
152+
trigger.get_binding_name() == trigger_type.get_binding_name()

azure/functions/decorators/function_app.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
from azure.functions.decorators.blob import BlobTrigger, BlobInput, BlobOutput
88
from azure.functions.decorators.core import Binding, Trigger, DataType, \
9-
AuthLevel, SCRIPT_FILE_NAME, Cardinality, AccessRights
9+
AuthLevel, SCRIPT_FILE_NAME, Cardinality, AccessRights, \
10+
is_supported_trigger_type
1011
from azure.functions.decorators.cosmosdb import CosmosDBTrigger, \
1112
CosmosDBOutput, CosmosDBInput
1213
from azure.functions.decorators.eventhub import EventHubTrigger, EventHubOutput
1314
from azure.functions.decorators.http import HttpTrigger, HttpOutput, \
14-
HttpMethod, is_http_trigger
15+
HttpMethod
1516
from azure.functions.decorators.queue import QueueTrigger, QueueOutput
1617
from azure.functions.decorators.servicebus import ServiceBusQueueTrigger, \
1718
ServiceBusQueueOutput, ServiceBusTopicTrigger, \
@@ -178,8 +179,9 @@ def _validate_function(self) -> None:
178179
f"Function {function_name} trigger {trigger} not present"
179180
f" in bindings {bindings}")
180181

181-
if is_http_trigger(trigger) and getattr(trigger, 'route',
182-
None) is None:
182+
# Set route to function name if unspecified in the http trigger
183+
if is_supported_trigger_type(trigger, HttpTrigger) \
184+
and getattr(trigger, 'route', None) is None:
183185
setattr(trigger, 'route', self._function.get_function_name())
184186

185187
def build(self) -> Function:
@@ -369,9 +371,11 @@ def route(self,
369371
on the request in order to invoke the function.
370372
:return: Decorator function.
371373
:param trigger_extra_fields: Additional fields to include in trigger
372-
json.
374+
json. For example,
375+
>>> data_type='STRING' # 'dataType': 'STRING' in trigger json
373376
:param binding_extra_fields: Additional fields to include in binding
374-
json.
377+
json. For example,
378+
>>> data_type='STRING' # 'dataType': 'STRING' in binding json
375379
"""
376380

377381
@self._configure_function_builder

azure/functions/decorators/http.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,3 @@ def __init__(self,
4646
data_type: Optional[DataType] = None,
4747
**kwargs) -> None:
4848
super().__init__(name=name, data_type=data_type)
49-
50-
51-
def is_http_trigger(trigger):
52-
return isinstance(trigger, HttpTrigger) or \
53-
trigger.get_binding_name() == HTTP_TRIGGER

tests/decorators/test_http.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
from azure.functions.decorators.constants import HTTP_TRIGGER, HTTP_OUTPUT
66
from azure.functions.decorators.core import BindingDirection, DataType, \
77
AuthLevel
8-
from azure.functions.decorators.custom import CustomTrigger
98
from azure.functions.decorators.http import HttpTrigger, HttpOutput, \
10-
HttpMethod, is_http_trigger
9+
HttpMethod
1110

1211

1312
class TestHttp(unittest.TestCase):
@@ -49,14 +48,3 @@ def test_http_output_valid_creation(self):
4948
"name": "req",
5049
"dataType": DataType.UNDEFINED,
5150
})
52-
53-
def test_is_http_trigger_binding_name(self):
54-
self.assertTrue(
55-
is_http_trigger(CustomTrigger(name='req', type=HTTP_TRIGGER)))
56-
57-
def test_is_http_trigger_instance(self):
58-
self.assertTrue(is_http_trigger(HttpTrigger(name='req')))
59-
60-
def test_is_not_http_trigger(self):
61-
self.assertFalse(
62-
is_http_trigger(CustomTrigger(name='req', type="dummy")))

tests/decorators/test_utils.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
from azure.functions import HttpMethod
66
from azure.functions.decorators import utils
7-
from azure.functions.decorators.core import DataType
7+
from azure.functions.decorators.constants import HTTP_TRIGGER
8+
from azure.functions.decorators.core import DataType, is_supported_trigger_type
9+
from azure.functions.decorators.custom import CustomTrigger
10+
from azure.functions.decorators.http import HttpTrigger
811
from azure.functions.decorators.utils import to_camel_case, BuildDictMeta, \
912
is_snake_case, is_word
1013

@@ -152,7 +155,7 @@ def test_clean_nones_nested(self):
152155
{
153156
"hello2": ["dummy1", "dummy2", ["dummy3"], {}],
154157
"hello4": {"dummy5": "pass1"}
155-
} # NoQA
158+
} # NoQA
156159
)
157160

158161
def test_add_to_dict_no_args(self):
@@ -200,3 +203,17 @@ def get_dict_repr(self):
200203
self.assertCountEqual(getattr(test_obj, 'init_params'),
201204
{'self', 'arg1', 'arg2'})
202205
self.assertEqual(test_obj.get_dict_repr(), {"world": ["dummy"]})
206+
207+
def test_is_supported_trigger_binding_name(self):
208+
self.assertTrue(
209+
is_supported_trigger_type(
210+
CustomTrigger(name='req', type=HTTP_TRIGGER), HttpTrigger))
211+
212+
def test_is_supported_trigger_instance(self):
213+
self.assertTrue(
214+
is_supported_trigger_type(HttpTrigger(name='req'), HttpTrigger))
215+
216+
def test_is_not_supported_trigger_type(self):
217+
self.assertFalse(
218+
is_supported_trigger_type(CustomTrigger(name='req', type="dummy"),
219+
HttpTrigger))

0 commit comments

Comments
 (0)