Azure

Validating an HTTP Endpoint to Obtain Occasions from Azure Occasion Grid

Introduction

This text explains learn how to confirm an HTTP endpoint earlier than receiving and deserializing occasions from an occasion subscription.

Stipulations

To validate an HTTP endpoint to obtain occasions from Azure Occasion Grid, you may certainly have to create an Azure Perform App with an HTTP-triggered perform.

Endpoint Validation

You need to maintain Microsoft.EventGrid.SubscriptionValidationEvent associated to subscription validation. Occasion Grid sends a validation occasion to the endpoint with a validation code within the knowledge payload every time an occasion is subscribed to. To confirm that the endpoint is justified and yours, it should amplify this again within the physique of the response. To deserialize a Binary Knowledge occasion containing a number of occasions into an array of EventGridEvent, use the ParseMany() methodology. You might use the Parse methodology rather than deserializing a single occasion when you had been conscious prematurely.

Use the next code to programmatically amplify the validation code.

/// <abstract>
/// EventIntake Perform.
/// </abstract>
/// <param title="req">The Req.</param>
/// <param title="log">The Log.</param>
/// <returns>Returns Response Code.</returns>
[FunctionName("EventGridFunction")]
public async Job<IActionResult> EventIntake(
	[HttpTrigger(AuthorizationLevel.User, "post", Route = null)]
HttpRequest req, ILogger log)
{
	attempt
	{
		BinaryData occasions = await BinaryData.FromStreamAsync(req.Physique);
		log.LogInformation($"Obtained occasions: {occasions}");
		EventGridEvent[] eventGridEvents = EventGridEvent.ParseMany(occasions);
		var validationEvent = eventGridEvents.FirstOrDefault(x => x.EventType == "Microsoft.EventGrid.SubscriptionValidationEvent");
		if (validationEvent != null)
		{
			var validationData = validationEvent.Knowledge.ToObjectFromJson<SubscriptionValidationEventData>();
			var validationResponse = new
			{
				ValidationResponse = validationData.ValidationCode
			};

			log.LogInformation("Obtained Validate Subscription occasion. Echo again the validation code {code}", validationResponse.ValidationResponse);
			return new OkObjectResult(validationResponse);
		}

		// Your Logic

		return new OkObjectResult(string.Empty);
	}
	catch (Exception e)
	{
		log.LogError(e, "Unable to cross occasion grid message");
		return new BadRequestObjectResult(e);
	}
}

Take a look at Validation Response

Copy and paste the pattern occasion into the perform’s check subject to check the validation response perform.

[{
  "id": "21915976-38b1-449d-8edf-a406ee6d23e5",
  "topic": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
  "subject": "",
  "data": {
    "validationCode": "6179467e-fefb-43c3-b92f-67370daedf99"
  },
  "eventType": "Microsoft.EventGrid.SubscriptionValidationEvent",
  "eventTime": "2018-01-25T22:12:19.4556811Z",
  "metadataVersion": "1",
  "dataVersion": "1"
}]

Conclusion

By following the rules offered on this article, you may successfully confirm HTTP endpoints earlier than receiving and deserializing occasions from occasion subscriptions, enhancing the safety and reliability of your event-driven structure in Azure Occasion Grid.

Know extra about our firm at Skrots. Know extra about our companies at Skrots Companies, Additionally checkout all different blogs at Weblog at Skrots

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button