Reputation: 25
I have a azure python function : It gets triggered by HTTP , responds with HTTP response and puts the message in the Azure Service Bus Queue.
Function.json: for outbound Azure Service bus
{
"type": "serviceBus",
"connection": "ServiceBus",
"name": "outputMessage",
"queueName": "testqueue",
"accessRights": "send",
"direction": "out"
}
I have function as
def main(req: func.HttpRequest, outputMessage: func.Out[func.ServiceBusMessage]) -> str:
I get below error: Result: Failure Exception: FunctionLoadError: cannot load the HttpTrg function: type of outputMessage binding in function.json "serviceBus" does not match its Python annotation "ServiceBusMessage"
Question: 1. What should be the python annotation for Azure Service Bus outbound ?
def main( , outputMessage: func.Out[func.ServiceBusMessage])
Can I keep -> str for Azure Service Bus ?
func.Out[func.ServiceBusMessage]) -> str
Can i use the set method to send message like :
outputMessage.set(text)
"To produce multiple outputs, use the set() method provided by the azure.functions.Out interface to assign a value to the binding" -> will this work?
Thanks Sandeep
Upvotes: 2
Views: 1568
Reputation: 3967
Using the Azure ServiceBusMessage class as an outbound parameter is not supported:
Attributes are not supported by Python.
Use the Azure Service Bus SDK rather than the built-in output binding.
Azure Service Bus output binding for Azure Functions
ServiceBusMessage can only be used for Trigger binding:
import azure.functions as func
def main(msg: func.ServiceBusMessage):
The queue message is available to the function via a parameter typed as func.ServiceBusMessage. The Service Bus message is passed into the function as either a string or JSON object.
Azure Service Bus trigger for Azure Functions
Upvotes: 0
Reputation: 60871
Another example of being able to do output binding to service bus with python:
// function.json
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
},
{
"type": "serviceBus",
"direction": "out",
"connection": "AzureWebJobsServiceBusConnectionString",
"name": "msg",
"queueName": "outqueue"
}
]
}
.
# __init__.py
import azure.functions as func
def main(req: func.HttpRequest, msg: func.Out[str]) -> func.HttpResponse:
msg.set(req.get_body())
return 'OK'
Upvotes: 0
Reputation: 60871
When you say Azure Service Queue you probably mean Azure Storage Queue.
The binding serviceBus
is specifically for Service Bus, not Storage Queues.
You need to use queueName
.
Here's an example of output binding. Firstly, we have the function.json
file:
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
},
{
"type": "queue",
"direction": "out",
"name": "msg",
"queueName": "outqueue",
"connection": "AzureWebJobsStorage"
}
]
}
Now we can use it like so:
import logging
import azure.functions as func
def main(req: func.HttpRequest, msg: func.Out[func.QueueMessage]) -> str:
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
msg.set(name)
return func.HttpResponse(f"Hello {name}!")
else:
return func.HttpResponse(
"Please pass a name on the query string or in the request body",
status_code=400
)
Output Binding with Python to Service Bus
Here's your function.json
code:
{
"name": "outputSbQueue",
"type": "serviceBus",
"queueName": "testqueue",
"connection": "MyServiceBusConnection",
"direction": "out"
}
Also, the following references may be helpful for you:
Azure Function - Python - ServiceBus Output Binding - Setting Custom Properties https://github.com/yokawasa/azure-functions-python-samples/blob/master/docs/quickstart-v2-python-functions.md https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python https://azure.microsoft.com/en-us/blog/taking-a-closer-look-at-python-support-for-azure-functions/
https://unofficialism.info/posts/quick-start-with-azure-function-v2-python-preview/
Sending messages to service bus queue without an explicit binding
from azure.servicebus import QueueClient, Message
# Create the QueueClient
queue_client = QueueClient.from_connection_string(
"<CONNECTION STRING>", "<QUEUE NAME>")
# Send a test message to the queue
msg = Message(b'Test Message')
queue_client.send(msg)
Upvotes: 0