Skip to content

Commit c2c9d55

Browse files
Updates
1 parent ecfa0d1 commit c2c9d55

File tree

1 file changed

+34
-48
lines changed

1 file changed

+34
-48
lines changed

articles/azure-functions/functions-bindings-event-grid.md

+34-48
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,22 @@ Here's the binding data in the *function.json* file:
195195
Here's the Python code:
196196

197197
```python
198+
import json
198199
import logging
199-
import azure.functions as func
200200

201+
import azure.functions as func
201202

202203
def main(event: func.EventGridEvent):
203-
logging.info("Python Event Grid function processed a request.")
204-
logging.info(" Subject: %s", event.subject)
205-
logging.info(" Time: %s", event.event_time)
206-
logging.info(" Data: %s", event.get_json())
204+
205+
result = json.dumps({
206+
'id': event.id,
207+
'data': event.get_json(),
208+
'topic': event.topic,
209+
'subject': event.subject,
210+
'event_type': event.event_type,
211+
})
212+
213+
logging.info('Python EventGrid trigger processed an event: %s', result)
207214
```
208215

209216
# [Java](#tab/java)
@@ -305,19 +312,19 @@ For a complete example, see C# example.
305312

306313
# [C# Script](#tab/csharp-script)
307314

308-
**TODO**
315+
Attributes are not supported by C# Script.
309316

310317
# [JavaScript](#tab/javascript)
311318

312-
**TODO**
319+
Attributes are not supported by JavaScript.
313320

314321
# [Python](#tab/python)
315322

316-
**TODO**
323+
Attributes are not supported by Python.
317324

318325
# [Java](#tab/java)
319326

