Skip to content

Commit 7926e48

Browse files
authored
[@azure/eventgrid] Fixing the Readme issue (Azure#27489)
### Packages impacted by this PR @azure/eventgrid ### Issues associated with this PR Azure#26367 ### Describe the problem that is addressed by this PR The sample provided in the Readme is both complex and not working. I have updated the sample with a simple scenario which highlights the API that is explained. I have also verified that this new sample works fine. ### What are the possible designs available to address the problem? If there are more than one possible design, why was the one in this PR chosen? No special consideration for design. ### Are there test cases added in this PR? _(If not, why?)_ No. This is just updating the Readme file. Also, the sample is tested. ### Provide a list of related PRs _(if any)_ NA ### Command used to generate this PR:**_(Applicable only to SDK release request PRs)_ NA ### Checklists - [X] Added impacted package name to the issue description - [ ] Does this PR needs any fixes in the SDK Generator?** _(If so, create an Issue in the [Autorest/typescript](https://github.com/Azure/autorest.typescript) repository and link it here)_ - [ ] Added a changelog (if necessary) @xirzec Please review and approve the PR
1 parent 19d46b7 commit 7926e48

File tree

1 file changed

+25
-32
lines changed

1 file changed

+25
-32
lines changed

sdk/eventgrid/eventgrid/README.md

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -252,45 +252,38 @@ await client.send([
252252

253253
### Deserializing an Event
254254

255-
`EventGridDeserializer` can be used to deserialize events delivered by Event Grid. When deserializing an event, you need to know the schema used to deliver the event. In this example we have events being delivered to an Azure Service Bus Queue in the Cloud Events schema. Using the Service Bus SDK we can receive these events from the Service Bus Queue and then deserialize them using `EventGridDeserializer` and use `isSystemEvent` to detect what type of events they are.
255+
`EventGridDeserializer` can be used to deserialize events delivered by Event Grid. In this example we have a cloud event that is deserialized using `EventGridDeserializer` and use `isSystemEvent` to detect what type of events they are.
256256

257257
```js
258-
const { ServiceBusClient } = require("@azure/service-bus");
259-
const { DefaultAzureCredential } = require("@azure/identity");
260258
const { EventGridDeserializer, isSystemEvent } = require("@azure/eventgrid");
261259

262-
const client = new ServiceBusClient("<service bus hostname>", new DefaultAzureCredential());
263-
264-
const receiver = client.createReceiver("<queue name>", "peekLock");
265-
266-
const consumer = new EventGridDeserializer();
267-
268-
async function processMessage(message) {
269-
// When delivering to a Service Bus Queue or Topic, EventGrid delivers a single event per message.
270-
// so we just pluck the first one.
271-
const event = (await consumer.deserializeCloudEvents(message.body))[0];
272-
273-
if (isSystemEvent("Microsoft.ContainerRegistry.ImagePushed", event)) {
274-
console.log(
275-
`${event.time}: Container Registry Image Pushed event for image ${event.data.target.repository}:${event.data.target.tag}`
276-
);
277-
} else if (isSystemEvent("Microsoft.ContainerRegistry.ImageDeleted", event)) {
278-
console.log(
279-
`${event.time}: Container Registry Image Deleted event for repository ${event.data.target.repository}`
280-
);
260+
async function main() {
261+
const deserializer = new EventGridDeserializer();
262+
const message = {
263+
id: "5bc888aa-c2f4-11ea-b3de-0242ac130004",
264+
source:
265+
"/subscriptions/<subscriptionid>/resourceGroups/dummy-rg/providers/Microsoft.EventGrid/topics/dummy-topic",
266+
specversion: "1.0",
267+
type: "Microsoft.ContainerRegistry.ImagePushed",
268+
subject: "Test Subject",
269+
time: "2020-07-10T21:27:12.925Z",
270+
data: {
271+
hello: "world",
272+
},
273+
};
274+
const deserializedMessage = await deserializer.deserializeCloudEvents(message);
275+
console.log(deserializedMessage);
276+
277+
if (
278+
deserializedMessage != null &&
279+
deserializedMessage.length !== 0 &&
280+
isSystemEvent("Microsoft.ContainerRegistry.ImagePushed", deserializedMessage[0])
281+
) {
282+
console.log("This is a Microsoft.ContainerRegistry.ImagePushed event");
281283
}
282-
283-
await message.complete();
284284
}
285285

286-
console.log("starting receiver");
287-
288-
receiver.subscribe({
289-
processError: async (err) => {
290-
console.error(err);
291-
},
292-
processMessage,
293-
});
286+
main();
294287
```
295288

296289
## Troubleshooting

0 commit comments

Comments
 (0)