Skip to content

Commit 6af952d

Browse files
authored
Merge pull request #84058 from craigshoemaker/crs-bindings-python
Add Python binding examples
2 parents 5ac57dd + 6ffa979 commit 6af952d

File tree

3 files changed

+267
-0
lines changed

3 files changed

+267
-0
lines changed

articles/azure-functions/functions-bindings-cosmosdb-v2.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,6 +1742,7 @@ See the language-specific examples:
17421742
* [F#](#output---f-examples)
17431743
* [Java](#output---java-examples)
17441744
* [JavaScript](#output---javascript-examples)
1745+
* [Python](#output---python-examples)
17451746

17461747
See also the [input example](#input---c-examples) that uses `DocumentClient`.
17471748

@@ -2272,6 +2273,57 @@ The following example shows a Java function that writes multiple documents to Co
22722273

22732274
In the [Java functions runtime library](/java/api/overview/azure/functions/runtime), use the `@CosmosDBOutput` annotation on parameters that will be written to Cosmos DB. The annotation parameter type should be ```OutputBinding<T>```, where T is either a native Java type or a POJO.
22742275

2276+
### Output - Python examples
2277+
2278+
The following example demonstrates how to write a document to an Azure CosmosDB database as the output of a function.
2279+
2280+
The binding definition is defined in *function.json* where *type* is set to `cosmosDB`.
2281+
2282+
```json
2283+
{
2284+
"scriptFile": "__init__.py",
2285+
"bindings": [
2286+
{
2287+
"authLevel": "function",
2288+
"type": "httpTrigger",
2289+
"direction": "in",
2290+
"name": "req",
2291+
"methods": [
2292+
"get",
2293+
"post"
2294+
]
2295+
},
2296+
{
2297+
"type": "cosmosDB",
2298+
"direction": "out",
2299+
"name": "doc",
2300+
"databaseName": "demodb",
2301+
"collectionName": "data",
2302+
"createIfNotExists": "true",
2303+
"connectionStringSetting": "AzureCosmosDBConnectionString"
2304+
},
2305+
{
2306+
"type": "http",
2307+
"direction": "out",
2308+
"name": "$return"
2309+
}
2310+
]
2311+
}
2312+
```
2313+
2314+
To write to the database, pass a document object to the `set` method of the database parameter.
2315+
2316+
```python
2317+
import azure.functions as func
2318+
2319+
def main(req: func.HttpRequest, doc: func.Out[func.Document]) -> func.HttpResponse:
2320+
2321+
request_body = req.get_body()
2322+
2323+
doc.set(func.Document.from_json(request_body))
2324+
2325+
return 'OK'
2326+
```
22752327

22762328
## Output - attributes
22772329

articles/azure-functions/functions-bindings-service-bus.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ See the language-specific example:
4646
* [F#](#trigger---f-example)
4747
* [Java](#trigger---java-example)
4848
* [JavaScript](#trigger---javascript-example)
49+
* [Python](#trigger---python-example)
4950

5051
### Trigger - C# example
5152

@@ -209,6 +210,57 @@ module.exports = function(context, myQueueItem) {
209210
};
210211
```
211212

213+
### Trigger - Python example
214+
215+
The following example demonstrates how to read a ServiceBus queue message via a trigger.
216+
217+
A ServiceBus binding is defined in *function.json* where *type* is set to `serviceBusTrigger`.
218+
219+
```json
220+
{
221+
"scriptFile": "__init__.py",
222+
"bindings": [
223+
{
224+
"name": "msg",
225+
"type": "serviceBusTrigger",
226+
"direction": "in",
227+
"queueName": "inputqueue",
228+
"connection": "AzureServiceBusConnectionString"
229+
}
230+
]
231+
}
232+
```
233+
234+
The code in *_\_init_\_.py* declares a parameter as `func.ServiceBusMessage` which allows you to read the queue message in your function.
235+
236+
```python
237+
import azure.functions as func
238+
239+
import logging
240+
import json
241+
242+
def main(msg: func.ServiceBusMessage):
243+
logging.info('Python ServiceBus queue trigger processed message.')
244+
245+
result = json.dumps({
246+
'message_id': msg.message_id,
247+
'body': msg.get_body().decode('utf-8'),
248+
'content_type': msg.content_type,
249+
'expiration_time': msg.expiration_time,
250+
'label': msg.label,
251+
'partition_key': msg.partition_key,
252+
'reply_to': msg.reply_to,
253+
'reply_to_session_id': msg.reply_to_session_id,
254+
'scheduled_enqueue_time': msg.scheduled_enqueue_time,
255+
'session_id': msg.session_id,
256+
'time_to_live': msg.time_to_live,
257+
'to': msg.to,
258+
'user_properties': msg.user_properties,
259+
})
260+
261+
logging.info(result)
262+
```
263+
212264
## Trigger - attributes
213265

214266
In [C# class libraries](functions-dotnet-class-library.md), use the following attributes to configure a Service Bus trigger:
@@ -365,6 +417,7 @@ See the language-specific example:
365417
* [F#](#output---f-example)
366418
* [Java](#output---java-example)
367419
* [JavaScript](#output---javascript-example)
420+
* [Python](#output---python-example)
368421

369422
### Output - C# example
370423

@@ -555,6 +608,56 @@ module.exports = function (context, myTimer) {
555608
};
556609
```
557610

611+
### Output - Python example
612+
613+
The following example demonstrates how to write out to a ServiceBus queue in Python.
614+
615+
A ServiceBue binding definition is defined in *function.json* where *type* is set to `serviceBus`.
616+
617+
```json
618+
{
619+
"scriptFile": "__init__.py",
620+
"bindings": [
621+
{
622+
"authLevel": "function",
623+
"type": "httpTrigger",
624+
"direction": "in",
625+
"name": "req",
626+
"methods": [
627+
"get",
628+
"post"
629+
]
630+
},
631+
{
632+
"type": "http",
633+
"direction": "out",
634+
"name": "$return"
635+
},
636+
{
637+
"type": "serviceBus",
638+
"direction": "out",
639+
"connection": "AzureServiceBusConnectionString",
640+
"name": "msg",
641+
"queueName": "outqueue"
642+
}
643+
]
644+
}
645+
```
646+
647+
In *_\_init_\_.py*, you can write out a message to the queue by passing a value to the `set` method.
648+
649+
```python
650+
import azure.functions as func
651+
652+
def main(req: func.HttpRequest, msg: func.Out[str]) -> func.HttpResponse:
653+
654+
input_msg = req.params.get('message')
655+
656+
msg.set(input_msg)
657+
658+
return 'OK'
659+
```
660+
558661
## Output - attributes
559662

560663
In [C# class libraries](functions-dotnet-class-library.md), use the [ServiceBusAttribute](https://github.com/Azure/azure-webjobs-sdk/blob/master/src/Microsoft.Azure.WebJobs.Extensions.ServiceBus/ServiceBusAttribute.cs).

articles/azure-functions/functions-bindings-storage-queue.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ See the language-specific example:
5050
* [C# script (.csx)](#trigger---c-script-example)
5151
* [JavaScript](#trigger---javascript-example)
5252
* [Java](#trigger---java-example)
53+
* [Python](#trigger---python-example)
5354

5455
### Trigger - C# example
5556

@@ -184,6 +185,54 @@ The following Java example shows a storage queue trigger functions which logs th
184185
}
185186
```
186187

188+
### Trigger - Python example
189+
190+
The following example demonstrates how to read a queue message passed to a function via a trigger.
191+
192+
A Storage queue trigger is defined in *function.json* where *type* is set to `queueTrigger`.
193+
194+
```json
195+
{
196+
"scriptFile": "__init__.py",
197+
"bindings": [
198+
{
199+
"name": "msg",
200+
"type": "queueTrigger",
201+
"direction": "in",
202+
"queueName": "messages",
203+
"connection": "AzureStorageQueuesConnectionString"
204+
}
205+
]
206+
}
207+
```
208+
209+
The code *_\_init_\_.py* declares a parameter as `func.ServiceBusMessage` which allows you to read the queue message in your function.
210+
211+
```python
212+
import logging
213+
import json
214+
215+
import azure.functions as func
216+
217+
def main(msg: func.QueueMessage):
218+
logging.info('Python queue trigger function processed a queue item.')
219+
220+
result = json.dumps({
221+
'id': msg.id,
222+
'body': msg.get_body().decode('utf-8'),
223+
'expiration_time': (msg.expiration_time.isoformat()
224+
if msg.expiration_time else None),
225+
'insertion_time': (msg.insertion_time.isoformat()
226+
if msg.insertion_time else None),
227+
'time_next_visible': (msg.time_next_visible.isoformat()
228+
if msg.time_next_visible else None),
229+
'pop_receipt': msg.pop_receipt,
230+
'dequeue_count': msg.dequeue_count
231+
})
232+
233+
logging.info(result)
234+
```
235+
187236
## Trigger - attributes
188237

189238
In [C# class libraries](functions-dotnet-class-library.md), use the following attributes to configure a queue trigger:
@@ -315,6 +364,7 @@ See the language-specific example:
315364
* [C# script (.csx)](#output---c-script-example)
316365
* [JavaScript](#output---javascript-example)
317366
* [Java](#output---java-example)
367+
* [Python](#output---python-example)
318368

319369
### Output - C# example
320370

@@ -463,6 +513,68 @@ module.exports = function(context) {
463513

464514
In the [Java functions runtime library](/java/api/overview/azure/functions/runtime), use the `@QueueOutput` annotation on parameters whose value would be written to Queue storage. The parameter type should be `OutputBinding<T>`, where T is any native Java type of a POJO.
465515

516+
### Output - Python example
517+
518+
The following example demonstrates how to output single and multiple values to storage queues. The configuration needed for *function.json* is the same either way.
519+
520+
A Storage queue binding is defined in *function.json* where *type* is set to `queue`.
521+
522+
```json
523+
{
524+
"scriptFile": "__init__.py",
525+
"bindings": [
526+
{
527+
"authLevel": "function",
528+
"type": "httpTrigger",
529+
"direction": "in",
530+
"name": "req",
531+
"methods": [
532+
"get",
533+
"post"
534+
]
535+
},
536+
{
537+
"type": "http",
538+
"direction": "out",
539+
"name": "$return"
540+
},
541+
{
542+
"type": "queue",
543+
"direction": "out",
544+
"name": "msg",
545+
"queueName": "outqueue",
546+
"connection": "AzureStorageQueuesConnectionString"
547+
}
548+
]
549+
}
550+
```
551+
552+
To set a individual message on the queue, you pass a single value to the `set` method.
553+
554+
```python
555+
import azure.functions as func
556+
557+
def main(req: func.HttpRequest, msg: func.Out[str]) -> func.HttpResponse:
558+
559+
input_msg = req.params.get('message')
560+
561+
msg.set(input_msg)
562+
563+
return 'OK'
564+
```
565+
566+
To create multiple messages on the queue, declare a parameter as the appropriate list type and pass an array of values (that match the list type) to the `set` method.
567+
568+
```python
569+
import azure.functions as func
570+
import typing
571+
572+
def main(req: func.HttpRequest, msg: func.Out[typing.List[str]]) -> func.HttpResponse:
573+
574+
msg.set(['one', 'two'])
575+
576+
return 'OK'
577+
```
466578

467579
## Output - attributes
468580

0 commit comments

Comments
 (0)