Skip to content

Commit 49f4b1d

Browse files
authored
Merge pull request #1 from dylanshine/master
SendGridKit Version 1.0.0
2 parents 01f83a5 + 1ebeb75 commit 49f4b1d

16 files changed

+836
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
.build
3+
/Packages
4+
*.xcodeproj
5+
Package.pins
6+
Package.resolved
7+
.swiftpm/

CHANGELOG.md

Whitespace-only changes.

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
MIT LICENSE
3+
4+
Copyright (c) 2020 Dylan Shine
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.

Package.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// swift-tools-version:5.2
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "sendgrid-kit",
6+
platforms: [
7+
.macOS(.v10_15),
8+
],
9+
products: [
10+
.library(name: "SendGridKit", targets: ["SendGridKit"]),
11+
],
12+
dependencies: [
13+
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.0.0"),
14+
],
15+
targets: [
16+
.target(name: "SendGridKit", dependencies: [
17+
.product(name: "AsyncHTTPClient", package: "async-http-client"),
18+
]),
19+
.testTarget(name: "SendGridKitTests", dependencies: [
20+
.target(name: "SendGridKit"),
21+
])
22+
]
23+
)

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# SendGridKit
2+
3+
![Swift](http://img.shields.io/badge/swift-5.2-brightgreen.svg)
4+
5+
SendGridKit is a Swift package used to communicate with the SendGrid API for Server Side Swift Apps.
6+
7+
## Setup
8+
Add the dependency to Package.swift:
9+
10+
~~~~swift
11+
dependencies: [
12+
...
13+
.package(url: "https://github.com/vapor-community/sendgrid-kit.git", from: "1.0.0")
14+
],
15+
targets: [
16+
.target(name: "App", dependencies: [
17+
.product(name: "SendGridKit", package: "sendgrid-kit"),
18+
]),
19+
~~~~
20+
21+
Register the config and the provider.
22+
23+
~~~~swift
24+
let httpClient = HTTPClient(...)
25+
let sendgridClient = SendGridClient(httpClient: httpClient, apiKey: "YOUR_API_KEY")
26+
~~~~
27+
28+
## Using the API
29+
30+
You can use all of the available parameters here to build your `SendGridEmail`
31+
Usage in a route closure would be as followed:
32+
33+
~~~~swift
34+
import SendGrid
35+
36+
let email = SendGridEmail(...)
37+
try sendGridClient.send(email, on: eventLoop)
38+
~~~~
39+
40+
## Error handling
41+
If the request to the API failed for any reason a `SendGridError` is `thrown` and has an `errors` property that contains an array of errors returned by the API.
42+
Simply ensure you catch errors thrown like any other throwing function
43+
44+
~~~~swift
45+
do {
46+
try sendGridClient.send(...)
47+
}
48+
catch let error as SendGridError {
49+
print(error)
50+
}
51+
~~~~
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import Foundation
2+
3+
public struct AdvancedSuppressionManager: Encodable {
4+
/// The unsubscribe group to associate with this email.
5+
public var groupId: Int
6+
7+
/// An array containing the unsubscribe groups that you would like to be displayed on the unsubscribe preferences page.
8+
public var groupsToDisplay: [String]?
9+
10+
public init(groupId: Int,
11+
groupsToDisplay: [String]? = nil) {
12+
self.groupId = groupId
13+
self.groupsToDisplay = groupsToDisplay
14+
}
15+
16+
private enum CodingKeys: String, CodingKey {
17+
case groupId = "group_id"
18+
case groupsToDisplay = "groups_to_display"
19+
}
20+
21+
public func encode(to encoder: Encoder) throws {
22+
var container = encoder.container(keyedBy: CodingKeys.self)
23+
try container.encode(groupId, forKey: .groupId)
24+
try container.encode(groupsToDisplay, forKey: .groupsToDisplay)
25+
}
26+
27+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import Foundation
2+
3+
public struct EmailAddress: Encodable {
4+
/// format: email
5+
public var email: String
6+
7+
/// The name of the person to whom you are sending an email.
8+
public var name: String?
9+
10+
public init(email: String,
11+
name: String? = nil) {
12+
self.email = email
13+
self.name = name
14+
}
15+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// EmailAttachment.swift
3+
// Sendgrid
4+
//
5+
// Created by Andrew Edwards on 3/29/18.
6+
//
7+
8+
import Foundation
9+
10+
public struct EmailAttachment: Encodable {
11+
12+
/// The Base64 encoded content of the attachment.
13+
public var content: String
14+
15+
/// The mime type of the content you are attaching. For example, “text/plain” or “text/html”.
16+
public var type: String?
17+
18+
/// The filename of the attachment.
19+
public var filename: String
20+
21+
/// The content-disposition of the attachment specifying how you would like the attachment to be displayed.
22+
public var disposition: String?
23+
24+
/// 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.
25+
public var contentId: String?
26+
27+
public init(content: String,
28+
type: String? = nil,
29+
filename: String,
30+
disposition: String? = nil,
31+
contentId: String? = nil) {
32+
self.content = content
33+
self.type = type
34+
self.filename = filename
35+
self.disposition = disposition
36+
self.contentId = contentId
37+
}
38+
39+
private enum CodingKeys: String, CodingKey {
40+
case content
41+
case type
42+
case filename
43+
case disposition
44+
case contentId = "content_id"
45+
}
46+
47+
public func encode(to encoder: Encoder) throws {
48+
var container = encoder.container(keyedBy: CodingKeys.self)
49+
try container.encode(content, forKey: .content)
50+
try container.encode(type, forKey: .type)
51+
try container.encode(filename, forKey: .filename)
52+
try container.encode(disposition, forKey: .disposition)
53+
try container.encode(contentId, forKey: .contentId)
54+
}
55+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import Foundation
2+
3+
public struct MailSettings: Encodable {
4+
/// This allows you to have a blind carbon copy automatically sent to the specified email address for every email that is sent.
5+
public var bcc: BCC?
6+
7+
/// 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.
8+
public var bypassListManagement: BypassListManagement?
9+
10+
/// The default footer that you would like included on every email.
11+
public var footer: Footer?
12+
13+
/// This allows you to send a test email to ensure that your request body is valid and formatted correctly.
14+
public var sandboxMode: SandboxMode?
15+
16+
/// This allows you to test the content of your email for spam.
17+
public var spamCheck: SpamCheck?
18+
19+
public init(bcc: BCC? = nil,
20+
bypassListManagement: BypassListManagement? = nil,
21+
footer: Footer? = nil,
22+
sandboxMode: SandboxMode? = nil,
23+
spamCheck: SpamCheck? = nil) {
24+
self.bcc = bcc
25+
self.bypassListManagement = bypassListManagement
26+
self.footer = footer
27+
self.sandboxMode = sandboxMode
28+
self.spamCheck = spamCheck
29+
}
30+
31+
private enum CodingKeys: String, CodingKey {
32+
case bcc
33+
case bypassListManagement = "bypass_list_management"
34+
case footer
35+
case sandboxMode = "sandbox_mode"
36+
case spamCheck = "spam_check"
37+
}
38+
39+
public func encode(to encoder: Encoder) throws {
40+
var container = encoder.container(keyedBy: CodingKeys.self)
41+
try container.encode(bcc, forKey: .bcc)
42+
try container.encode(bypassListManagement, forKey: .bypassListManagement)
43+
try container.encode(footer, forKey: .footer)
44+
try container.encode(sandboxMode, forKey: .sandboxMode)
45+
try container.encode(spamCheck, forKey: .spamCheck)
46+
}
47+
}
48+
49+
public struct BCC: Encodable {
50+
/// Indicates if this setting is enabled.
51+
public var enable: Bool?
52+
public var email: String?
53+
54+
public init(enable: Bool? = nil,
55+
email: String? = nil) {
56+
self.enable = enable
57+
self.email = email
58+
}
59+
}
60+
61+
public struct BypassListManagement: Encodable {
62+
/// Indicates if this setting is enabled.
63+
public var enable: Bool?
64+
65+
public init(enable: Bool? = nil) {
66+
self.enable = enable
67+
}
68+
}
69+
70+
public struct Footer: Encodable {
71+
/// Indicates if this setting is enabled.
72+
public var enable: Bool?
73+
74+
/// The plain text content of your footer.
75+
public var text: String?
76+
77+
/// The HTML content of your footer.
78+
public var html: String?
79+
80+
public init(enable: Bool? = nil,
81+
text: String? = nil,
82+
html: String? = nil) {
83+
self.enable = enable
84+
self.text = text
85+
self.html = html
86+
}
87+
}
88+
89+
public struct SandboxMode: Encodable {
90+
/// Indicates if this setting is enabled.
91+
public var enable: Bool?
92+
93+
public init(enable: Bool? = nil) {
94+
self.enable = enable
95+
}
96+
}
97+
98+
public struct SpamCheck: Encodable {
99+
/// Indicates if this setting is enabled.
100+
public var enable: Bool?
101+
102+
/// 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.
103+
public var threshold: Int?
104+
105+
/// An Inbound Parse URL that you would like a copy of your email along with the spam report to be sent to.
106+
public var postToUrl: String?
107+
108+
public init(enable: Bool? = nil,
109+
threshold: Int? = nil,
110+
postToUrl: String? = nil) {
111+
self.enable = enable
112+
self.threshold = threshold
113+
self.postToUrl = postToUrl
114+
}
115+
116+
private enum CodingKeys: String, CodingKey {
117+
case enable
118+
case threshold
119+
case postToUrl = "post_to_url"
120+
}
121+
122+
public func encode(to encoder: Encoder) throws {
123+
var container = encoder.container(keyedBy: CodingKeys.self)
124+
try container.encode(enable, forKey: .enable)
125+
try container.encode(threshold, forKey: .threshold)
126+
try container.encode(postToUrl, forKey: .postToUrl)
127+
}
128+
129+
}

0 commit comments

Comments
 (0)