Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the Personalization’s dynamicTemplateData generic #17

Merged
merged 3 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 28 additions & 4 deletions Sources/SendGridKit/Models/Personalization.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public struct Personalization: Codable {
public struct Personalization<DynamicTemplateData: Codable>: Codable {

/// An array of recipients. Each object within this array may contain the name, but must always contain the email, of a recipient.
public var to: [EmailAddress]?
Expand All @@ -21,8 +21,8 @@ public struct Personalization: Codable {
public var substitutions: [String: String]?

/// A collection of key/value pairs following the pattern "key":"value" to substitute handlebar template data
public var dynamicTemplateData: [String: String]?
public var dynamicTemplateData: DynamicTemplateData?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This technically breaks public API correct? @tonyarnold

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it does, sadly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright we'll just have new major versions of Kit and the helper library.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry! Without this change, though, it's not really possible to use SendGrid's dynamic templates properly.

Seems worth it? 😄


/// Values that are specific to this personalization that will be carried along with the email and its activity data.
public var customArgs: [String: String]?

Expand All @@ -36,7 +36,7 @@ public struct Personalization: Codable {
subject: String? = nil,
headers: [String: String]? = nil,
substitutions: [String: String]? = nil,
dynamicTemplateData: [String: String]? = nil,
dynamicTemplateData: DynamicTemplateData? = nil,
customArgs: [String: String]? = nil,
sendAt: Date? = nil
) {
Expand Down Expand Up @@ -64,3 +64,27 @@ public struct Personalization: Codable {
}

}

public extension Personalization where DynamicTemplateData == [String: String] {
init(
to: [EmailAddress]? = nil,
cc: [EmailAddress]? = nil,
bcc: [EmailAddress]? = nil,
subject: String? = nil,
headers: [String: String]? = nil,
substitutions: [String: String]? = nil,
dynamicTemplateData: [String: String]? = nil,
customArgs: [String: String]? = nil,
sendAt: Date? = nil
) {
self.to = to
self.cc = cc
self.bcc = bcc
self.subject = subject
self.headers = headers
self.substitutions = substitutions
self.dynamicTemplateData = dynamicTemplateData
self.customArgs = customArgs
self.sendAt = sendAt
}
}
48 changes: 44 additions & 4 deletions Sources/SendGridKit/Models/SendGridEmail.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Foundation

public struct SendGridEmail: Codable {
public struct SendGridEmail<DynamicTemplateData: Codable>: Codable {

/// An array of messages and their metadata. Each object within personalizations can be thought of as an envelope - it defines who should receive an individual message and how that message should be handled.
public var personalizations: [Personalization]
public var personalizations: [Personalization<DynamicTemplateData>]

public var from: EmailAddress

Expand Down Expand Up @@ -52,7 +52,7 @@ public struct SendGridEmail: Codable {
public var trackingSettings: TrackingSettings?

public init(
personalizations: [Personalization],
personalizations: [Personalization<DynamicTemplateData>],
from: EmailAddress,
replyTo: EmailAddress? = nil,
replyToList: [EmailAddress]? = nil,
Expand Down Expand Up @@ -110,3 +110,43 @@ public struct SendGridEmail: Codable {
}

}

public extension SendGridEmail where DynamicTemplateData == [String: String] {
init(
personalizations: [Personalization<[String: String]>],
from: EmailAddress,
replyTo: EmailAddress? = nil,
replyToList: [EmailAddress]? = nil,
subject: String? = nil,
content: [EmailContent]? = nil,
attachments: [EmailAttachment]? = nil,
templateId: String? = nil,
headers: [String: String]? = nil,
categories: [String]? = nil,
customArgs: [String: String]? = nil,
sendAt: Date? = nil,
batchId: String? = nil,
asm: AdvancedSuppressionManager? = nil,
ipPoolName: String? = nil,
mailSettings: MailSettings? = nil,
trackingSettings: TrackingSettings? = nil
) {
self.personalizations = personalizations
self.from = from
self.replyTo = replyTo
self.replyToList = replyToList
self.subject = subject
self.content = content
self.attachments = attachments
self.templateId = templateId
self.headers = headers
self.categories = categories
self.customArgs = customArgs
self.sendAt = sendAt
self.batchId = batchId
self.asm = asm
self.ipPoolName = ipPoolName
self.mailSettings = mailSettings
self.trackingSettings = trackingSettings
}
}
4 changes: 2 additions & 2 deletions Sources/SendGridKit/SendGridClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public struct SendGridClient {
self.apiKey = apiKey
}

public func send(email: SendGridEmail) async throws {
public func send<DynamicTemplateData: Codable>(email: SendGridEmail<DynamicTemplateData>) async throws {

var headers = HTTPHeaders()
headers.add(name: "Authorization", value: "Bearer \(apiKey)")
headers.add(name: "Content-Type", value: "application/json")
Expand Down
2 changes: 1 addition & 1 deletion Tests/SendGridKitTests/SendGridTestsKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SendGridKitTests: XCTestCase {

// TODO: Replace to address with the email address you'd like to recieve your test email!
let personalization = Personalization(to: ["TO-ADDRESS"])

// TODO: Replace from address with the email address associated with your verified Sender Identity!
let email = SendGridEmail(
personalizations: [personalization],
Expand Down