Skip to content

Commit

Permalink
Merge pull request #35 from vapor-community/vapor-4-provider
Browse files Browse the repository at this point in the history
Update for Vapor 4,  implementation moved to `sendgrid-kit`.
  • Loading branch information
gwynne committed Mar 16, 2020
2 parents 4fca9d0 + c66cb77 commit e32f5a1
Show file tree
Hide file tree
Showing 18 changed files with 110 additions and 667 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/test.yml
@@ -0,0 +1,26 @@
name: test
on:
- pull_request
jobs:
#sendgrid_macos:
# runs-on: macos-latest
# env:
# DEVELOPER_DIR: /Applications/Xcode_11.4_beta.app/Contents/Developer
# steps:
# - uses: actions/checkout@v2
# - run: brew install vapor/tap/vapor-beta
# - run: xcrun swift test --enable-test-discovery --sanitize=thread
sendgrid_xenial:
container:
image: vapor/swift:5.2-xenial
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: swift test --enable-test-discovery --sanitize=thread
sendgrid_bionic:
container:
image: vapor/swift:5.2-bionic
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: swift test --enable-test-discovery --sanitize=thread
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@
/*.xcodeproj
Package.pins
Package.resolved
/.swiftpm
17 changes: 12 additions & 5 deletions Package.swift
@@ -1,16 +1,23 @@
// swift-tools-version:4.0
// swift-tools-version:5.2
import PackageDescription

let package = Package(
name: "SendGrid",
platforms: [
.macOS(.v10_15)
],
products: [
.library(name: "SendGrid", targets: ["SendGrid"])
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0"),
],
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-rc"),
.package(url: "https://github.com/vapor-community/sendgrid-kit.git", from: "1.0.0"),
],
targets: [
.target(name: "SendGrid", dependencies: ["Vapor"]),
.testTarget(name: "SendGridTests", dependencies: ["Vapor", "SendGrid"])
.target(name: "SendGrid", dependencies: [
.product(name: "Vapor", package: "vapor"),
.product(name: "SendGridKit", package: "sendgrid-kit"),
]),
.testTarget(name: "SendGridTests", dependencies: ["SendGrid"])
]
)
51 changes: 30 additions & 21 deletions README.md
@@ -1,8 +1,13 @@
# SendGrid Provider for Vapor

![Swift](http://img.shields.io/badge/swift-4.1-brightgreen.svg)
![Vapor](http://img.shields.io/badge/vapor-3.0-brightgreen.svg)
[![CircleCI](https://circleci.com/gh/vapor-community/sendgrid-provider.svg?style=shield)](https://circleci.com/gh/vapor-community/sendgrid-provider)
<p align="center">
<a href="https://github.com/vapor-community/sendgrid/actions">
<img src="https://github.com/vapor-community/sendgrid/workflows/test/badge.svg" alt="Continuous Integration">
</a>
<a href="https://swift.org">
<img src="http://img.shields.io/badge/swift-5.2-brightgreen.svg" alt="Swift 5.2">
</a>
</p>

Adds a mail backend for SendGrid to the Vapor web framework. Send simple emails,
or leverage the full capabilities of SendGrid's V3 API.
Expand All @@ -11,20 +16,24 @@ or leverage the full capabilities of SendGrid's V3 API.
Add the dependency to Package.swift:

~~~~swift
.package(url: "https://github.com/vapor-community/sendgrid-provider.git", from: "3.0.0")
.package(url: "https://github.com/vapor-community/sendgrid.git", from: "4.0.0")
~~~~

Register the config and the provider.
~~~~swift
let config = SendGridConfig(apiKey: "SG.something")

services.register(config)
Make sure `SENDGRID_API_KEY` is set in your environment. This can be set in the
Xcode scheme, or specified in your `docker-compose.yml`, or even provided as
part of a `swift run` command.

try services.register(SendGridProvider())
Optionally, explicitly initialize the provider (this is strongly recommended, as
otherwise a missing API key will cause a fatal error some time later in your
application):

app = try Application(services: services)
~~~~swift
app.sendgrid.initialize()
~~~~

sendGridClient = try app.make(SendGridClient.self)
Now you can access the client at any time:
~~~~swift
app.sendgrid.client
~~~~

## Using the API
Expand All @@ -36,20 +45,20 @@ Usage in a route closure would be as followed:
import SendGrid

let email = SendGridEmail()
let sendGridClient = try req.make(SendGridClient.self)

try sendGridClient.send([email], on: req.eventLoop)
return req.application.sendgrid.client.send([email], on: req.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
If the request to the API failed for any reason a `SendGridError` is the result
of the future, and has an `errors` property that contains an array of errors
returned by the API:

~~~~swift
do {
try sendgridClient.send(...)
}
catch let error as SendGridError {
print(error)
return req.application.sendgrid.client.send([email], on: req.eventLoop).flatMapError { error in
if let sendgridError = error as? SendGridError {
req.logger.error("\(error)")
}
// ...
}
~~~~
17 changes: 0 additions & 17 deletions Sourcery/LinuxMain.stencil

This file was deleted.

40 changes: 40 additions & 0 deletions Sources/SendGrid/Application+SendGrid.swift
@@ -0,0 +1,40 @@
import Vapor
import SendGridKit

extension Application {
public struct Sendgrid {
private final class Storage {
let apiKey: String

init(apiKey: String) {
self.apiKey = apiKey
}
}

private struct Key: StorageKey {
typealias Value = Storage
}

private var storage: Storage {
if self.application.storage[Key.self] == nil {
self.initialize()
}
return self.application.storage[Key.self]!
}

public func initialize() {
guard let apiKey = Environment.process.SENDGRID_API_KEY else {
fatalError("No sendgrid API key provided")
}

self.application.storage[Key.self] = .init(apiKey: apiKey)
}

fileprivate let application: Application

public var client: SendGridClient { .init(httpClient: self.application.client.http, apiKey: self.storage.apiKey) }
}

public var sendgrid: Sendgrid { .init(application: self) }
}

1 change: 1 addition & 0 deletions Sources/SendGrid/Exports.swift
@@ -0,0 +1 @@
@_exported import SendGridKit
20 changes: 0 additions & 20 deletions Sources/SendGrid/Models/AdvancedSuppressionManager.swift

This file was deleted.

22 changes: 0 additions & 22 deletions Sources/SendGrid/Models/EmailAddress.swift

This file was deleted.

46 changes: 0 additions & 46 deletions Sources/SendGrid/Models/EmailAttachment.swift

This file was deleted.

0 comments on commit e32f5a1

Please sign in to comment.