├── Sendgrid.Webhooks ├── Events │ ├── OpenEvent.cs │ ├── DeliveryEvent.cs │ ├── DroppedEvent.cs │ ├── GroupResubscribeEvent.cs │ ├── GroupUnsubscribeEvent.cs │ ├── SpamReportEvent.cs │ ├── UnsubscribeEvent.cs │ ├── UrlOffset.cs │ ├── ClickEvent.cs │ ├── DeferredEvent.cs │ ├── ProcessedEvent.cs │ ├── BounceEvent.cs │ ├── DeliveryEventBase.cs │ ├── EngagementEventBase.cs │ ├── WebhookEventBase.cs │ └── WebhookEventType.cs ├── Converters │ ├── WebhookCategoryConverter.cs │ ├── BooleanConverter.cs │ ├── EpochToDateTimeConverter.cs │ ├── GenericListCreationJsonConverter.cs │ └── WebhookJsonConverter.cs ├── Service │ └── WebhookParser.cs └── Sendgrid.Webhooks.csproj ├── .travis.yml ├── Sendgrid.Webhooks.Tests ├── Events │ ├── spamreport.json │ ├── unsubscribe.json │ ├── processed.json │ ├── bounce-null.json │ ├── delivered.json │ ├── drop.json │ ├── deferred.json │ ├── open.json │ ├── bounce.json │ ├── group_resubscribe.json │ ├── group_unsubscribe.json │ ├── bounce-with-ip.json │ ├── click.json │ ├── processed-with-sendat.json │ ├── delivered-with-tlsanderror.json │ └── click-with-urloffset.json ├── BooleanConverterTests.cs ├── EpochConverterTests.cs ├── Sendgrid.Webhooks.Tests.csproj ├── JsonEventBuilder.cs └── WebhookParserTests.cs ├── Sendgrid.Webhooks.sln ├── README.md ├── .gitignore └── LICENSE /Sendgrid.Webhooks/Events/OpenEvent.cs: -------------------------------------------------------------------------------- 1 | namespace Sendgrid.Webhooks.Events 2 | { 3 | public class OpenEvent : EngagementEventBase 4 | { 5 | 6 | } 7 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks/Events/DeliveryEvent.cs: -------------------------------------------------------------------------------- 1 | namespace Sendgrid.Webhooks.Events 2 | { 3 | public class DeliveryEvent : DeliveryEventBase 4 | { 5 | 6 | } 7 | } -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: csharp 2 | dotnet: 2.0.0 3 | env: 4 | - CONFIGURATION=Release 5 | install: 6 | - dotnet restore 7 | script: 8 | - dotnet build 9 | - dotnet test Sendgrid.Webhooks.Tests/Sendgrid.Webhooks.Tests.csproj 10 | - dotnet pack -------------------------------------------------------------------------------- /Sendgrid.Webhooks/Events/DroppedEvent.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace Sendgrid.Webhooks.Events 4 | { 5 | public class DroppedEvent : DeliveryEventBase 6 | { 7 | [JsonProperty("reason")] 8 | public string Reason { get; set; } 9 | } 10 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks/Events/GroupResubscribeEvent.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace Sendgrid.Webhooks.Events 4 | { 5 | public class GroupResubscribeEvent : EngagementEventBase 6 | { 7 | [JsonProperty("asm_group_id")] 8 | public int GroupId { get; set; } 9 | } 10 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks/Events/GroupUnsubscribeEvent.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace Sendgrid.Webhooks.Events 4 | { 5 | public class GroupUnsubscribeEvent : EngagementEventBase 6 | { 7 | [JsonProperty("asm_group_id")] 8 | public int GroupId { get; set; } 9 | } 10 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks/Events/SpamReportEvent.cs: -------------------------------------------------------------------------------- 1 | namespace Sendgrid.Webhooks.Events 2 | { 3 | /// 4 | /// BMP 2/19/2016 5 | /// Switched Base Class to correct base 6 | /// 7 | public class SpamReportEvent : EngagementEventBase 8 | { 9 | 10 | } 11 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks/Converters/WebhookCategoryConverter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Newtonsoft.Json; 3 | using Sendgrid.Webhooks.Converters; 4 | 5 | namespace Sendgrid.Webhooks.Service 6 | { 7 | public class WebhookCategoryConverter : GenericListCreationJsonConverter 8 | { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Sendgrid.Webhooks/Events/UnsubscribeEvent.cs: -------------------------------------------------------------------------------- 1 | namespace Sendgrid.Webhooks.Events 2 | { 3 | /// 4 | /// BMP 2/19/2016 5 | /// Switched Base Class to correct base 6 | /// 7 | public class UnsubscribeEvent : EngagementEventBase 8 | { 9 | 10 | } 11 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks.Tests/Events/spamreport.json: -------------------------------------------------------------------------------- 1 | { 2 | "sg_event_id":"sendgrid_internal_event_id", 3 | "sg_message_id":"sendgrid_internal_message_id", 4 | "email":"email@example.com", 5 | "timestamp":1249948800, 6 | "unique_arg_key":"unique_arg_value", 7 | "category":["category1", "category2"], 8 | "event":"spamreport" 9 | } 10 | -------------------------------------------------------------------------------- /Sendgrid.Webhooks.Tests/Events/unsubscribe.json: -------------------------------------------------------------------------------- 1 | { 2 | "sg_event_id":"sendgrid_internal_event_id", 3 | "sg_message_id":"sendgrid_internal_message_id", 4 | "email":"email@example.com", 5 | "timestamp":1249948800, 6 | "unique_arg_key":"unique_arg_value", 7 | "category":["category1", "category2"], 8 | "event":"unsubscribe" 9 | } 10 | -------------------------------------------------------------------------------- /Sendgrid.Webhooks/Events/UrlOffset.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace Sendgrid.Webhooks.Events 4 | { 5 | public class UrlOffset 6 | { 7 | [JsonProperty("index")] 8 | public int Index { get; set; } 9 | 10 | [JsonProperty("type")] 11 | public string Type { get; set; } 12 | } 13 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks/Events/ClickEvent.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace Sendgrid.Webhooks.Events 4 | { 5 | public class ClickEvent : EngagementEventBase 6 | { 7 | [JsonProperty("url")] 8 | public string Url { get; set; } 9 | 10 | [JsonProperty("url_offset")] 11 | public UrlOffset UrlOffset { get; set; } 12 | } 13 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks/Events/DeferredEvent.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace Sendgrid.Webhooks.Events 4 | { 5 | public class DeferredEvent : DeliveryEventBase 6 | { 7 | [JsonProperty("response")] 8 | public string Response { get; set; } 9 | 10 | [JsonProperty("attempt")] 11 | public string Attempts { get; set; } 12 | } 13 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks.Tests/Events/processed.json: -------------------------------------------------------------------------------- 1 | { 2 | "sg_event_id":"sendgrid_internal_event_id", 3 | "sg_message_id":"sendgrid_internal_message_id", 4 | "email":"email@example.com", 5 | "timestamp":1249948800, 6 | "smtp-id":"", 7 | "unique_arg_key":"unique_arg_value", 8 | "category":["category1","category2"], 9 | "event":"processed" 10 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks.Tests/Events/bounce-null.json: -------------------------------------------------------------------------------- 1 | { 2 | "email": "some-random-email@gsk.com", 3 | "timestamp": 1434967476, 4 | "status": "5.7.999", 5 | "reason": "550 5.7.999 The user is inactive and not accepting messages.", 6 | "sg_event_id":"sendgrid_internal_event_id", 7 | "sg_message_id":"sendgrid_internal_message_id", 8 | "type": "blocked", 9 | "event": "bounce" 10 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks/Events/ProcessedEvent.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Newtonsoft.Json; 3 | using Sendgrid.Webhooks.Converters; 4 | 5 | namespace Sendgrid.Webhooks.Events 6 | { 7 | public class ProcessedEvent : DeliveryEventBase 8 | { 9 | [JsonProperty("send_at"), JsonConverter(typeof(EpochToDateTimeConverter))] 10 | public DateTime SendAt { get; set; } 11 | } 12 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks.Tests/Events/delivered.json: -------------------------------------------------------------------------------- 1 | { 2 | "response":"250 OK", 3 | "sg_event_id":"sendgrid_internal_event_id", 4 | "sg_message_id":"sendgrid_internal_message_id", 5 | "event":"delivered", 6 | "email":"email@example.com", 7 | "timestamp":1249948800, 8 | "smtp-id":"", 9 | "unique_arg_key":"unique_arg_value", 10 | "category":["category1", "category2"] 11 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks.Tests/Events/drop.json: -------------------------------------------------------------------------------- 1 | { 2 | "sg_event_id":"sendgrid_internal_event_id", 3 | "sg_message_id":"sendgrid_internal_message_id", 4 | "email":"email@example.com", 5 | "timestamp":1249948800, 6 | "smtp-id":"", 7 | "unique_arg_key":"unique_arg_value", 8 | "category":["category1", "category2"], 9 | "reason":"Bounced Address", 10 | "event":"dropped" 11 | } 12 | -------------------------------------------------------------------------------- /Sendgrid.Webhooks.Tests/Events/deferred.json: -------------------------------------------------------------------------------- 1 | { 2 | "response":"400 Try again", 3 | "sg_event_id":"sendgrid_internal_event_id", 4 | "sg_message_id":"sendgrid_internal_message_id", 5 | "event":"deferred", 6 | "email":"email@example.com", 7 | "timestamp":1249948800, 8 | "smtp-id":"", 9 | "unique_arg_key":"unique_arg_value", 10 | "category":["category1", "category2"], 11 | "attempt":"10" 12 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks.Tests/Events/open.json: -------------------------------------------------------------------------------- 1 | { 2 | "email":"email@example.com", 3 | "timestamp":1249948800, 4 | "ip":"255.255.255.255", 5 | "sg_event_id":"sendgrid_internal_event_id", 6 | "sg_message_id":"sendgrid_internal_message_id", 7 | "useragent":"Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko Firefox/11.0 (via ggpht.com GoogleImageProxy)", 8 | "event":"open", 9 | "unique_arg_key":"unique_arg_value", 10 | "category":["category1", "category2"] 11 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks/Events/BounceEvent.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace Sendgrid.Webhooks.Events 4 | { 5 | public class BounceEvent : DeliveryEventBase 6 | { 7 | [JsonProperty("reason")] 8 | public string Reason { get; set; } 9 | 10 | [JsonProperty("type")] 11 | public string BounceType { get; set; } 12 | 13 | [JsonProperty("status")] 14 | public string BounceStatus { get; set; } 15 | } 16 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks.Tests/Events/bounce.json: -------------------------------------------------------------------------------- 1 | { 2 | "status":"5.0.0", 3 | "sg_event_id":"sendgrid_internal_event_id", 4 | "sg_message_id":"sendgrid_internal_message_id", 5 | "event":"bounce", 6 | "email":"email@example.com", 7 | "timestamp":1249948800, 8 | "smtp-id":"", 9 | "unique_arg_key":"unique_arg_value", 10 | "category":["category1", "category2"], 11 | "reason":"500 No Such User", 12 | "type":"bounce" 13 | } 14 | -------------------------------------------------------------------------------- /Sendgrid.Webhooks.Tests/Events/group_resubscribe.json: -------------------------------------------------------------------------------- 1 | { 2 | "sg_event_id":"sendgrid_internal_event_id", 3 | "sg_message_id":"sendgrid_internal_message_id", 4 | "email":"email@example.com", 5 | "timestamp":1249948800, 6 | "unique_arg_key":"unique_arg_value", 7 | "category":["category1", "category2"], 8 | "event":"group_resubscribe", 9 | "asm_group_id":1, 10 | "useragent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36", 11 | "ip":"255.255.255.255" 12 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks.Tests/Events/group_unsubscribe.json: -------------------------------------------------------------------------------- 1 | { 2 | "sg_event_id":"sendgrid_internal_event_id", 3 | "sg_message_id":"sendgrid_internal_message_id", 4 | "email":"email@example.com", 5 | "timestamp":1249948800, 6 | "unique_arg_key":"unique_arg_value", 7 | "category":["category1", "category2"], 8 | "event":"group_unsubscribe", 9 | "asm_group_id":1, 10 | "useragent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36", 11 | "ip":"255.255.255.255" 12 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks.Tests/Events/bounce-with-ip.json: -------------------------------------------------------------------------------- 1 | { 2 | "ip": "192.254.114.26", 3 | "status": "550", 4 | "sg_event_id":"sendgrid_internal_event_id", 5 | "sg_message_id":"sendgrid_internal_message_id", 6 | "reason": "550 Requested action not taken: mailbox unavailable ", 7 | "tls": 1, 8 | "event": "bounce", 9 | "email": "xxxxxx@yyyy.com", 10 | "timestamp": 1465067926, 11 | "smtp-id": "", 12 | "type": "bounce", 13 | "category": "EmailActivation" 14 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks.Tests/Events/click.json: -------------------------------------------------------------------------------- 1 | { 2 | "sg_event_id":"sendgrid_internal_event_id", 3 | "sg_message_id":"sendgrid_internal_message_id", 4 | "ip":"255.255.255.255", 5 | "useragent":"Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D257 Safari/9537.53", 6 | "event":"click", 7 | "email":"email@example.com", 8 | "timestamp":1249948800, 9 | "url":"http://yourdomain.com/blog/news.html", 10 | "unique_arg_key":"unique_arg_value", 11 | "category":["category1", "category2"] 12 | } 13 | -------------------------------------------------------------------------------- /Sendgrid.Webhooks.Tests/Events/processed-with-sendat.json: -------------------------------------------------------------------------------- 1 | { 2 | "sg_event_id":"sendgrid_internal_event_id", 3 | "sg_message_id":"sendgrid_internal_message_id", 4 | "email":"email@example.com", 5 | "timestamp":1249948800, 6 | "smtp-id":"", 7 | "unique_arg_key":"unique_arg_value", 8 | "category":["category1", "category2"], 9 | "event":"processed", 10 | "newsletter": { 11 | "newsletter_user_list_id": "10557865", 12 | "newsletter_id": "1943530", 13 | "newsletter_send_id": "2308608" 14 | }, 15 | "asm_group_id": 1, 16 | "send_at":1249949000 17 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks.Tests/Events/delivered-with-tlsanderror.json: -------------------------------------------------------------------------------- 1 | { 2 | "response":"250 OK", 3 | "sg_event_id":"sendgrid_internal_event_id", 4 | "sg_message_id":"sendgrid_internal_message_id", 5 | "event":"delivered", 6 | "email":"email@example.com", 7 | "timestamp":1249948800, 8 | "smtp-id":"", 9 | "unique_arg_key":"unique_arg_value", 10 | "category":["category1", "category2"], 11 | "newsletter": { 12 | "newsletter_user_list_id": "10557865", 13 | "newsletter_id": "1943530", 14 | "newsletter_send_id": "2308608" 15 | }, 16 | "asm_group_id": 1, 17 | "ip" : "127.0.0.1", 18 | "tls" : "1", 19 | "cert_err" : "1" 20 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks/Events/DeliveryEventBase.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using Sendgrid.Webhooks.Converters; 3 | 4 | namespace Sendgrid.Webhooks.Events 5 | { 6 | public abstract class DeliveryEventBase : WebhookEventBase 7 | { 8 | [JsonProperty("smtp-id")] 9 | public string SmtpId { get; set; } 10 | 11 | [JsonProperty("ip")] 12 | public string Ip { get; set; } 13 | 14 | [JsonConverter(typeof(BooleanConverter))] 15 | [JsonProperty("tls")] 16 | public bool Tls { get; set; } 17 | 18 | [JsonConverter(typeof(BooleanConverter))] 19 | [JsonProperty("cert_err")] 20 | public bool CertificateError { get; set; } 21 | } 22 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks/Events/EngagementEventBase.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using Sendgrid.Webhooks.Converters; 3 | 4 | namespace Sendgrid.Webhooks.Events 5 | { 6 | public abstract class EngagementEventBase : WebhookEventBase 7 | { 8 | [JsonProperty("useragent")] 9 | public string UserAgent { get; set; } 10 | 11 | [JsonProperty("ip")] 12 | public string Ip { get; set; } 13 | 14 | [JsonConverter(typeof(BooleanConverter))] 15 | [JsonProperty("tls")] 16 | public bool Tls { get; set; } 17 | 18 | [JsonConverter(typeof(BooleanConverter))] 19 | [JsonProperty("cert_err")] 20 | public bool CertificateError { get; set; } 21 | } 22 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks/Converters/BooleanConverter.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using System; 3 | 4 | namespace Sendgrid.Webhooks.Converters 5 | { 6 | public class BooleanConverter : JsonConverter 7 | { 8 | public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 9 | { 10 | var booleanValue = (Boolean) value; 11 | writer.WriteValue(booleanValue ? 1 : 0); 12 | } 13 | 14 | public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 15 | { 16 | return !reader.Value.Equals("0"); 17 | } 18 | 19 | public override bool CanConvert(Type objectType) 20 | { 21 | return objectType == typeof(Boolean); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks.Tests/Events/click-with-urloffset.json: -------------------------------------------------------------------------------- 1 | { 2 | "sg_event_id":"sendgrid_internal_event_id", 3 | "sg_message_id":"sendgrid_internal_message_id", 4 | "ip":"255.255.255.255", 5 | "useragent":"Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D257 Safari/9537.53", 6 | "event":"click", 7 | "email":"email@example.com", 8 | "timestamp":1249948800, 9 | "url":"http://yourdomain.com/blog/news.html", 10 | "url_offset": { 11 | "index": 0, 12 | "type": "html" 13 | }, 14 | "unique_arg_key":"unique_arg_value", 15 | "category":["category1", "category2"], 16 | "newsletter": { 17 | "newsletter_user_list_id": "10557865", 18 | "newsletter_id": "1943530", 19 | "newsletter_send_id": "2308608" 20 | }, 21 | "asm_group_id": 1 22 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks/Service/WebhookParser.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Newtonsoft.Json; 4 | using Sendgrid.Webhooks.Converters; 5 | using Sendgrid.Webhooks.Events; 6 | 7 | namespace Sendgrid.Webhooks.Service 8 | { 9 | public class WebhookParser 10 | { 11 | private readonly JsonConverter[] _converters; 12 | 13 | public WebhookParser() 14 | { 15 | _converters = new JsonConverter[] { new WebhookJsonConverter() }; 16 | } 17 | 18 | public WebhookParser(JsonConverter[] converters) 19 | { 20 | if (converters == null) { 21 | throw new ArgumentNullException("converters"); 22 | } 23 | 24 | _converters = converters; 25 | } 26 | 27 | public IList ParseEvents(String json) 28 | { 29 | return JsonConvert.DeserializeObject>(json, _converters); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Sendgrid.Webhooks/Converters/EpochToDateTimeConverter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Newtonsoft.Json; 3 | 4 | namespace Sendgrid.Webhooks.Converters 5 | { 6 | public class EpochToDateTimeConverter : JsonConverter 7 | { 8 | private static readonly DateTime EpochDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); 9 | 10 | public override bool CanConvert(Type objectType) 11 | { 12 | return objectType == typeof(DateTime); 13 | } 14 | 15 | public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 16 | { 17 | if (value == null) 18 | return; 19 | 20 | var date = (DateTime) value; 21 | var diff = date - EpochDate; 22 | 23 | var secondsSinceEpoch = (int) diff.TotalSeconds; 24 | serializer.Serialize(writer, secondsSinceEpoch); 25 | } 26 | 27 | public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 28 | { 29 | var timestamp = Convert.ToDouble(reader.Value); 30 | return EpochDate.AddSeconds(timestamp); 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks/Sendgrid.Webhooks.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net40;netstandard1.0 5 | true 6 | true 7 | embedded 8 | /usr/lib/mono/4.5/ 9 | 10 | 11 | Sendgrid.Webhooks 12 | A library to parse event webhooks from Sendgrid. Contains Parser and a set of strongly typed DTOs. It supports all available webhook events, unique arguments and categories. 13 | 2.0.0 14 | Mira Javora,Andy McCready,Brian Petersen,m0sa 15 | https://github.com/mirajavora/sendgrid-webhooks 16 | https://github.com/mirajavora/sendgrid-webhooks.git 17 | git 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Sendgrid.Webhooks/Events/WebhookEventBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Newtonsoft.Json; 3 | using System.Collections.Generic; 4 | using Newtonsoft.Json.Converters; 5 | using Sendgrid.Webhooks.Converters; 6 | using Sendgrid.Webhooks.Service; 7 | 8 | namespace Sendgrid.Webhooks.Events 9 | { 10 | public abstract class WebhookEventBase 11 | { 12 | public WebhookEventBase() 13 | { 14 | UniqueParameters = new Dictionary(); 15 | } 16 | 17 | [JsonProperty("sg_message_id")] 18 | public string SgMessageId { get; set; } 19 | 20 | [JsonProperty("sg_event_id")] 21 | public string SgEventId { get; set; } 22 | 23 | [JsonProperty("event"), JsonConverter(typeof(StringEnumConverter))] 24 | public WebhookEventType EventType { get; set; } 25 | 26 | [JsonProperty("email")] 27 | public string Email { get; set; } 28 | 29 | [JsonProperty("category"), JsonConverter(typeof(WebhookCategoryConverter))] 30 | public IList Category { get; set; } 31 | 32 | [JsonProperty("timestamp"), JsonConverter(typeof(EpochToDateTimeConverter))] 33 | public DateTime Timestamp { get; set; } 34 | 35 | public IDictionary UniqueParameters { get; set; } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Sendgrid.Webhooks/Converters/GenericListCreationJsonConverter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Newtonsoft.Json; 4 | 5 | namespace Sendgrid.Webhooks.Converters 6 | { 7 | public abstract class GenericListCreationJsonConverter : JsonConverter 8 | { 9 | 10 | public override bool CanConvert(Type objectType) 11 | { 12 | return true; 13 | } 14 | 15 | public override bool CanRead 16 | { 17 | get { return true; } 18 | } 19 | 20 | public override bool CanWrite 21 | { 22 | get { return false; } 23 | } 24 | 25 | public override object ReadJson(JsonReader reader, Type objectType, object existingValue, 26 | JsonSerializer serializer) 27 | { 28 | if (reader.TokenType == JsonToken.StartArray) 29 | { 30 | return serializer.Deserialize>(reader); 31 | } 32 | else 33 | { 34 | T t = serializer.Deserialize(reader); 35 | return new List(new[] {t}); 36 | } 37 | } 38 | 39 | public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 40 | { 41 | throw new NotImplementedException(); 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks.Tests/BooleanConverterTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Text; 4 | using Newtonsoft.Json; 5 | using Newtonsoft.Json.Linq; 6 | using NUnit.Framework; 7 | using Sendgrid.Webhooks.Converters; 8 | 9 | namespace Sendgrid.Webhooks.Tests 10 | { 11 | [TestFixture] 12 | public class BooleanConverterTests 13 | { 14 | private BooleanConverter _converter; 15 | 16 | [SetUp] 17 | public void SetUp() 18 | { 19 | _converter = new BooleanConverter(); 20 | } 21 | 22 | [TestCase(typeof(bool), true)] 23 | [TestCase(typeof(string), false)] 24 | public void CanConvert_Boolean_Only(Type tested, bool expected) 25 | { 26 | var result = _converter.CanConvert(tested); 27 | Assert.AreEqual(expected, result); 28 | } 29 | 30 | [TestCase("0", false)] 31 | [TestCase("1", true)] 32 | public void ReadJson_String_ConvertsToBoolean(String value, bool expected) 33 | { 34 | var reader = new JTokenReader(new JValue(value)); 35 | reader.Read(); 36 | var result = _converter.ReadJson(reader, typeof (String), null, new JsonSerializer()); 37 | 38 | Assert.AreEqual(expected, result); 39 | } 40 | 41 | [TestCase("0", false)] 42 | [TestCase("1", true)] 43 | public void WriteJson_Bool_ConvertsToString(String expected, bool value) 44 | { 45 | var stringBuilder = new StringBuilder(); 46 | _converter.WriteJson(new JsonTextWriter(new StringWriter(stringBuilder)), value, new JsonSerializer()); 47 | 48 | Assert.AreEqual(expected, stringBuilder.ToString()); 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks/Events/WebhookEventType.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace Sendgrid.Webhooks.Events 4 | { 5 | public enum WebhookEventType 6 | { 7 | /// 8 | /// Message has been received and is ready to be delivered. 9 | /// 10 | Processed, 11 | /// 12 | /// You may see the following drop reasons: Invalid SMTPAPI header, Spam Content (if spam checker app enabled), Unsubscribed Address, Bounced Address, Spam Reporting Address, Invalid, Recipient List over Package Quota 13 | /// 14 | Dropped, 15 | /// 16 | /// Message has been successfully delivered to the receiving server. 17 | /// 18 | Delivered, 19 | /// 20 | /// Recipient’s email server temporarily rejected message. 21 | /// 22 | Deferred, 23 | /// 24 | /// Receiving server could not or would not accept message. 25 | /// 26 | Bounce, 27 | /// 28 | /// Recipient has opened the HTML message. 29 | /// 30 | Open, 31 | /// 32 | /// Recipient clicked on a link within the message. 33 | /// 34 | Click, 35 | /// 36 | /// Recipient marked message as spam. 37 | /// 38 | SpamReport, 39 | /// 40 | /// Recipient clicked on message’s subscription management link. 41 | /// 42 | Unsubscribe, 43 | /// 44 | /// Recipient unsubscribed from specific group, by either direct link or updating preferences. 45 | /// 46 | Group_Unsubscribe, 47 | Group_Resubscribe 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Sendgrid.Webhooks.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.26430.6 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sendgrid.Webhooks", "Sendgrid.Webhooks\Sendgrid.Webhooks.csproj", "{A01761F3-3B82-4097-B32A-AC3F79B739B1}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sendgrid.Webhooks.Tests", "Sendgrid.Webhooks.Tests\Sendgrid.Webhooks.Tests.csproj", "{D8F3B538-B707-4CF9-A78D-FF7D7E5F1C70}" 9 | EndProject 10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{4BA89CBE-4C1B-4C9C-86FD-943C99D37176}" 11 | ProjectSection(SolutionItems) = preProject 12 | EndProjectSection 13 | EndProject 14 | Global 15 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 16 | Debug|Any CPU = Debug|Any CPU 17 | Release|Any CPU = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 20 | {A01761F3-3B82-4097-B32A-AC3F79B739B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {A01761F3-3B82-4097-B32A-AC3F79B739B1}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {A01761F3-3B82-4097-B32A-AC3F79B739B1}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {A01761F3-3B82-4097-B32A-AC3F79B739B1}.Release|Any CPU.Build.0 = Release|Any CPU 24 | {D8F3B538-B707-4CF9-A78D-FF7D7E5F1C70}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 25 | {D8F3B538-B707-4CF9-A78D-FF7D7E5F1C70}.Debug|Any CPU.Build.0 = Debug|Any CPU 26 | {D8F3B538-B707-4CF9-A78D-FF7D7E5F1C70}.Release|Any CPU.ActiveCfg = Release|Any CPU 27 | {D8F3B538-B707-4CF9-A78D-FF7D7E5F1C70}.Release|Any CPU.Build.0 = Release|Any CPU 28 | EndGlobalSection 29 | GlobalSection(SolutionProperties) = preSolution 30 | HideSolutionNode = FALSE 31 | EndGlobalSection 32 | EndGlobal 33 | -------------------------------------------------------------------------------- /Sendgrid.Webhooks.Tests/EpochConverterTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Text; 4 | using Newtonsoft.Json; 5 | using Newtonsoft.Json.Linq; 6 | using NUnit.Framework; 7 | using Sendgrid.Webhooks.Converters; 8 | 9 | namespace Sendgrid.Webhooks.Tests 10 | { 11 | [TestFixture] 12 | public class EpochConverterTests 13 | { 14 | private EpochToDateTimeConverter _converter; 15 | 16 | [SetUp] 17 | public void SetUp() 18 | { 19 | _converter = new EpochToDateTimeConverter(); 20 | } 21 | 22 | [TestCase(typeof(DateTime), true)] 23 | [TestCase(typeof(string), false)] 24 | public void CanConvert_DateTime_Only(Type tested, bool expected) 25 | { 26 | var result = _converter.CanConvert(tested); 27 | Assert.AreEqual(expected, result); 28 | } 29 | 30 | [Test] 31 | public void ReadJson_Long_ConvertsToDate() 32 | { 33 | var reader = new JTokenReader(new JValue(123)); 34 | reader.Read(); 35 | var result = _converter.ReadJson(reader, typeof (long), null, new JsonSerializer()); 36 | 37 | Assert.AreEqual(new DateTime(1970, 1, 1, 0, 2, 3), result); 38 | } 39 | 40 | [Test] 41 | public void ReadJson_Double_ConvertsToDate() 42 | { 43 | var reader = new JTokenReader(new JValue((double)123.555)); 44 | reader.Read(); 45 | var result = _converter.ReadJson(reader, typeof(long), null, new JsonSerializer()); 46 | 47 | Assert.AreEqual(new DateTime(1970, 1, 1, 0, 2, 3, 555), result); 48 | } 49 | 50 | [Test] 51 | public void WriteJson_Date_ConvertsToEpoch() 52 | { 53 | var stringBuilder = new StringBuilder(); 54 | _converter.WriteJson(new JsonTextWriter(new StringWriter(stringBuilder)), new DateTime(1980, 1, 1), new JsonSerializer()); 55 | 56 | Assert.AreEqual("315532800", stringBuilder.ToString()); 57 | } 58 | } 59 | } -------------------------------------------------------------------------------- /Sendgrid.Webhooks.Tests/Sendgrid.Webhooks.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | false 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | Always 25 | 26 | 27 | Always 28 | 29 | 30 | Always 31 | 32 | 33 | Always 34 | 35 | 36 | Always 37 | 38 | 39 | Always 40 | 41 | 42 | Always 43 | 44 | 45 | Always 46 | 47 | 48 | Always 49 | 50 | 51 | Always 52 | 53 | 54 | Always 55 | 56 | 57 | Always 58 | 59 | 60 | Always 61 | 62 | 63 | Always 64 | 65 | 66 | Always 67 | 68 | 69 | Always 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /Sendgrid.Webhooks/Converters/WebhookJsonConverter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Collections.Generic; 4 | using Newtonsoft.Json; 5 | using Newtonsoft.Json.Linq; 6 | using Sendgrid.Webhooks.Events; 7 | 8 | namespace Sendgrid.Webhooks.Converters 9 | { 10 | public class WebhookJsonConverter : JsonConverter 11 | { 12 | //these will be filtered out from the UniqueParams 13 | private static readonly string[] KnownProperties = new string[] {"event", "email", "category", "timestamp", "ip", "useragent", "type", 14 | "reason", "status", "url", "url_offset", "send_at", "tls", "cert_err" }; 15 | 16 | private static readonly IDictionary TypeMapping = new Dictionary() 17 | { 18 | {"processed", typeof(ProcessedEvent)}, 19 | {"bounce", typeof(BounceEvent)}, 20 | {"click", typeof(ClickEvent)}, 21 | {"deferred", typeof(DeferredEvent)}, 22 | {"delivered", typeof(DeliveryEvent)}, 23 | {"dropped", typeof(DroppedEvent)}, 24 | {"open", typeof(OpenEvent)}, 25 | {"spamreport", typeof(SpamReportEvent)}, 26 | {"unsubscribe", typeof(UnsubscribeEvent)}, 27 | {"group_resubscribe", typeof(GroupResubscribeEvent)}, 28 | {"group_unsubscribe", typeof(GroupUnsubscribeEvent)} 29 | }; 30 | 31 | public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 32 | { 33 | throw new NotImplementedException("The webhook JSON converter does not support write operations."); 34 | } 35 | 36 | public override object ReadJson(JsonReader reader, Type objectType, object existingValue, 37 | JsonSerializer serializer) 38 | { 39 | var jsonObject = JObject.Load(reader); 40 | 41 | //serialise based on the event type 42 | JToken eventName = null; 43 | jsonObject.TryGetValue("event", StringComparison.CurrentCultureIgnoreCase, out eventName); 44 | 45 | if (!TypeMapping.ContainsKey(eventName.ToString())) 46 | throw new NotImplementedException(string.Format("Event {0} is not implemented yet.", eventName)); 47 | 48 | Type type = TypeMapping[eventName.ToString()]; 49 | WebhookEventBase webhookItem = (WebhookEventBase)jsonObject.ToObject(type, serializer); 50 | 51 | AddUnmappedPropertiesAsUnique(webhookItem, jsonObject); 52 | 53 | return webhookItem; 54 | } 55 | 56 | public override bool CanConvert(Type objectType) 57 | { 58 | return objectType == typeof (WebhookEventBase); 59 | } 60 | 61 | private void AddUnmappedPropertiesAsUnique(WebhookEventBase webhookEvent, JObject jObject) 62 | { 63 | var dict = jObject.ToObject>(); 64 | 65 | foreach (var o in dict) 66 | { 67 | if (KnownProperties.Contains(o.Key)) 68 | continue; 69 | 70 | webhookEvent.UniqueParameters.Add(o.Key, o.Value == null ? null : o.Value.ToString()); 71 | } 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Travis Build Status](https://api.travis-ci.org/mirajavora/sendgrid-webhooks.svg?branch=master)](https://travis-ci.org/mirajavora/sendgrid-webhooks) 2 | 3 | # Sendgrid Webhooks 4 | A library to parse event webhooks from Sendgrid. Contains Parser and a set of strongly typed DTOs. It supports all available webhook events, unique arguments and categories. 5 | 6 | # Download via NuGet 7 | Install Sendgrid.Webhooks via NuGet package manager (nuget.org) 8 | 9 | Install-Package Sendgrid.Webhooks 10 | 11 | # Usage 12 | 13 | Declare `WebhookParser` and call `ParseEvents`. This takes in string as JSON received from the HTTP POST callback. 14 | ```csharp 15 | var parser = new WebhookParser(); 16 | var events = parser.ParseEvents(json); 17 | ``` 18 | The parser returns a polymorphic `IList` of `WebhookEventBase`, where each item is a strongly typed Webhook Event. 19 | 20 | *Example Properties* 21 | ```csharp 22 | var webhookEvent = events[0]; 23 | 24 | // Shared base properties 25 | webhookEvent.EventType; // Enum - type of the event as enum 26 | webhookEvent.Categories; // IList - list of categories assigned ot the event 27 | webhookEvent.TimeStamp; // DateTime - datetime of the event converted from Unix time 28 | webhookEvent.UniqueParameters; // IDictionary - map of key-value unique parameters 29 | 30 | //All delivery events (deliver, bounce, deferred) also contain 31 | var deliveryEvent = webhookEvent as DeliveryEvent; // Cast to the parent based on EventType 32 | deliveryEvent.Ip; //string ip address used to send the event 33 | deliveryEvent.Tls; //bool whether or not TLS was used when sending the email 34 | deliveryEvent.CertificateError; //bool whether there was a certificate error on the receiving side 35 | deliveryEvent.SmtpId; //string id attached to the message by the originating system 36 | 37 | // Event-specific properties for example 38 | var clickEvent = webhookEvent as ClickEvent; // Cast to the parent based on EventType 39 | clickEvent.Url; // string - URL on what the user has clicked 40 | clickEvent.UrlOffset; //UrlOffset - further info about what link was clicked 41 | ``` 42 | *Example JSON* 43 | ```json 44 | [ 45 | { 46 | "email": "john.doe@sendgrid.com", 47 | "timestamp": 1337197600, 48 | "smtp-id": "<4FB4041F.6080505@sendgrid.com>", 49 | "event": "processed" 50 | }, 51 | { 52 | "email": "john.doe@sendgrid.com", 53 | "timestamp": 1337966815, 54 | "category": "newuser", 55 | "event": "click", 56 | "url": "https://sendgrid.com" 57 | } 58 | ] 59 | ``` 60 | 61 | # Unique Arguments 62 | Is the ability to pass in additional arguments along with the message. This is mainly to add metadata to the message. [Find out more in the documentation](https://sendgrid.com/docs/API_Reference/SMTP_API/unique_arguments.html). 63 | ```json 64 | { 65 | "unique_args": { 66 | "customerAccountNumber": "55555", 67 | "activationAttempt": "1", 68 | "New Argument 1": "New Value 1", 69 | "New Argument 2": "New Value 2", 70 | "New Argument 3": "New Value 3", 71 | "New Argument 4": "New Value 4" 72 | } 73 | } 74 | ``` 75 | The `WebhookParser` looks at each unique property within the JSON and adds it to a UniqueParameters dictionary. 76 | ```csharp 77 | var value = event.UniqueParameters["customerAccountNumber"]; 78 | Console.WriteLine(value); // outputs 55555 79 | ``` 80 | 81 | # Overriding JsonConverter 82 | The parser supports a custom `JsonConverter` as an argument. This theoretically allows you to write your own custom DTOs, as long as they are still based on the `WebhookEventBase` class. 83 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | build/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studo 2015 cache/options directory 26 | .vs/ 27 | 28 | # MSTest test Results 29 | [Tt]est[Rr]esult*/ 30 | [Bb]uild[Ll]og.* 31 | 32 | # NUNIT 33 | *.VisualState.xml 34 | TestResult.xml 35 | 36 | # Build Results of an ATL Project 37 | [Dd]ebugPS/ 38 | [Rr]eleasePS/ 39 | dlldata.c 40 | 41 | *_i.c 42 | *_p.c 43 | *_i.h 44 | *.ilk 45 | *.meta 46 | *.obj 47 | *.pch 48 | *.pdb 49 | *.pgc 50 | *.pgd 51 | *.rsp 52 | *.sbr 53 | *.tlb 54 | *.tli 55 | *.tlh 56 | *.tmp 57 | *.tmp_proj 58 | *.log 59 | *.vspscc 60 | *.vssscc 61 | .builds 62 | *.pidb 63 | *.svclog 64 | *.scc 65 | 66 | # Chutzpah Test files 67 | _Chutzpah* 68 | 69 | # Visual C++ cache files 70 | ipch/ 71 | *.aps 72 | *.ncb 73 | *.opensdf 74 | *.sdf 75 | *.cachefile 76 | 77 | # Visual Studio profiler 78 | *.psess 79 | *.vsp 80 | *.vspx 81 | 82 | # TFS 2012 Local Workspace 83 | $tf/ 84 | 85 | # Guidance Automation Toolkit 86 | *.gpState 87 | 88 | # ReSharper is a .NET coding add-in 89 | _ReSharper*/ 90 | *.[Rr]e[Ss]harper 91 | *.DotSettings.user 92 | 93 | # JustCode is a .NET coding addin-in 94 | .JustCode 95 | 96 | # TeamCity is a build add-in 97 | _TeamCity* 98 | 99 | # DotCover is a Code Coverage Tool 100 | *.dotCover 101 | 102 | # NCrunch 103 | _NCrunch_* 104 | .*crunch*.local.xml 105 | 106 | # MightyMoose 107 | *.mm.* 108 | AutoTest.Net/ 109 | 110 | # Web workbench (sass) 111 | .sass-cache/ 112 | 113 | # Installshield output folder 114 | [Ee]xpress/ 115 | 116 | # DocProject is a documentation generator add-in 117 | DocProject/buildhelp/ 118 | DocProject/Help/*.HxT 119 | DocProject/Help/*.HxC 120 | DocProject/Help/*.hhc 121 | DocProject/Help/*.hhk 122 | DocProject/Help/*.hhp 123 | DocProject/Help/Html2 124 | DocProject/Help/html 125 | 126 | # Click-Once directory 127 | publish/ 128 | 129 | # Publish Web Output 130 | *.[Pp]ublish.xml 131 | *.azurePubxml 132 | # TODO: Comment the next line if you want to checkin your web deploy settings 133 | # but database connection strings (with potential passwords) will be unencrypted 134 | *.pubxml 135 | *.publishproj 136 | 137 | # NuGet Packages 138 | *.nupkg 139 | # The packages folder can be ignored because of Package Restore 140 | **/packages/* 141 | # except build/, which is used as an MSBuild target. 142 | !**/packages/build/ 143 | # Uncomment if necessary however generally it will be regenerated when needed 144 | #!**/packages/repositories.config 145 | 146 | # Windows Azure Build Output 147 | csx/ 148 | *.build.csdef 149 | 150 | # Windows Store app package directory 151 | AppPackages/ 152 | 153 | # Others 154 | *.[Cc]ache 155 | ClientBin/ 156 | [Ss]tyle[Cc]op.* 157 | ~$* 158 | *~ 159 | *.dbmdl 160 | *.dbproj.schemaview 161 | *.pfx 162 | *.publishsettings 163 | node_modules/ 164 | bower_components/ 165 | 166 | # RIA/Silverlight projects 167 | Generated_Code/ 168 | 169 | # Backup & report files from converting an old project file 170 | # to a newer Visual Studio version. Backup files are not needed, 171 | # because we have git ;-) 172 | _UpgradeReport_Files/ 173 | Backup*/ 174 | UpgradeLog*.XML 175 | UpgradeLog*.htm 176 | 177 | # SQL Server files 178 | *.mdf 179 | *.ldf 180 | 181 | # Business Intelligence projects 182 | *.rdl.data 183 | *.bim.layout 184 | *.bim_*.settings 185 | 186 | # Microsoft Fakes 187 | FakesAssemblies/ 188 | 189 | # Node.js Tools for Visual Studio 190 | .ntvs_analysis.dat 191 | 192 | # Visual Studio 6 build log 193 | *.plg 194 | 195 | # Visual Studio 6 workspace options file 196 | *.opt 197 | -------------------------------------------------------------------------------- /Sendgrid.Webhooks.Tests/JsonEventBuilder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Reflection; 4 | using System.Text; 5 | 6 | namespace Sendgrid.Webhooks.Tests 7 | { 8 | public class JsonEventBuilder 9 | { 10 | private StringBuilder builder; 11 | private String baseDirectory; 12 | 13 | public JsonEventBuilder() 14 | { 15 | builder = new StringBuilder("["); 16 | baseDirectory = Path.Combine(GetAssemblyDirectory().Parent.FullName, "Events"); 17 | } 18 | 19 | public JsonEventBuilder AppendProcessed() 20 | { 21 | AppendString(ReadContent("processed.json")); 22 | return this; 23 | } 24 | 25 | public JsonEventBuilder AppendProcessedWithSendAt() 26 | { 27 | AppendString(ReadContent("processed-with-sendat.json")); 28 | return this; 29 | } 30 | 31 | public JsonEventBuilder AppendBounce() 32 | { 33 | AppendString(ReadContent("bounce.json")); 34 | return this; 35 | } 36 | 37 | public JsonEventBuilder AppendBounceNull() 38 | { 39 | AppendString(ReadContent("bounce-null.json")); 40 | return this; 41 | } 42 | 43 | public JsonEventBuilder AppendBounceWithIp() 44 | { 45 | AppendString(ReadContent("bounce-with-ip.json")); 46 | return this; 47 | } 48 | 49 | public JsonEventBuilder AppendClick() 50 | { 51 | AppendString(ReadContent("click.json")); 52 | return this; 53 | } 54 | 55 | public JsonEventBuilder AppendClickWithUrlOffset() 56 | { 57 | AppendString(ReadContent("click-with-urloffset.json")); 58 | return this; 59 | } 60 | 61 | public JsonEventBuilder AppendDeferred() 62 | { 63 | AppendString(ReadContent("deferred.json")); 64 | return this; 65 | } 66 | 67 | public JsonEventBuilder AppendDelivered() 68 | { 69 | AppendString(ReadContent("delivered.json")); 70 | return this; 71 | } 72 | 73 | public JsonEventBuilder AppendDeliveredWithTlsAndCertError() 74 | { 75 | AppendString(ReadContent("delivered-with-tlsanderror.json")); 76 | return this; 77 | } 78 | 79 | public JsonEventBuilder AppendDrop() 80 | { 81 | AppendString(ReadContent("drop.json")); 82 | return this; 83 | } 84 | 85 | public JsonEventBuilder AppendGroupResubscribe() 86 | { 87 | AppendString(ReadContent("group_resubscribe.json")); 88 | return this; 89 | } 90 | 91 | public JsonEventBuilder AppendGroupUnsubscribe() 92 | { 93 | AppendString(ReadContent("group_unsubscribe.json")); 94 | return this; 95 | } 96 | 97 | public JsonEventBuilder AppendOpen() 98 | { 99 | AppendString(ReadContent("open.json")); 100 | return this; 101 | } 102 | 103 | public JsonEventBuilder AppendSpamReport() 104 | { 105 | AppendString(ReadContent("spamreport.json")); 106 | return this; 107 | } 108 | 109 | public JsonEventBuilder AppendUnsubscribe() 110 | { 111 | AppendString(ReadContent("unsubscribe.json")); 112 | return this; 113 | } 114 | 115 | public string Build() 116 | { 117 | builder.Append("]"); 118 | 119 | return builder.ToString(); 120 | } 121 | 122 | private void AppendString(string item) 123 | { 124 | if (builder.Length > 1) 125 | builder.Append(","); 126 | 127 | builder.Append(item); 128 | } 129 | 130 | private String ReadContent(string filename) 131 | { 132 | var path = Path.Combine(baseDirectory, filename); 133 | return File.ReadAllText(path); 134 | } 135 | 136 | //move out to helper 137 | private static DirectoryInfo GetAssemblyDirectory() 138 | { 139 | var codeBase = Assembly.GetExecutingAssembly().CodeBase; 140 | var uri = new UriBuilder(codeBase); 141 | var path = Uri.UnescapeDataString(uri.Path); 142 | return new DirectoryInfo(path); 143 | } 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /Sendgrid.Webhooks.Tests/WebhookParserTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using NUnit.Framework; 5 | using Sendgrid.Webhooks.Events; 6 | using Sendgrid.Webhooks.Service; 7 | 8 | namespace Sendgrid.Webhooks.Tests 9 | { 10 | [TestFixture] 11 | public class WebhookParserTests 12 | { 13 | private WebhookParser parser; 14 | 15 | [SetUp] 16 | public void SetUp() 17 | { 18 | parser = new WebhookParser(); 19 | } 20 | 21 | [Test] 22 | public void Parse_Bounce_Event() 23 | { 24 | var json = new JsonEventBuilder().AppendBounce().Build(); 25 | var result = parser.ParseEvents(json); 26 | 27 | AssertCommonProperties(result, typeof(BounceEvent)); 28 | var bounceEvent = result[0] as BounceEvent; 29 | Assert.AreEqual("500 No Such User", bounceEvent.Reason); 30 | Assert.AreEqual("bounce", bounceEvent.BounceType); 31 | Assert.AreEqual("5.0.0", bounceEvent.BounceStatus); 32 | } 33 | 34 | [Test] 35 | public void Parse_BounceWithNullArgs_EventIsStillParsed() 36 | { 37 | var json = new JsonEventBuilder().AppendBounceNull().Build(); 38 | var result = parser.ParseEvents(json); 39 | 40 | var bounceEvent = result[0] as BounceEvent; 41 | Assert.AreEqual("550 5.7.999 The user is inactive and not accepting messages.", bounceEvent.Reason); 42 | Assert.AreEqual("blocked", bounceEvent.BounceType); 43 | } 44 | 45 | [Test] 46 | public void Parse_BounceWithExtraProperties_IpIsPresent() 47 | { 48 | var json = new JsonEventBuilder().AppendBounceWithIp().Build(); 49 | var result = parser.ParseEvents(json); 50 | 51 | var bounceEvent = result[0] as BounceEvent; 52 | Assert.AreEqual("192.254.114.26", bounceEvent.Ip); 53 | } 54 | 55 | [Test] 56 | public void Parse_Click_Event() 57 | { 58 | var json = new JsonEventBuilder().AppendClick().Build(); 59 | var result = parser.ParseEvents(json); 60 | 61 | AssertCommonProperties(result, typeof(ClickEvent)); 62 | var clickEvent = result[0] as ClickEvent; 63 | Assert.AreEqual("http://yourdomain.com/blog/news.html", clickEvent.Url); 64 | } 65 | 66 | [Test] 67 | public void Parse_Click_EventWithUrlOffset() 68 | { 69 | var json = new JsonEventBuilder().AppendClickWithUrlOffset().Build(); 70 | var result = parser.ParseEvents(json); 71 | 72 | AssertCommonProperties(result, typeof(ClickEvent)); 73 | var clickEvent = result[0] as ClickEvent; 74 | Assert.AreEqual("http://yourdomain.com/blog/news.html", clickEvent.Url); 75 | Assert.IsNotNull(clickEvent); 76 | Assert.AreEqual(0, clickEvent.UrlOffset.Index); 77 | Assert.AreEqual("html", clickEvent.UrlOffset.Type); 78 | } 79 | 80 | [Test] 81 | public void Parse_Deferred_Event() 82 | { 83 | var json = new JsonEventBuilder().AppendDeferred().Build(); 84 | var result = parser.ParseEvents(json); 85 | 86 | AssertCommonProperties(result, typeof(DeferredEvent)); 87 | var deferredEvent = result[0] as DeferredEvent; 88 | Assert.AreEqual("400 Try again", deferredEvent.Response); 89 | Assert.AreEqual("10", deferredEvent.Attempts); 90 | } 91 | 92 | [Test] 93 | public void Parse_Delivered_Event() 94 | { 95 | var json = new JsonEventBuilder().AppendDelivered().Build(); 96 | var result = parser.ParseEvents(json); 97 | 98 | AssertCommonProperties(result, typeof(DeliveryEvent)); 99 | var deliveryEvent = result[0] as DeliveryEvent; 100 | Assert.IsFalse(deliveryEvent.CertificateError); 101 | Assert.IsFalse(deliveryEvent.Tls); 102 | } 103 | 104 | [Test] 105 | public void Parse_Delivered_EventWithTlsAndCertError() 106 | { 107 | var json = new JsonEventBuilder().AppendDeliveredWithTlsAndCertError().Build(); 108 | var result = parser.ParseEvents(json); 109 | 110 | AssertCommonProperties(result, typeof(DeliveryEvent)); 111 | var deliveryEvent = result[0] as DeliveryEvent; 112 | Assert.IsTrue(deliveryEvent.CertificateError); 113 | Assert.IsTrue(deliveryEvent.Tls); 114 | } 115 | 116 | [Test] 117 | public void Parse_Drop_Event() 118 | { 119 | var json = new JsonEventBuilder().AppendDrop().Build(); 120 | var result = parser.ParseEvents(json); 121 | 122 | AssertCommonProperties(result, typeof(DroppedEvent)); 123 | var dropEvent = result[0] as DroppedEvent; 124 | Assert.AreEqual("Bounced Address", dropEvent.Reason); 125 | } 126 | 127 | [Test] 128 | public void Parse_Open_Event() 129 | { 130 | var json = new JsonEventBuilder().AppendOpen().Build(); 131 | var result = parser.ParseEvents(json); 132 | 133 | AssertCommonProperties(result, typeof(OpenEvent)); ; 134 | } 135 | 136 | 137 | [Test] 138 | public void Parse_Processed_Event() 139 | { 140 | var json = new JsonEventBuilder().AppendProcessed().Build(); 141 | var result = parser.ParseEvents(json); 142 | 143 | AssertCommonProperties(result, typeof(ProcessedEvent)); 144 | } 145 | 146 | [Test] 147 | public void Parse_Processed_EventWithSentAt() 148 | { 149 | var json = new JsonEventBuilder().AppendProcessedWithSendAt().Build(); 150 | var result = parser.ParseEvents(json); 151 | 152 | AssertCommonProperties(result, typeof(ProcessedEvent)); 153 | var processedEvent = result[0] as ProcessedEvent; 154 | Assert.IsNotNull(processedEvent.SendAt); 155 | Assert.AreEqual(new DateTime(2009, 08, 11, 0, 3, 20, DateTimeKind.Utc), processedEvent.SendAt); 156 | } 157 | 158 | [Test] 159 | public void Parse_SpamReport_Event() 160 | { 161 | var json = new JsonEventBuilder().AppendSpamReport().Build(); 162 | var result = parser.ParseEvents(json); 163 | 164 | AssertCommonProperties(result, typeof(SpamReportEvent)); 165 | } 166 | 167 | [Test] 168 | public void Parse_Unsubscribe_Event() 169 | { 170 | var json = new JsonEventBuilder().AppendUnsubscribe().Build(); 171 | var result = parser.ParseEvents(json); 172 | 173 | AssertCommonProperties(result, typeof(UnsubscribeEvent)); 174 | } 175 | 176 | [Test] 177 | public void Parse_Group_Resubscribe_Event() 178 | { 179 | var json = new JsonEventBuilder().AppendGroupResubscribe().Build(); 180 | var result = parser.ParseEvents(json); 181 | 182 | AssertCommonProperties(result, typeof(GroupResubscribeEvent)); 183 | var castEvent = result[0] as GroupResubscribeEvent; 184 | Assert.AreEqual(1, castEvent.GroupId); 185 | } 186 | 187 | [Test] 188 | public void Parse_Group_Unsubscribe_Event() 189 | { 190 | var json = new JsonEventBuilder().AppendGroupUnsubscribe().Build(); 191 | var result = parser.ParseEvents(json); 192 | 193 | AssertCommonProperties(result, typeof(GroupUnsubscribeEvent)); 194 | var castEvent = result[0] as GroupUnsubscribeEvent; 195 | Assert.AreEqual(1, castEvent.GroupId); 196 | } 197 | 198 | private void AssertCommonProperties(IList result, Type expectedType) 199 | { 200 | Assert.IsNotNull(result); 201 | Assert.AreEqual(1, result.Count); 202 | 203 | var webhookEvent = result.FirstOrDefault(); 204 | Assert.IsNotNull(webhookEvent); 205 | Assert.IsTrue(webhookEvent.GetType() == expectedType, "Expected type of: {0}", expectedType); 206 | 207 | //has sg_message_id 208 | Assert.AreEqual("sendgrid_internal_message_id", webhookEvent.SgMessageId); 209 | 210 | //has sg_event_id 211 | Assert.AreEqual("sendgrid_internal_event_id", webhookEvent.SgEventId); 212 | 213 | //has unique keys 214 | Assert.IsTrue(webhookEvent.UniqueParameters.ContainsKey("unique_arg_key")); 215 | Assert.AreEqual("unique_arg_value", webhookEvent.UniqueParameters["unique_arg_key"]); 216 | 217 | //has categories 218 | Assert.IsNotNull(webhookEvent.Category); 219 | Assert.AreEqual(2, webhookEvent.Category.Count); 220 | CollectionAssert.AreEquivalent(webhookEvent.Category, new[] { "category1", "category2" }); 221 | 222 | //has correct timestamp 223 | Assert.AreEqual(new DateTime(2009, 08, 11, 0, 0, 0, DateTimeKind.Utc), webhookEvent.Timestamp); 224 | } 225 | } 226 | } 227 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | --------------------------------------------------------------------------------