320-
**TODO**
327+
The [EventGridTrigger](https://github.com/Azure/azure-functions-java-library/blob/master/src/main/java/com/microsoft/azure/functions/annotation/EventGridTrigger.java) annotation allows you to declaratively configure an Event Grid binding by providing configuration values. See the [example](#example) and [configuration](#configuration) sections for more detail.
321328

322329
---
323330

@@ -345,23 +352,33 @@ In Azure Functions 2.x and higher, you also have the option to use the following
345352
* `Microsoft.Azure.EventGrid.Models.EventGridEvent`- Defines properties for the fields common to all event types.
346353

347354
> [!NOTE]
348-
> In Functions v1 if you try to bind to `Microsoft.Azure.WebJobs.Extensions.EventGrid.EventGridEvent`, the compiler will display a "deprecated" message and advise you to use `Microsoft.Azure.EventGrid.Models.EventGridEvent` instead. To use the newer type, reference the [Microsoft.Azure.EventGrid](https://www.nuget.org/packages/Microsoft.Azure.EventGrid) NuGet package and fully qualify the `EventGridEvent` type name by prefixing it with `Microsoft.Azure.EventGrid.Models`. For information about how to reference NuGet packages in a C# script function, see [Using NuGet packages](functions-reference-csharp.md#using-nuget-packages)
355+
> In Functions v1 if you try to bind to `Microsoft.Azure.WebJobs.Extensions.EventGrid.EventGridEvent`, the compiler will display a "deprecated" message and advise you to use `Microsoft.Azure.EventGrid.Models.EventGridEvent` instead. To use the newer type, reference the [Microsoft.Azure.EventGrid](https://www.nuget.org/packages/Microsoft.Azure.EventGrid) NuGet package and fully qualify the `EventGridEvent` type name by prefixing it with `Microsoft.Azure.EventGrid.Models`.
349356
350357
# [C# Script](#tab/csharp-script)
351358

352-
**TODO**
359+
In Azure Functions 1.x, you can use the following parameter types for the Event Grid trigger:
360+
361+
* `JObject`
362+
* `string`
363+
364+
In Azure Functions 2.x and higher, you also have the option to use the following parameter type for the Event Grid trigger:
365+
366+
* `Microsoft.Azure.EventGrid.Models.EventGridEvent`- Defines properties for the fields common to all event types.
367+
368+
> [!NOTE]
369+
> In Functions v1 if you try to bind to `Microsoft.Azure.WebJobs.Extensions.EventGrid.EventGridEvent`, the compiler will display a "deprecated" message and advise you to use `Microsoft.Azure.EventGrid.Models.EventGridEvent` instead. To use the newer type, reference the [Microsoft.Azure.EventGrid](https://www.nuget.org/packages/Microsoft.Azure.EventGrid) NuGet package and fully qualify the `EventGridEvent` type name by prefixing it with `Microsoft.Azure.EventGrid.Models`. For information about how to reference NuGet packages in a C# script function, see [Using NuGet packages](functions-reference-csharp.md#using-nuget-packages)
353370
354371
# [JavaScript](#tab/javascript)
355372

356-
The parameter named by the *function.json* `name` property has a reference to the event object.
373+
The Event Grid instance is available via the parameter configured in the *function.json* file's `name` property.
357374

358375
# [Python](#tab/python)
359376

360-
**TODO**
377+
The Event Grid instance is available via the parameter configured in the *function.json* file's `name` property, typed as `func.EventGridEvent`.
361378

362379
# [Java](#tab/java)
363380

364-
**TODO**
381+
The Event Grid event instance is available via the parameter associated to the `EventGridTrigger` attribute, typed as an `EventSchema`. See the [example](#example) for more detail.
365382

366383
---
367384

@@ -661,12 +678,10 @@ If you use an HTTP trigger, you have to write code for what the Event Grid trigg
661678
* Sends a validation response to a [subscription validation request](../event-grid/security-authentication.md#webhook-event-delivery).
662679
* Invokes the function once per element of the event array contained in the request body.
663680

664-
For information about the URL to use for invoking the function locally or when it runs in Azure, see the [HTTP trigger binding reference documentation](functions-bindings-http-webhook.md)
681+
For information about the URL to use for invoking the function locally or when it runs in Azure, see the [HTTP trigger binding reference documentation](functions-bindings-http-webhook.md).
665682

666683
### Event Grid schema
667684

668-
**TODO**
669-
670685
# [C#](#tab/csharp)
671686

672687
The following sample C# code for an HTTP trigger simulates Event Grid trigger behavior. Use this example for events delivered in the Event Grid schema.
@@ -707,36 +722,6 @@ public static async Task<HttpResponseMessage> Run(
707722
}
708723
```
709724

710-
The following sample JavaScript code for an HTTP trigger simulates Event Grid trigger behavior. Use this example for events delivered in the Event Grid schema.
711-
712-
```javascript
713-
module.exports = function (context, req) {
714-
context.log('JavaScript HTTP trigger function processed a request.');
715-
716-
var messages = req.body;
717-
// If the request is for subscription validation, send back the validation code.
718-
if (messages.length > 0 && messages[0].eventType == "Microsoft.EventGrid.SubscriptionValidationEvent") {
719-
context.log('Validate request received');
720-
var code = messages[0].data.validationCode;
721-
context.res = { status: 200, body: { "ValidationResponse": code } };
722-
}
723-
else {
724-
// The request is not for subscription validation, so it's for one or more events.
725-
// Event Grid schema delivers events in an array.
726-
for (var i = 0; i < messages.length; i++) {
727-
// Handle one event.
728-
var message = messages[i];
729-
context.log('Subject: ' + message.subject);
730-
context.log('Time: ' + message.eventTime);
731-
context.log('Data: ' + JSON.stringify(message.data));
732-
}
733-
}
734-
context.done();
735-
};
736-
```
737-
738-
Your event-handling code goes inside the loop through the `messages` array.
739-
740725
### CloudEvents schema
741726

742727
The following sample C# code for an HTTP trigger simulates Event Grid trigger behavior. Use this example for events delivered in the CloudEvents schema.
@@ -776,13 +761,14 @@ public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLeve
776761
return req.CreateResponse(HttpStatusCode.OK);
777762
}
778763
```
764+
779765
# [C# Script](#tab/csharp-script)
780766

781767
**TODO**
782768

783769
# [JavaScript](#tab/javascript)
784770

785-
The following sample JavaScript code for an HTTP trigger simulates Event Grid trigger behavior. Use this example for events delivered in the CloudEvents schema.
771+
The following sample simulates the Event Grid trigger behavior. Use this example for events delivered in the CloudEvents schema.
786772

787773
```javascript
788774
module.exports = function (context, req) {

0 commit comments

Comments
 (0)