diff --git a/.gitignore b/.gitignore index ff47d7f5f..aa1ac9ae5 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *.nupkg templates.json bin +.idea obj Publish diff --git a/Functions.Templates/Templates-v2/BlobTrigger-Python/blob_trigger_template.md b/Functions.Templates/Templates-v2/BlobTrigger-Python/blob_trigger_template.md index 208e68e21..6c78b0d1c 100644 --- a/Functions.Templates/Templates-v2/BlobTrigger-Python/blob_trigger_template.md +++ b/Functions.Templates/Templates-v2/BlobTrigger-Python/blob_trigger_template.md @@ -6,7 +6,7 @@ The Blob storage trigger starts a function when a new or updated blob is detecte ## Using the Template -Following is an example code snippet for Blob Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel) (currently in Preview). +Following is an example code snippet for Blob Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel). ```python import logging @@ -23,13 +23,33 @@ def test_function(myblob: func.InputStream): f"Blob Size: {myblob.length} bytes") ``` +This example uses SDK types to directly access the underlying BlobClient object provided by the Blob storage trigger: + +```python +import logging +import azure.functions as func +import azurefunctions.extensions.bindings.blob as blob + +app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION) + +@app.blob_trigger(arg_name="client", path="samples-workitems/{name}", + connection="BlobStorageConnection") +def blob_trigger(client: blob.BlobClient): + logging.info( + f"Python blob trigger function processed blob \n" + f"Properties: {client.get_blob_properties()}\n" + f"Blob content head: {client.download_blob().read(size=1)}" + ) +``` + To run the code snippet generated through the command palette, note the following: - The function application is defined and named `app`. - Confirm that the parameters within the trigger reflect values that correspond with your storage account. - The name of the file must be `function_app.py`. +- If you are using SDK-Type Bindings, make sure to include `azurefunctions-extensions-bindings-blob` in your `requirements.txt` file. -Note that Blob input and output bindings are also supported in Azure Functions. To learn more, see [Azure Blob storage bindings overview](https://aka.ms/azure-function-binding-storage-blob) +Note that Blob input and output bindings are also supported in Azure Functions. To learn more, see [Azure Blob storage bindings overview](https://aka.ms/azure-function-binding-storage-blob). ## Programming Model V2 diff --git a/Functions.Templates/Templates-v2/BlobTrigger-Python/function_app.py b/Functions.Templates/Templates-v2/BlobTrigger-Python/function_app.py index 84f501efa..efa5f241d 100644 --- a/Functions.Templates/Templates-v2/BlobTrigger-Python/function_app.py +++ b/Functions.Templates/Templates-v2/BlobTrigger-Python/function_app.py @@ -9,3 +9,16 @@ def $(FUNCTION_NAME_INPUT)(myblob: func.InputStream): logging.info(f"Python blob trigger function processed blob" f"Name: {myblob.name}" f"Blob Size: {myblob.length} bytes") + + +# This example uses SDK types to directly access the underlying BlobClient object provided by the Blob storage trigger. +# To use, uncomment the section below and add azurefunctions-extensions-bindings-blob to your requirements.txt file +# import azurefunctions.extensions.bindings.blob as blob +# @app.blob_trigger(arg_name="client", path="$(PATH_TO_BLOB_INPUT)", +# connection="$(CONNECTION_STRING_INPUT)") +# def $(FUNCTION_NAME_INPUT)(client: blob.BlobClient): +# logging.info( +# f"Python blob trigger function processed blob \n" +# f"Properties: {client.get_blob_properties()}\n" +# f"Blob content head: {client.download_blob().read(size=1)}" +# ) diff --git a/Functions.Templates/Templates-v2/BlobTrigger-Python/function_body.py b/Functions.Templates/Templates-v2/BlobTrigger-Python/function_body.py index 185aad931..3270c0a5c 100644 --- a/Functions.Templates/Templates-v2/BlobTrigger-Python/function_body.py +++ b/Functions.Templates/Templates-v2/BlobTrigger-Python/function_body.py @@ -5,3 +5,16 @@ def $(FUNCTION_NAME_INPUT)(myblob: func.InputStream): logging.info(f"Python blob trigger function processed blob" f"Name: {myblob.name}" f"Blob Size: {myblob.length} bytes") + + +# This example uses SDK types to directly access the underlying BlobClient object provided by the Blob storage trigger. +# To use, uncomment the section below and add azurefunctions-extensions-bindings-blob to your requirements.txt file +# import azurefunctions.extensions.bindings.blob as blob +# @app.blob_trigger(arg_name="client", path="$(PATH_TO_BLOB_INPUT)", +# connection="$(CONNECTION_STRING_INPUT)") +# def $(FUNCTION_NAME_INPUT)(client: blob.BlobClient): +# logging.info( +# f"Python blob trigger function processed blob \n" +# f"Properties: {client.get_blob_properties()}\n" +# f"Blob content head: {client.download_blob().read(size=1)}" +# ) diff --git a/Functions.Templates/Templates-v2/CosmosDbTrigger-Python/cosmosdb_trigger_template.md b/Functions.Templates/Templates-v2/CosmosDbTrigger-Python/cosmosdb_trigger_template.md index 4a3ad72a6..31e971257 100644 --- a/Functions.Templates/Templates-v2/CosmosDbTrigger-Python/cosmosdb_trigger_template.md +++ b/Functions.Templates/Templates-v2/CosmosDbTrigger-Python/cosmosdb_trigger_template.md @@ -6,7 +6,7 @@ The Azure Cosmos DB Trigger uses the Azure Cosmos DB Change Feed to listen for i ## Using the Template -Following is an example code snippet for Cosmos DB Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel) (currently in Preview). +Following is an example code snippet for Cosmos DB Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel). ```python import logging diff --git a/Functions.Templates/Templates-v2/DaprPublishOutputBinding-Python/dapr-publish-output_binding_template.md b/Functions.Templates/Templates-v2/DaprPublishOutputBinding-Python/dapr-publish-output_binding_template.md index f11533334..427832e50 100644 --- a/Functions.Templates/Templates-v2/DaprPublishOutputBinding-Python/dapr-publish-output_binding_template.md +++ b/Functions.Templates/Templates-v2/DaprPublishOutputBinding-Python/dapr-publish-output_binding_template.md @@ -6,7 +6,7 @@ With Dapr output binding, you can invoke external resources. An optional payload ## Using the Template -Following is an example code snippet for Dapr Service Invocation Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel) (currently in Preview). +Following is an example code snippet for Dapr Service Invocation Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel). ```python import datetime diff --git a/Functions.Templates/Templates-v2/DaprServiceInvocationTrigger-Python/dapr_service_invocation_trigger_template.md b/Functions.Templates/Templates-v2/DaprServiceInvocationTrigger-Python/dapr_service_invocation_trigger_template.md index bfa499bdd..33a9850b9 100644 --- a/Functions.Templates/Templates-v2/DaprServiceInvocationTrigger-Python/dapr_service_invocation_trigger_template.md +++ b/Functions.Templates/Templates-v2/DaprServiceInvocationTrigger-Python/dapr_service_invocation_trigger_template.md @@ -6,7 +6,7 @@ Using service invocation, your application can reliably and securely communicate ## Using the Template -Following is an example code snippet for Dapr Service Invocation Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel) (currently in Preview). +Following is an example code snippet for Dapr Service Invocation Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel). ```python import json diff --git a/Functions.Templates/Templates-v2/DaprTopicTrigger-Python/dapr-topic_trigger_template.md b/Functions.Templates/Templates-v2/DaprTopicTrigger-Python/dapr-topic_trigger_template.md index 74c1bb0c4..8ba4d36dd 100644 --- a/Functions.Templates/Templates-v2/DaprTopicTrigger-Python/dapr-topic_trigger_template.md +++ b/Functions.Templates/Templates-v2/DaprTopicTrigger-Python/dapr-topic_trigger_template.md @@ -6,7 +6,7 @@ Using `Dapr Topic Trigger`, your azure functions can react to a message publishe ## Using the Template -Following is an example code snippet for Dapr Topic Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel) (currently in Preview). +Following is an example code snippet for Dapr Topic Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel). ```python import json diff --git a/Functions.Templates/Templates-v2/EventGridBlobTrigger-Python/eventgrid_blob_trigger_template.md b/Functions.Templates/Templates-v2/EventGridBlobTrigger-Python/eventgrid_blob_trigger_template.md new file mode 100644 index 000000000..e3aef60a9 --- /dev/null +++ b/Functions.Templates/Templates-v2/EventGridBlobTrigger-Python/eventgrid_blob_trigger_template.md @@ -0,0 +1,44 @@ +# Azure Functions: Blob Trigger (using Event Grid) in Python + +## Blob Trigger (using Event Grid) + +The Blob storage trigger starts a function when a new or updated blob is detected. The blob contents are provided as input to the function. +Starting with extension version 5.x+, you can use an Event Grid event subscription on the container, which reduces latency. The Azure Blob storage trigger requires a general-purpose storage account. Storage V2 accounts with hierarchical namespaces are also supported. + +## Using the Template + +Following is an example code snippet for Blob Trigger (using Event Grid) using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel). + +```python +import logging +import azure.functions as func + +app = func.FunctionApp() + +@app.function_name(name="BlobTrigger1") +@app.blob_trigger(arg_name="myblob", path="samples-workitems/{name}", + source="EventGrid", connection="BlobStorageConnection") +def test_function(myblob: func.InputStream): + logging.info("Python blob trigger (using Event Grid) function processed blob \n" + f"Name: {myblob.name}\n" + f"Blob Size: {myblob.length} bytes") +``` + +To run the code snippet generated through the command palette, note the following: + +- The function application is defined and named `app`. +- Confirm that the parameters within the trigger reflect values that correspond with your storage account. +- The name of the file must be `function_app.py`. + +Note that Blob input and output bindings are also supported in Azure Functions. To learn more, see [Azure Blob storage bindings overview](https://aka.ms/azure-function-binding-storage-blob). +To learn more about Blob Triggers using Event Grid, see the [Trigger Azure Functions on blob containers using an event subscription Tutorial](https://learn.microsoft.com/en-us/azure/azure-functions/functions-event-grid-blob-trigger?pivots=programming-language-python) + +## Programming Model V2 + +The new programming model in Azure Functions Python delivers an experience that aligns with Python development principles, and subsequently with commonly used Python frameworks. + +The improved programming model requires fewer files than the default model, and specifically eliminates the need for a configuration file (`function.json`). Instead, triggers and bindings are represented in the `function_app.py` file as decorators. Moreover, functions can be logically organized with support for multiple functions to be stored in the same file. Functions within the same function application can also be stored in different files, and be referenced as blueprints. + +To learn more about using the new Python programming model for Azure Functions, see the [Azure Functions Python developer guide](https://aka.ms/pythondeveloperguide). Note that in addition to the documentation, [hints](https://aka.ms/functions-python-hints) are available in code editors that support type checking with PYI files. + +To learn more about the new programming model for Azure Functions in Python, see [Programming Models in Azure Functions](https://aka.ms/functions-programming-models). diff --git a/Functions.Templates/Templates-v2/EventGridBlobTrigger-Python/template.json b/Functions.Templates/Templates-v2/EventGridBlobTrigger-Python/template.json index a5b8603f2..94cd93225 100644 --- a/Functions.Templates/Templates-v2/EventGridBlobTrigger-Python/template.json +++ b/Functions.Templates/Templates-v2/EventGridBlobTrigger-Python/template.json @@ -201,7 +201,7 @@ { "name": "ShowMarkdownPreview", "type": "ShowMarkdownPreview", - "filePath" : "blob_trigger_template.md" + "filePath" : "eventgrid_blob_trigger_template.md" }, { "name": "readFileContent_BlueprintFile", diff --git a/Functions.Templates/Templates-v2/EventGridTrigger-Python/eventgrid_trigger_template.md b/Functions.Templates/Templates-v2/EventGridTrigger-Python/eventgrid_trigger_template.md index 05a4dfd9a..689b34ff1 100644 --- a/Functions.Templates/Templates-v2/EventGridTrigger-Python/eventgrid_trigger_template.md +++ b/Functions.Templates/Templates-v2/EventGridTrigger-Python/eventgrid_trigger_template.md @@ -5,7 +5,7 @@ The Event Grid function trigger can be used to respond to an event sent by an Event Grid source. You must have an event subscription to the source to receive events. When the function is triggered, it converts the event data into a JSON string which is then logged using the Python logging module. ## Using the Template -Following is an example code snippet for Event Grid Trigger using the Python programming model V2 (currently in Preview). +Following is an example code snippet for Event Grid Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel). ```python import azure.functions as func import logging diff --git a/Functions.Templates/Templates-v2/EventHubTrigger-Python/eventhub_trigger_template.md b/Functions.Templates/Templates-v2/EventHubTrigger-Python/eventhub_trigger_template.md index 36cb35163..68d4a5195 100644 --- a/Functions.Templates/Templates-v2/EventHubTrigger-Python/eventhub_trigger_template.md +++ b/Functions.Templates/Templates-v2/EventHubTrigger-Python/eventhub_trigger_template.md @@ -6,7 +6,7 @@ The Event Hub function trigger can be used to respond to an event sent to an eve ## Using the Template -Following is an example code snippet for Event Hub Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel) (currently in Preview). +Following is an example code snippet for Event Hub Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel). ```python import logging @@ -22,11 +22,31 @@ def test_function(myhub: func.EventHubEvent): myhub.get_body().decode('utf-8')) ``` +This example uses SDK types to directly access the underlying EventData object provided by the Event Hubs trigger (currently in Preview): + +```python +import logging +import azure.functions as func +import azurefunctions.extensions.bindings.eventhub as eh + +app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION) + +@app.event_hub_message_trigger( + arg_name="event", event_hub_name="EVENTHUB_NAME", connection="EventHubConnection" +) +def eventhub_trigger(event: eh.EventData): + logging.info( + "Python EventHub trigger processed an event %s", + event.body_as_str() + ) +``` + To run the code snippet generated through the command palette, note the following: - The function application is defined and named `app`. - Confirm that the parameters within the trigger reflect values that correspond with your storage account. - The name of the file must be `function_app.py`. +- If you are using SDK-Type Bindings, make sure to include `azurefunctions-extensions-bindings-eventhub` in your `requirements.txt` file. Note that Event Hub output bindings are also supported in Azure Functions. To learn more, see [Azure Event Hubs trigger and bindings for Azure Functions](https://aka.ms/azure-function-binding-event-hubs) diff --git a/Functions.Templates/Templates-v2/EventHubTrigger-Python/function_app.py b/Functions.Templates/Templates-v2/EventHubTrigger-Python/function_app.py index bd93c451c..fbfc58dae 100644 --- a/Functions.Templates/Templates-v2/EventHubTrigger-Python/function_app.py +++ b/Functions.Templates/Templates-v2/EventHubTrigger-Python/function_app.py @@ -8,3 +8,16 @@ def $(FUNCTION_NAME_INPUT)(azeventhub: func.EventHubEvent): logging.info('Python EventHub trigger processed an event: %s', azeventhub.get_body().decode('utf-8')) + + +# This example uses SDK types to directly access the underlying EventData object provided by the Event Hubs trigger. +# To use, uncomment the section below and add azurefunctions-extensions-bindings-eventhub to your requirements.txt file +# import azurefunctions.extensions.bindings.eventhub as eh +# @app.event_hub_message_trigger( +# arg_name="event", event_hub_name="$(EVENTHUB_NAME_INPUT)", connection="$(CONNECTION_STRING_INPUT)" +# ) +# def $(FUNCTION_NAME_INPUT)(event: eh.EventData): +# logging.info( +# "Python EventHub trigger processed an event %s", +# event.body_as_str() +# ) diff --git a/Functions.Templates/Templates-v2/EventHubTrigger-Python/function_body.py b/Functions.Templates/Templates-v2/EventHubTrigger-Python/function_body.py index 02de04e65..00c115ff5 100644 --- a/Functions.Templates/Templates-v2/EventHubTrigger-Python/function_body.py +++ b/Functions.Templates/Templates-v2/EventHubTrigger-Python/function_body.py @@ -4,3 +4,16 @@ def $(FUNCTION_NAME_INPUT)(azeventhub: func.EventHubEvent): logging.info('Python EventHub trigger processed an event: %s', azeventhub.get_body().decode('utf-8')) + + +# This example uses SDK types to directly access the underlying EventData object provided by the Event Hubs trigger. +# To use, uncomment the section below and add azurefunctions-extensions-bindings-eventhub to your requirements.txt file +# import azurefunctions.extensions.bindings.eventhub as eh +# @app.event_hub_message_trigger( +# arg_name="event", event_hub_name="$(EVENTHUB_NAME_INPUT)", connection="$(CONNECTION_STRING_INPUT)" +# ) +# def $(FUNCTION_NAME_INPUT)(event: eh.EventData): +# logging.info( +# "Python EventHub trigger processed an event %s", +# event.body_as_str() +# ) diff --git a/Functions.Templates/Templates-v2/HttpTrigger-Python/http_trigger_template.md b/Functions.Templates/Templates-v2/HttpTrigger-Python/http_trigger_template.md index 1d906d718..6dde43b5a 100644 --- a/Functions.Templates/Templates-v2/HttpTrigger-Python/http_trigger_template.md +++ b/Functions.Templates/Templates-v2/HttpTrigger-Python/http_trigger_template.md @@ -6,7 +6,7 @@ The HTTP trigger lets you invoke a function with an HTTP request. You can use an ## Using the Template -Following is an example code snippet for HTTP Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel) (currently in Preview). +Following is an example code snippet for HTTP Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel). ```python import azure.functions as func diff --git a/Functions.Templates/Templates-v2/QueueTrigger-Python/queue_trigger_template.md b/Functions.Templates/Templates-v2/QueueTrigger-Python/queue_trigger_template.md index d589a0ba2..a9f88186c 100644 --- a/Functions.Templates/Templates-v2/QueueTrigger-Python/queue_trigger_template.md +++ b/Functions.Templates/Templates-v2/QueueTrigger-Python/queue_trigger_template.md @@ -6,7 +6,7 @@ The queue storage trigger runs a function as messages are added to Azure Queue s ## Using the Template -Following is an example code snippet for Queue Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel) (currently in Preview). +Following is an example code snippet for Queue Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel). ```python import logging diff --git a/Functions.Templates/Templates-v2/ServiceBusQueueTrigger-Python/function_app.py b/Functions.Templates/Templates-v2/ServiceBusQueueTrigger-Python/function_app.py index a42b82742..aa807b746 100644 --- a/Functions.Templates/Templates-v2/ServiceBusQueueTrigger-Python/function_app.py +++ b/Functions.Templates/Templates-v2/ServiceBusQueueTrigger-Python/function_app.py @@ -8,3 +8,17 @@ def $(FUNCTION_NAME_INPUT)(azservicebus: func.ServiceBusMessage): logging.info('Python ServiceBus Queue trigger processed a message: %s', azservicebus.get_body().decode('utf-8')) + + +# This example uses SDK types to directly access the underlying ServiceBusReceivedMessage object provided by the Service Bus trigger. +# To use, uncomment the section below and add azurefunctions-extensions-bindings-servicebus to your requirements.txt file +# import azurefunctions.extensions.bindings.servicebus as servicebus +# @app.service_bus_queue_trigger(arg_name="receivedmessage", +# queue_name="$(SERVICEBUS_NAME_INPUT)", +# connection="$(CONNECTION_STRING_INPUT)") +# def $(FUNCTION_NAME_INPUT)(receivedmessage: servicebus.ServiceBusReceivedMessage): +# logging.info("Python ServiceBus queue trigger processed message.") +# logging.info("Receiving: %s\n" +# "Body: %s\n", +# receivedmessage, +# receivedmessage.body) diff --git a/Functions.Templates/Templates-v2/ServiceBusQueueTrigger-Python/function_body.py b/Functions.Templates/Templates-v2/ServiceBusQueueTrigger-Python/function_body.py index 9aca9d2c7..5517eeb42 100644 --- a/Functions.Templates/Templates-v2/ServiceBusQueueTrigger-Python/function_body.py +++ b/Functions.Templates/Templates-v2/ServiceBusQueueTrigger-Python/function_body.py @@ -4,3 +4,17 @@ def $(FUNCTION_NAME_INPUT)(azservicebus: func.ServiceBusMessage): logging.info('Python ServiceBus Queue trigger processed a message: %s', azservicebus.get_body().decode('utf-8')) + + +# This example uses SDK types to directly access the underlying ServiceBusReceivedMessage object provided by the Service Bus trigger. +# To use, uncomment the section below and add azurefunctions-extensions-bindings-servicebus to your requirements.txt file +# import azurefunctions.extensions.bindings.servicebus as servicebus +# @app.service_bus_queue_trigger(arg_name="receivedmessage", +# queue_name="$(SERVICEBUS_NAME_INPUT)", +# connection="$(CONNECTION_STRING_INPUT)") +# def $(FUNCTION_NAME_INPUT)(receivedmessage: servicebus.ServiceBusReceivedMessage): +# logging.info("Python ServiceBus queue trigger processed message.") +# logging.info("Receiving: %s\n" +# "Body: %s\n", +# receivedmessage, +# receivedmessage.body) diff --git a/Functions.Templates/Templates-v2/ServiceBusQueueTrigger-Python/servicebusqueue_trigger_template.md b/Functions.Templates/Templates-v2/ServiceBusQueueTrigger-Python/servicebusqueue_trigger_template.md index 9281d589a..e528cfee2 100644 --- a/Functions.Templates/Templates-v2/ServiceBusQueueTrigger-Python/servicebusqueue_trigger_template.md +++ b/Functions.Templates/Templates-v2/ServiceBusQueueTrigger-Python/servicebusqueue_trigger_template.md @@ -6,7 +6,7 @@ Use the Service Bus Queue trigger to respond to messages from a Service Bus queu ## Using the Template -Following is an example code snippet for Service Bus Queue Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel) (currently in Preview). +Following is an example code snippet for Service Bus Queue Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel). ```python import logging @@ -21,11 +21,28 @@ def test_function(msg: func.ServiceBusMessage): msg.get_body().decode('utf-8')) ``` +This example uses SDK types to directly access the underlying ServiceBusReceivedMessage object provided by the Service Bus trigger (currently in Preview): + +```python +import azurefunctions.extensions.bindings.servicebus as servicebus + +@app.service_bus_queue_trigger(arg_name="receivedmessage", + queue_name="QUEUE_NAME", + connection="SERVICEBUS_CONNECTION") +def servicebus_queue_trigger(receivedmessage: servicebus.ServiceBusReceivedMessage): + logging.info("Python ServiceBus queue trigger processed message.") + logging.info("Receiving: %s\n" + "Body: %s\n", + receivedmessage, + receivedmessage.body) +``` + To run the code snippet generated through the command palette, note the following: - The function application is defined and named `app`. - Confirm that the parameters within the trigger reflect values that correspond with your storage account. - The name of the file must be `function_app.py`. +- If you are using SDK-Type Bindings, make sure to include `azurefunctions-extensions-bindings-servicebus` in your `requirements.txt` file. Note that Service Bus output bindings are also supported in Azure Functions. To learn more, see [Azure Service Bus bindings for Azure Functions](https://aka.ms/azure-function-binding-service-bus) diff --git a/Functions.Templates/Templates-v2/ServiceBusTopicTrigger-Python/function_app.py b/Functions.Templates/Templates-v2/ServiceBusTopicTrigger-Python/function_app.py index 11a15c59b..61450873f 100644 --- a/Functions.Templates/Templates-v2/ServiceBusTopicTrigger-Python/function_app.py +++ b/Functions.Templates/Templates-v2/ServiceBusTopicTrigger-Python/function_app.py @@ -8,3 +8,18 @@ def $(FUNCTION_NAME_INPUT)(azservicebus: func.ServiceBusMessage): logging.info('Python ServiceBus Topic trigger processed a message: %s', azservicebus.get_body().decode('utf-8')) + + +# This example uses SDK types to directly access the underlying ServiceBusReceivedMessage object provided by the Service Bus trigger. +# To use, uncomment the section below and add azurefunctions-extensions-bindings-servicebus to your requirements.txt file +# import azurefunctions.extensions.bindings.servicebus as servicebus +# @app.service_bus_topic_trigger(arg_name="receivedmessage", +# topic_name="$(SERVICEBUS_NAME_INPUT)", +# connection="$(CONNECTION_STRING_INPUT)", +# subscription_name="$(SERVICEBUS_SUBSCRIPTION_NAME_INPUT)") +# def $(FUNCTION_NAME_INPUT)(receivedmessage: servicebus.ServiceBusReceivedMessage): +# logging.info("Python ServiceBus topic trigger processed message.") +# logging.info("Receiving: %s\n" +# "Body: %s\n", +# receivedmessage, +# receivedmessage.body) diff --git a/Functions.Templates/Templates-v2/ServiceBusTopicTrigger-Python/function_body.py b/Functions.Templates/Templates-v2/ServiceBusTopicTrigger-Python/function_body.py index 56e55a3f9..be037c022 100644 --- a/Functions.Templates/Templates-v2/ServiceBusTopicTrigger-Python/function_body.py +++ b/Functions.Templates/Templates-v2/ServiceBusTopicTrigger-Python/function_body.py @@ -4,3 +4,18 @@ def $(FUNCTION_NAME_INPUT)(azservicebus: func.ServiceBusMessage): logging.info('Python ServiceBus Topic trigger processed a message: %s', azservicebus.get_body().decode('utf-8')) + + +# This example uses SDK types to directly access the underlying ServiceBusReceivedMessage object provided by the Service Bus trigger. +# To use, uncomment the section below and add azurefunctions-extensions-bindings-servicebus to your requirements.txt file +# import azurefunctions.extensions.bindings.servicebus as servicebus +# @app.service_bus_topic_trigger(arg_name="receivedmessage", +# topic_name="$(SERVICEBUS_NAME_INPUT)", +# connection="$(CONNECTION_STRING_INPUT)", +# subscription_name="$(SERVICEBUS_SUBSCRIPTION_NAME_INPUT)") +# def $(FUNCTION_NAME_INPUT)(receivedmessage: servicebus.ServiceBusReceivedMessage): +# logging.info("Python ServiceBus topic trigger processed message.") +# logging.info("Receiving: %s\n" +# "Body: %s\n", +# receivedmessage, +# receivedmessage.body) diff --git a/Functions.Templates/Templates-v2/ServiceBusTopicTrigger-Python/servicebustopic_trigger_template.md b/Functions.Templates/Templates-v2/ServiceBusTopicTrigger-Python/servicebustopic_trigger_template.md index d5c4a4e6a..8347b515f 100644 --- a/Functions.Templates/Templates-v2/ServiceBusTopicTrigger-Python/servicebustopic_trigger_template.md +++ b/Functions.Templates/Templates-v2/ServiceBusTopicTrigger-Python/servicebustopic_trigger_template.md @@ -6,7 +6,7 @@ Use the Service Bus Queue trigger to respond to messages from a Service Bus topi ## Using the Template -Following is an example code snippet for Service Bus Topic Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel) (currently in Preview). +Following is an example code snippet for Service Bus Topic Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel). ```python import logging @@ -22,11 +22,33 @@ def test_function(message: func.ServiceBusMessage): logging.info("Message Body: " + message_body) ``` +This example uses SDK types to directly access the underlying ServiceBusReceivedMessage object provided by the Service Bus trigger (currently in Preview): + +```python +import logging +import azure.functions as func +import azurefunctions.extensions.bindings.servicebus as servicebus + +app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION) + +@app.service_bus_topic_trigger(arg_name="receivedmessage", + topic_name="TOPIC_NAME", + connection="SERVICEBUS_CONNECTION", + subscription_name="SUBSCRIPTION_NAME") +def servicebus_topic_trigger(receivedmessage: servicebus.ServiceBusReceivedMessage): + logging.info("Python ServiceBus topic trigger processed message.") + logging.info("Receiving: %s\n" + "Body: %s\n", + receivedmessage, + receivedmessage.body) +``` + To run the code snippet generated through the command palette, note the following: - The function application is defined and named `app`. - Confirm that the parameters within the trigger reflect values that correspond with your storage account. - The name of the file must be `function_app.py`. +- If you are using SDK-Type Bindings, make sure to include `azurefunctions-extensions-bindings-servicebus` in your `requirements.txt` file. Note that Service Bus output bindings are also supported in Azure Functions. To learn more, see [Azure Service Bus bindings for Azure Functions](https://aka.ms/azure-function-binding-service-bus) diff --git a/Functions.Templates/Templates-v2/TimerTrigger-Python/timer_trigger_template.md b/Functions.Templates/Templates-v2/TimerTrigger-Python/timer_trigger_template.md index 06e9fc7bd..ba69b5abb 100644 --- a/Functions.Templates/Templates-v2/TimerTrigger-Python/timer_trigger_template.md +++ b/Functions.Templates/Templates-v2/TimerTrigger-Python/timer_trigger_template.md @@ -6,7 +6,7 @@ A timer trigger lets you run a function on a schedule. ## Using the Template -Following is an example code snippet for Timer Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel) (currently in Preview). +Following is an example code snippet for Timer Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel). ```python import datetime