Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from vapor-community/vapor3
Vapor 3 support
- Loading branch information
Showing
34 changed files
with
552 additions
and
825 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
.build | ||
Package.pins | ||
|
||
.DS_Store | ||
*.xcodeproj | ||
/.build | ||
/Packages | ||
/*.xcodeproj | ||
Package.pins | ||
Package.resolved |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,16 @@ | ||
// swift-tools-version:4.0 | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "SendGridProvider", | ||
name: "SendGrid", | ||
products: [ | ||
.library(name: "SendGrid", targets: ["SendGrid"]) | ||
], | ||
dependencies: [ | ||
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 2), | ||
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0-rc"), | ||
], | ||
targets: [ | ||
.target(name: "SendGrid", dependencies: ["Vapor"]), | ||
.testTarget(name: "SendGridTests", dependencies: ["Vapor", "SendGrid"]) | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// sourcery:file:Tests/LinuxMain.swift | ||
import XCTest | ||
{{ argument.testimports }} | ||
|
||
{% for type in types.classes|based:"XCTestCase" %} | ||
{% if not type.annotations.disableTests %}extension {{ type.name }} { | ||
static var allTests = [ | ||
{% for method in type.methods %}{% if method.parameters.count == 0 and method.shortName|hasPrefix:"test" %} ("{{ method.shortName }}", {{ method.shortName }}), | ||
{% endif %}{% endfor %}] | ||
} | ||
|
||
{% endif %}{% endfor %} | ||
|
||
XCTMain([ | ||
{% for type in types.classes|based:"XCTestCase" %}{% if not type.annotations.disableTests %} testCase({{ type.name }}.allTests), | ||
{% endif %}{% endfor %}]) | ||
// sourcery:end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Vapor | ||
|
||
public struct AdvancedSuppressionManager: Content { | ||
/// The unsubscribe group to associate with this email. | ||
public var groupId: Int? | ||
|
||
/// An array containing the unsubscribe groups that you would like to be displayed on the unsubscribe preferences page. | ||
public var groupsToDisplay: [String]? | ||
|
||
public enum CodingKeys: CodingKey, String { | ||
case groupId = "group_id" | ||
case groupsToDisplay = "groups_to_display" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// EmailAddress.swift | ||
// Sendgrid | ||
// | ||
// Created by Andrew Edwards on 3/29/18. | ||
// | ||
|
||
import Vapor | ||
|
||
public struct EmailAddress: Content { | ||
/// format: email | ||
var email: String? | ||
|
||
/// The name of the person to whom you are sending an email. | ||
var name: String? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// EmailAttachment.swift | ||
// Sendgrid | ||
// | ||
// Created by Andrew Edwards on 3/29/18. | ||
// | ||
|
||
import Vapor | ||
|
||
public struct EmailAttachment: Content { | ||
|
||
/// The Base64 encoded content of the attachment. | ||
public var content: String? | ||
|
||
/// The mime type of the content you are attaching. For example, “text/plain” or “text/html”. | ||
public var type: String? | ||
|
||
/// The filename of the attachment. | ||
public var filename: String? | ||
|
||
/// The content-disposition of the attachment specifying how you would like the attachment to be displayed. | ||
public var disposition: String? | ||
|
||
/// The content id for the attachment. This is used when the disposition is set to “inline” and the attachment is an image, allowing the file to be displayed within the body of your email. | ||
public var contentId: String? | ||
|
||
public enum CodingKeys: CodingKey, String { | ||
case content | ||
case type | ||
case filename | ||
case disposition | ||
case contentId = "content_id" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// | ||
// MailSettings.swift | ||
// Sendgrid | ||
// | ||
// Created by Andrew Edwards on 3/29/18. | ||
// | ||
|
||
import Vapor | ||
|
||
public struct MailSettings: Content { | ||
/// This allows you to have a blind carbon copy automatically sent to the specified email address for every email that is sent. | ||
public var bcc: BCC? | ||
|
||
/// Allows you to bypass all unsubscribe groups and suppressions to ensure that the email is delivered to every single recipient. This should only be used in emergencies when it is absolutely necessary that every recipient receives your email. | ||
public var bypassListManagement: BypassListManagement? | ||
|
||
/// The default footer that you would like included on every email. | ||
public var footer: Footer? | ||
|
||
/// This allows you to send a test email to ensure that your request body is valid and formatted correctly. | ||
public var sandboxMode: SandboxMode? | ||
|
||
/// This allows you to test the content of your email for spam. | ||
public var spamCheck: SpamCheck? | ||
|
||
public enum CodingKeys: CodingKey, String { | ||
case bcc | ||
case bypassListManagement = "bypass_list_management" | ||
case footer | ||
case sandboxMode = "sandbox_mode" | ||
case spamCheck = "spam_check" | ||
} | ||
} | ||
|
||
public struct BCC: Content { | ||
/// Indicates if this setting is enabled. | ||
public var enable: Bool? | ||
public var email: String? | ||
} | ||
|
||
public struct BypassListManagement: Content { | ||
/// Indicates if this setting is enabled. | ||
public var enable: Bool? | ||
} | ||
|
||
public struct Footer: Content { | ||
/// Indicates if this setting is enabled. | ||
public var enable: Bool? | ||
|
||
/// The plain text content of your footer. | ||
public var text: String? | ||
|
||
/// The HTML content of your footer. | ||
public var html: String? | ||
} | ||
|
||
public struct SandboxMode: Content { | ||
/// Indicates if this setting is enabled. | ||
public var enable: Bool? | ||
} | ||
|
||
public struct SpamCheck: Content { | ||
/// Indicates if this setting is enabled. | ||
public var enable: Bool? | ||
|
||
/// The threshold used to determine if your content qualifies as spam on a scale from 1 to 10, with 10 being most strict, or most likely to be considered as spam. | ||
public var threshold: Int? | ||
|
||
/// An Inbound Parse URL that you would like a copy of your email along with the spam report to be sent to. | ||
public var postToUrl: String? | ||
|
||
public enum CodingKeys: CodingKey, String { | ||
case enable | ||
case threshold | ||
case postToUrl = "post_to_url" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import Vapor | ||
|
||
public struct Personalization: Content { | ||
|
||
/// 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]? | ||
|
||
/// An array of recipients who will receive a copy of your email. Each object within this array may contain the name, but must always contain the email, of a recipient. | ||
public var cc: [EmailAddress]? | ||
|
||
/// An array of recipients who will receive a blind carbon copy of your email. Each object within this array may contain the name, but must always contain the email, of a recipient. | ||
public var bcc: [EmailAddress]? | ||
|
||
/// The subject of your email. | ||
public var subject: String? | ||
|
||
/// A collection of JSON key/value pairs allowing you to specify specific handling instructions for your email. | ||
public var headers: [String: String]? | ||
|
||
/// A collection of key/value pairs following the pattern "substitution_tag":"value to substitute". | ||
public var substitutions: [String: String]? | ||
|
||
/// Values that are specific to this personalization that will be carried along with the email and its activity data. | ||
public var customArgs: [String: String]? | ||
|
||
/// A unix timestamp allowing you to specify when you want your email to be delivered. Scheduling more than 72 hours in advance is forbidden. | ||
public var sendAt: Date? | ||
|
||
public enum CodingKeys: CodingKey, String { | ||
case to | ||
case cc | ||
case bcc | ||
case subject | ||
case headers | ||
case substitutions | ||
case customArgs = "custom_args" | ||
case sendAt = "send_at" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import Vapor | ||
|
||
public final class SendGridClient: Service { | ||
let httpClient: Client | ||
let apiKey: String | ||
let apiEndpoint = "https://api.sendgrid.com/v3/mail/send" | ||
|
||
public init(client: Client, apiKey: String) { | ||
self.httpClient = client | ||
self.apiKey = apiKey | ||
} | ||
|
||
public func send(_ emails: [SendGridEmail], on worker: Worker) throws -> Future<Void> { | ||
|
||
return try emails.map { (email) in | ||
|
||
var headers: HTTPHeaders = [:] | ||
headers.add(name: .contentType, value: MediaType.json.description) | ||
headers.add(name: .authorization, value: "Bearer \(apiKey)") | ||
|
||
let encoder = JSONEncoder() | ||
|
||
encoder.dateEncodingStrategy = .secondsSince1970 | ||
|
||
let body = try encoder.encodeBody(from: email) | ||
|
||
let request = HTTPRequest(method: .POST, url: URL(string: apiEndpoint) ?? .root, headers: headers, body: body) | ||
|
||
return try httpClient.respond(to: Request(http: request, using: httpClient.container)).map(to: Void.self, { (response) -> (Void) in | ||
|
||
switch response.http.status { | ||
case .ok, .accepted: return | ||
default: | ||
let decoder = JSONDecoder() | ||
decoder.dateDecodingStrategy = .secondsSince1970 | ||
let error = try decoder.decode(SendGridError.self, from: response.http.body.data ?? Data()) | ||
|
||
throw error | ||
} | ||
}) | ||
}.flatten(on: worker) | ||
} | ||
} |
Oops, something went wrong.