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

Updates to better support Vapor emails #4

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
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
92 changes: 47 additions & 45 deletions Sources/SendGridKit/SendGridClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import AsyncHTTPClient
import NIOHTTP1

public struct SendGridClient {

let apiURL = "https://api.sendgrid.com/v3/mail/send"
let httpClient: HTTPClient
var eventLoop: EventLoop
let apiKey: String

private let encoder: JSONEncoder = {
Expand All @@ -21,58 +21,60 @@ public struct SendGridClient {
return decoder
}()

public init(httpClient: HTTPClient, apiKey: String) {
public init(httpClient: HTTPClient, eventLoop: EventLoop, apiKey: String) {
self.httpClient = httpClient
self.apiKey = apiKey
self.eventLoop = eventLoop
}

public func send(emails: [SendGridEmail], on eventLoop: EventLoop) throws -> EventLoopFuture<Void> {

let futures = emails.map { email -> EventLoopFuture<Void> in
do {
return try send(email: email, on: eventLoop)
} catch {
return eventLoop.makeFailedFuture(error)
}
}

public func send(emails: [SendGridEmail]) -> EventLoopFuture<Void> {
let futures = emails.map { send(email: $0) }
return EventLoopFuture<Void>.andAllSucceed(futures, on: eventLoop)
}

public func send(email: SendGridEmail, on eventLoop: EventLoop) throws -> EventLoopFuture<Void> {

var headers = HTTPHeaders()
headers.add(name: "Authorization", value: "Bearer \(apiKey)")
headers.add(name: "Content-Type", value: "application/json")

let bodyData = try encoder.encode(email)

let bodyString = String(decoding: bodyData, as: UTF8.self)

let request = try HTTPClient.Request(url: apiURL,
method: .POST,
headers: headers,
body: .string(bodyString))

return httpClient.execute(request: request,
eventLoop: .delegate(on: eventLoop))
.flatMap { response in
switch response.status {
case .ok, .accepted:
return eventLoop.makeSucceededFuture(())
default:

// JSONDecoder will handle empty body by throwing decoding error
let byteBuffer = response.body ?? ByteBuffer(.init())
let responseData = Data(byteBuffer.readableBytesView)

do {
let error = try self.decoder.decode(SendGridError.self, from: responseData)
return eventLoop.makeFailedFuture(error)
} catch {
return eventLoop.makeFailedFuture(error)
public func send(email: SendGridEmail) -> EventLoopFuture<Void> {
do {
var headers = HTTPHeaders()
headers.add(name: "Authorization", value: "Bearer \(apiKey)")
headers.add(name: "Content-Type", value: "application/json")

let bodyData = try encoder.encode(email)

let bodyString = String(decoding: bodyData, as: UTF8.self)

let request = try HTTPClient.Request(url: apiURL,
method: .POST,
headers: headers,
body: .string(bodyString))

return httpClient.execute(request: request,
eventLoop: .delegate(on: eventLoop))
.flatMap { response in
switch response.status {
case .ok, .accepted:
return self.eventLoop.makeSucceededFuture(())
default:

// JSONDecoder will handle empty body by throwing decoding error
let byteBuffer = response.body ?? ByteBuffer(.init())
let responseData = Data(byteBuffer.readableBytesView)

do {
let error = try self.decoder.decode(SendGridError.self, from: responseData)
return self.eventLoop.makeFailedFuture(error)
} catch {
return self.eventLoop.makeFailedFuture(error)
}
}
}
}
} catch {
return eventLoop.makeFailedFuture(error)
}
}

public func hopped(to eventLoop: EventLoop) -> SendGridClient {
var copy = self
copy.eventLoop = eventLoop
return copy
}
}