Skip to content

Commit 2b130cf

Browse files
committed
Bring code back inline
1 parent d7fdff8 commit 2b130cf

File tree

4 files changed

+342
-495
lines changed

4 files changed

+342
-495
lines changed

articles/azure-functions/functions-add-output-binding-storage-queue-python.md

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,64 @@ Although a function can have only one trigger, it can have multiple input and ou
3434
3535
From the previous quickstart, your *function.json* file in the *HttpExample* folder contains two bindings in the `bindings` collection:
3636
37-
:::code language="json" source="snippets/http-trigger-basics.md" range="90-109":::
37+
```json
38+
{
39+
"scriptFile": "__init__.py",
40+
"bindings": [
41+
{
42+
"authLevel": "function",
43+
"type": "httpTrigger",
44+
"direction": "in",
45+
"name": "req",
46+
"methods": [
47+
"get",
48+
"post"
49+
]
50+
},
51+
{
52+
"type": "http",
53+
"direction": "out",
54+
"name": "$return"
55+
}
56+
]
57+
}
58+
```
3859

3960
Each binding has at least a type, a direction, and a name. In the example above, the first binding is of type `httpTrigger` with the direction `in`. For the `in` direction, `name` specifies the name of an input parameter that's sent to the function when invoked by the trigger.
4061

4162
The second binding is of type `http` with the direction `out`, in which case the special `name` of `$return` indicates that this binding uses the function's return value rather than providing an input parameter.
4263

4364
To write to an Azure Storage queue from this function, add an `out` binding of type `queue` with the name `msg`, as shown in the code below:
4465

45-
:::code language="json" source="snippets/storage-binding-use-queue.md" range="178-204" highlight="18-25":::
66+
```json
67+
{
68+
"scriptFile": "__init__.py",
69+
"bindings": [
70+
{
71+
"authLevel": "function",
72+
"type": "httpTrigger",
73+
"direction": "in",
74+
"name": "req",
75+
"methods": [
76+
"get",
77+
"post"
78+
]
79+
},
80+
{
81+
"type": "http",
82+
"direction": "out",
83+
"name": "$return"
84+
},
85+
{
86+
"type": "queue",
87+
"direction": "out",
88+
"name": "msg",
89+
"queueName": "outqueue",
90+
"connection": "AzureWebJobsStorage"
91+
}
92+
]
93+
}
94+
```
4695

4796
In this case, `msg` is given to the function as an output argument. For a `queue` type, you must also specify the name of the queue in `queueName` and provide the *name* of the Azure Storage connection (from *local.settings.json*) in `connection`.
4897

@@ -54,7 +103,32 @@ With the queue binding specified in *function.json*, you can now update your fun
54103

55104
Update *HttpExample\\\_\_init\_\_.py* to match the following code, adding the `msg` parameter to the function definition and `msg.set(name)` under the `if name:` statement.
56105

57-
:::code language="python" source="snippets/storage-binding-use-queue.md" range="210-233" highlight="6,18":::
106+
```python
107+
import logging
108+
109+
import azure.functions as func
110+
111+
112+
def main(req: func.HttpRequest, msg: func.Out[func.QueueMessage]) -> str:
113+
114+
name = req.params.get('name')
115+
if not name:
116+
try:
117+
req_body = req.get_json()
118+
except ValueError:
119+
pass
120+
else:
121+
name = req_body.get('name')
122+
123+
if name:
124+
msg.set(name)
125+
return func.HttpResponse(f"Hello {name}!")
126+
else:
127+
return func.HttpResponse(
128+
"Please pass a name on the query string or in the request body",
129+
status_code=400
130+
)
131+
```
58132

59133
The `msg` parameter is an instance of the [`azure.functions.InputStream class`](/python/api/azure-functions/azure.functions.httprequest). Its `set` method writes a string message to the queue, in this case the name passed to the function in the URL query string.
60134

0 commit comments

Comments
 (0)