Skip to content

Instantly share code, notes, and snippets.

@robertmclaws
Created September 27, 2012 00:56
Show Gist options
  • Save robertmclaws/3791549 to your computer and use it in GitHub Desktop.
Save robertmclaws/3791549 to your computer and use it in GitHub Desktop.
Simple code to convert SendGrid's ridiculous Event API into usable C# objects.
public ActionResult Index()
{
var json = new StreamReader(Request.InputStream).ReadToEnd();
//RWM: The next line turns the faux-array into an actual one.
json = string.Format("[{0}]", json.Replace("}" + Environment.NewLine + "{", "},{"));
var models = JsonConvert.DeserializeObject<List<SendGridEventModel>>(json);
models.ForEach(c => ProcessEvent(c));
return new HttpStatusCodeResult(HttpStatusCode.OK);
}
public class SendGridEventModel
{
/// <summary>
/// Applies to all message types.
/// </summary>
public string Category { get; set; }
/// <summary>
/// Applies to all message types.
/// </summary>
public string Event { get; set; }
/// <summary>
/// Applies to all message types.
/// </summary>
public string Email { get; set; }
/// <summary>
/// Applies to Deferred and Delivered events.
/// </summary>
public string Response { get; set; }
/// <summary>
/// Applies to the Deferred event.
/// </summary>
public int Attempt { get; set; }
/// <summary>
/// Applies to the Bounce and Drop events
/// </summary>
public string Reason { get; set; }
/// <summary>
/// Applies to the Click event.
/// </summary>
public string Url { get; set; }
/// <summary>
/// 3 digit status code. Applies to the Bounce event.
/// </summary>
public int Status { get; set; }
/// <summary>
/// Applies to the Bounce event.
/// </summary>
public string Type { get; set; }
/// <summary>
///
/// </summary>
public Int64 TimeStamp { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment