Skip to content

Instantly share code, notes, and snippets.

@dbarkol
Created August 14, 2018 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbarkol/611c8e5a1f3437e8d02183814c0866e3 to your computer and use it in GitHub Desktop.
Save dbarkol/611c8e5a1f3437e8d02183814c0866e3 to your computer and use it in GitHub Desktop.
// Parse the request body
var requestBody = context.Request.Body.As<string>();
JObject json = JObject.Parse(requestBody);
// Add the customer ID, order details and
// request ID of the API call to the event
// data property.
var data = json["order"];
data["customerId"] = json["customerId"];
data["requestId"] = context.RequestId;
// Set the event type based off of the quantity ordered
var quantity = (int) json["order"]["quantity"];
var eventType = quantity > 50 ? "Contoso.Orders.Large" : "Contoso.Orders.Normal";
// Events are sent in an array
var events = new JArray();
// Initialize the event and add it to the array
var newOrderEvent = new JObject();
newOrderEvent.Add("Data", data);
newOrderEvent.Add("Subject", "contoso/newOrder");
newOrderEvent.Add("EventType", eventType);
newOrderEvent.Add("Id", Guid.NewGuid().ToString());
newOrderEvent.Add("EventTime", DateTime.UtcNow.ToString());
events.Add(newOrderEvent);
// Format to a string and ready to go!
return events.ToString(Newtonsoft.Json.Formatting.None);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment