Skip to content

Commit

Permalink
Merge pull request #1 from dylanshine/master
Browse files Browse the repository at this point in the history
SendGridKit Version 1.0.0
  • Loading branch information
Andrewangeta authored Mar 16, 2020
2 parents 01f83a5 + 1ebeb75 commit 49f4b1d
Show file tree
Hide file tree
Showing 16 changed files with 836 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.DS_Store
.build
/Packages
*.xcodeproj
Package.pins
Package.resolved
.swiftpm/
Empty file added CHANGELOG.md
Empty file.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

MIT LICENSE

Copyright (c) 2020 Dylan Shine

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
23 changes: 23 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// swift-tools-version:5.2
import PackageDescription

let package = Package(
name: "sendgrid-kit",
platforms: [
.macOS(.v10_15),
],
products: [
.library(name: "SendGridKit", targets: ["SendGridKit"]),
],
dependencies: [
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.0.0"),
],
targets: [
.target(name: "SendGridKit", dependencies: [
.product(name: "AsyncHTTPClient", package: "async-http-client"),
]),
.testTarget(name: "SendGridKitTests", dependencies: [
.target(name: "SendGridKit"),
])
]
)
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# SendGridKit

![Swift](http://img.shields.io/badge/swift-5.2-brightgreen.svg)

SendGridKit is a Swift package used to communicate with the SendGrid API for Server Side Swift Apps.

## Setup
Add the dependency to Package.swift:

~~~~swift
dependencies: [
...
.package(url: "https://github.com/vapor-community/sendgrid-kit.git", from: "1.0.0")
],
targets: [
.target(name: "App", dependencies: [
.product(name: "SendGridKit", package: "sendgrid-kit"),
]),
~~~~

Register the config and the provider.

~~~~swift
let httpClient = HTTPClient(...)
let sendgridClient = SendGridClient(httpClient: httpClient, apiKey: "YOUR_API_KEY")
~~~~

## Using the API

You can use all of the available parameters here to build your `SendGridEmail`
Usage in a route closure would be as followed:

~~~~swift
import SendGrid

let email = SendGridEmail(...)
try sendGridClient.send(email, on: eventLoop)
~~~~

## Error handling
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.
Simply ensure you catch errors thrown like any other throwing function

~~~~swift
do {
try sendGridClient.send(...)
}
catch let error as SendGridError {
print(error)
}
~~~~
27 changes: 27 additions & 0 deletions Sources/SendGridKit/Models/AdvancedSuppressionManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Foundation

public struct AdvancedSuppressionManager: Encodable {
/// 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 init(groupId: Int,
groupsToDisplay: [String]? = nil) {
self.groupId = groupId
self.groupsToDisplay = groupsToDisplay
}

private enum CodingKeys: String, CodingKey {
case groupId = "group_id"
case groupsToDisplay = "groups_to_display"
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(groupId, forKey: .groupId)
try container.encode(groupsToDisplay, forKey: .groupsToDisplay)
}

}
15 changes: 15 additions & 0 deletions Sources/SendGridKit/Models/EmailAddress.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Foundation

public struct EmailAddress: Encodable {
/// format: email
public var email: String

/// The name of the person to whom you are sending an email.
public var name: String?

public init(email: String,
name: String? = nil) {
self.email = email
self.name = name
}
}
55 changes: 55 additions & 0 deletions Sources/SendGridKit/Models/EmailAttachment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// EmailAttachment.swift
// Sendgrid
//
// Created by Andrew Edwards on 3/29/18.
//

import Foundation

public struct EmailAttachment: Encodable {

/// 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 init(content: String,
type: String? = nil,
filename: String,
disposition: String? = nil,
contentId: String? = nil) {
self.content = content
self.type = type
self.filename = filename
self.disposition = disposition
self.contentId = contentId
}

private enum CodingKeys: String, CodingKey {
case content
case type
case filename
case disposition
case contentId = "content_id"
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(content, forKey: .content)
try container.encode(type, forKey: .type)
try container.encode(filename, forKey: .filename)
try container.encode(disposition, forKey: .disposition)
try container.encode(contentId, forKey: .contentId)
}
}
129 changes: 129 additions & 0 deletions Sources/SendGridKit/Models/MailSettings.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import Foundation

public struct MailSettings: Encodable {
/// 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 init(bcc: BCC? = nil,
bypassListManagement: BypassListManagement? = nil,
footer: Footer? = nil,
sandboxMode: SandboxMode? = nil,
spamCheck: SpamCheck? = nil) {
self.bcc = bcc
self.bypassListManagement = bypassListManagement
self.footer = footer
self.sandboxMode = sandboxMode
self.spamCheck = spamCheck
}

private enum CodingKeys: String, CodingKey {
case bcc
case bypassListManagement = "bypass_list_management"
case footer
case sandboxMode = "sandbox_mode"
case spamCheck = "spam_check"
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(bcc, forKey: .bcc)
try container.encode(bypassListManagement, forKey: .bypassListManagement)
try container.encode(footer, forKey: .footer)
try container.encode(sandboxMode, forKey: .sandboxMode)
try container.encode(spamCheck, forKey: .spamCheck)
}
}

public struct BCC: Encodable {
/// Indicates if this setting is enabled.
public var enable: Bool?
public var email: String?

public init(enable: Bool? = nil,
email: String? = nil) {
self.enable = enable
self.email = email
}
}

public struct BypassListManagement: Encodable {
/// Indicates if this setting is enabled.
public var enable: Bool?

public init(enable: Bool? = nil) {
self.enable = enable
}
}

public struct Footer: Encodable {
/// 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 init(enable: Bool? = nil,
text: String? = nil,
html: String? = nil) {
self.enable = enable
self.text = text
self.html = html
}
}

public struct SandboxMode: Encodable {
/// Indicates if this setting is enabled.
public var enable: Bool?

public init(enable: Bool? = nil) {
self.enable = enable
}
}

public struct SpamCheck: Encodable {
/// 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 init(enable: Bool? = nil,
threshold: Int? = nil,
postToUrl: String? = nil) {
self.enable = enable
self.threshold = threshold
self.postToUrl = postToUrl
}

private enum CodingKeys: String, CodingKey {
case enable
case threshold
case postToUrl = "post_to_url"
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(enable, forKey: .enable)
try container.encode(threshold, forKey: .threshold)
try container.encode(postToUrl, forKey: .postToUrl)
}

}
Loading

0 comments on commit 49f4b1d

Please sign in to comment.