Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/Resend.Webhooks/EmailEventAttachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@ public class EmailEventAttachment
{
/// <summary />
[JsonPropertyName( "id" )]
public Guid Id { get; set; }

/// <summary />
[JsonPropertyName( "filename" )]
public string Filename { get; set; } = default!;
public string Id { get; set; } = default!;

/// <summary />
[JsonPropertyName( "content_type" )]
public string ContentType { get; set; } = default!;

/// <summary />
[JsonPropertyName( "filename" )]
[JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )]
public string? Filename { get; set; }

/// <summary />
[JsonPropertyName( "content_disposition" )]
public string ContentDisposition { get; set; } = default!;
[JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )]
public string? ContentDisposition { get; set; }

/// <summary />
[JsonPropertyName( "content_id" )]
public string ContentId { get; set; } = default!;
[JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )]
public string? ContentId { get; set; }
}
56 changes: 54 additions & 2 deletions src/Resend.Webhooks/EmailEventData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,25 @@ public class EmailEventData : IWebhookData
[JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )]
public EmailAddressList? Cc { get; set; } = default!;

/// <summary />
/// <remarks>
/// Only set for <see cref="WebhookEventType.EmailReceived" />, otherwise is null.
/// </remarks>
[JsonPropertyName( "received_for" )]
[JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )]
public EmailAddressList? ReceivedFor { get; set; }

/// <summary />
[JsonPropertyName( "subject" )]
public string Subject { get; set; } = default!;

/// <summary>
/// Custom headers included on the email.
/// </summary>
[JsonPropertyName( "headers" )]
[JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )]
public List<EmailEventHeader>? Headers { get; set; }

/// <summary />
[JsonPropertyName( "broadcast_id" )]
[JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )]
Expand Down Expand Up @@ -80,6 +95,14 @@ public class EmailEventData : IWebhookData
[JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )]
public EmailClickData? Click { get; set; }

/// <summary />
/// <remarks>
/// Only set for <see cref="WebhookEventType.EmailOpened" />, otherwise is null.
/// </remarks>
[JsonPropertyName( "open" )]
[JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )]
public EmailOpenData? Open { get; set; }

/// <summary />
/// <remarks>
/// Only set for <see cref="WebhookEventType.EmailBounced" />, otherwise is null.
Expand Down Expand Up @@ -152,6 +175,24 @@ public class EmailClickData
}


/// <summary />
public class EmailOpenData
{
/// <summary />
[JsonPropertyName( "ipAddress" )]
public string IpAddress { get; set; } = default!;

/// <summary />
[JsonPropertyName( "timestamp" )]
[JsonConverter( typeof( JsonUtcDateTimeConverter ) )]
public DateTime MomentOpened { get; set; }

/// <summary />
[JsonPropertyName( "userAgent" )]
public string UserAgent { get; set; } = default!;
}


/// <summary />
public class EmailFailedData
{
Expand All @@ -169,7 +210,18 @@ public class EmailSuppressedData
public string Message { get; set; } = default!;

/// <summary />
/// <remarks>TODO: What are the possible values?</remarks>
/// <remarks>One of "Suppressed" or "OnAccountSuppressionList".</remarks>
[JsonPropertyName( "type" )]
public string Type { get; set; } = default!;
public string Type { get; set; } = default!;

/// <summary />
/// <remarks>One of "previous_bounce" or "previous_complaint".</remarks>
[JsonPropertyName( "reason" )]
[JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )]
public string? Reason { get; set; }

/// <summary />
[JsonPropertyName( "diagnosticCode" )]
[JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )]
public List<string>? DiagnosticCode { get; set; }
}
17 changes: 17 additions & 0 deletions src/Resend.Webhooks/EmailEventHeader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Text.Json.Serialization;

namespace Resend.Webhooks;

/// <summary>
/// Custom header included on an email event.
/// </summary>
public class EmailEventHeader
{
/// <summary />
[JsonPropertyName( "name" )]
public string Name { get; set; } = default!;

/// <summary />
[JsonPropertyName( "value" )]
public string Value { get; set; } = default!;
}
129 changes: 129 additions & 0 deletions tests/Resend.Webhooks.Tests/WebhookDataFieldsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using System.Text.Json;

namespace Resend.Webhooks.Tests;

