Skip to content

Commit

Permalink
Merge pull request #24 from mcdappdev/master
Browse files Browse the repository at this point in the history
Vapor 3 Update
  • Loading branch information
Andrewangeta authored May 7, 2018
2 parents f822859 + 19bd887 commit 45e61ff
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let package = Package(
.library(name: "SendGrid", targets: ["SendGrid"])
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0-rc"),
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0"),
],
targets: [
.target(name: "SendGrid", dependencies: ["Vapor"]),
Expand Down
18 changes: 7 additions & 11 deletions Sources/SendGrid/Models/SendGridClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,20 @@ public final class SendGridClient: Service {

public func send(_ emails: [SendGridEmail], on worker: Worker) throws -> Future<Void> {

return try emails.map { (email) in
return 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 request = httpClient.post(apiEndpoint, headers: headers, beforeSend: { req in
try req.content.encode(json: email, using: encoder)
})

let data = try encoder.encode(email)

let body = HTTPBody(data: data)

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

return request.map { response in
switch response.http.status {
case .ok, .accepted: return
default:
Expand All @@ -39,7 +35,7 @@ public final class SendGridClient: Service {

throw error
}
})
}
}.flatten(on: worker)
}
}

0 comments on commit 45e61ff

Please sign in to comment.