/// <summary />
public class WebhookDataFieldsTests
{
/// <summary />
[Fact]
public void EmailOpenedDeserializesOpenAndHeaders()
{
var json = """
{
"type": "email.opened",
"created_at": "2024-11-22T23:41:12.126Z",
"data": {
"created_at": "2024-11-22T23:41:11.894Z",
"email_id": "56761188-7520-42d8-8898-ff6fc54ce618",
"from": "onboarding@resend.dev",
"to": [ "delivered@resend.dev" ],
"subject": "Sending this example",
"headers": [ { "name": "X-Entity-Ref-ID", "value": "abc-123" } ],
"open": { "ipAddress": "1.2.3.4", "timestamp": "2024-11-22T23:41:12.126Z", "userAgent": "Mozilla/5.0" }
}
}
""";

var evt = JsonSerializer.Deserialize<WebhookEvent>( json );

Assert.NotNull( evt );
Assert.Equal( WebhookEventType.EmailOpened, evt.EventType );

var data = evt.DataAs<EmailEventData>();

Assert.NotNull( data.Open );
Assert.Equal( "1.2.3.4", data.Open!.IpAddress );
Assert.Equal( "Mozilla/5.0", data.Open.UserAgent );

Assert.NotNull( data.Headers );
Assert.Single( data.Headers );
Assert.Equal( "X-Entity-Ref-ID", data.Headers![ 0 ].Name );
Assert.Equal( "abc-123", data.Headers[ 0 ].Value );
}


/// <summary />
[Fact]
public void EmailReceivedDeserializesInboundFields()
{
var json = """
{
"type": "email.received",
"created_at": "2024-11-22T23:41:12.126Z",
"data": {
"created_at": "2024-11-22T23:41:11.894Z",
"email_id": "56761188-7520-42d8-8898-ff6fc54ce618",
"from": "sender@example.com",
"to": [ "inbox@example.com" ],
"subject": "Inbound",
"message_id": "<abc@mail.example.com>",
"bcc": [],
"cc": [ "copy@example.com" ],
"received_for": [ "inbox@example.com" ],
"attachments": [ {
"id": "att_123",
"content_type": "image/png",
"content_disposition": "inline",
"filename": "avatar.png",
"content_id": "img001"
} ]
}
}
""";

var evt = JsonSerializer.Deserialize<WebhookEvent>( json );

Assert.NotNull( evt );
Assert.Equal( WebhookEventType.EmailReceived, evt.EventType );

var data = evt.DataAs<EmailEventData>();

Assert.Equal( "<abc@mail.example.com>", data.MessageId );
Assert.NotNull( data.ReceivedFor );
Assert.Single( data.ReceivedFor! );
Assert.NotNull( data.Attachments );
Assert.Single( data.Attachments! );
Assert.Equal( "att_123", data.Attachments![ 0 ].Id );
Assert.Equal( "avatar.png", data.Attachments[ 0 ].Filename );
}


/// <summary />
[Fact]
public void EmailSuppressedDeserializesAllFields()
{
var json = """
{
"type": "email.suppressed",
"created_at": "2024-11-22T23:41:12.126Z",
"data": {
"created_at": "2024-11-22T23:41:11.894Z",
"email_id": "56761188-7520-42d8-8898-ff6fc54ce618",
"from": "sender@example.com",
"to": [ "suppressed@example.com" ],
"subject": "Suppressed",
"suppressed": {
"message": "recipient is on the account suppression list",
"type": "OnAccountSuppressionList",
"reason": "previous_bounce",
"diagnosticCode": [ "smtp; 550 5.1.1 user unknown" ]
}
}
}
""";

var evt = JsonSerializer.Deserialize<WebhookEvent>( json );

Assert.NotNull( evt );
Assert.Equal( WebhookEventType.EmailSuppressed, evt.EventType );

var data = evt.DataAs<EmailEventData>();

Assert.NotNull( data.Suppressed );
Assert.Equal( "OnAccountSuppressionList", data.Suppressed!.Type );
Assert.Equal( "previous_bounce", data.Suppressed.Reason );
Assert.NotNull( data.Suppressed.DiagnosticCode );
Assert.Single( data.Suppressed.DiagnosticCode! );
}
}
Loading