From 19bd887234c25a29e61663f0f990c5d88f8fb103 Mon Sep 17 00:00:00 2001 From: Jimmy McDermott Date: Fri, 4 May 2018 17:08:36 -0400 Subject: [PATCH] Update for Vapor 3 --- Package.swift | 2 +- Sources/SendGrid/Models/SendGridClient.swift | 18 +++++++----------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/Package.swift b/Package.swift index cc5ac6b..cd47045 100644 --- a/Package.swift +++ b/Package.swift @@ -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"]), diff --git a/Sources/SendGrid/Models/SendGridClient.swift b/Sources/SendGrid/Models/SendGridClient.swift index 30be12e..3971a6f 100644 --- a/Sources/SendGrid/Models/SendGridClient.swift +++ b/Sources/SendGrid/Models/SendGridClient.swift @@ -12,24 +12,20 @@ public final class SendGridClient: Service { public func send(_ emails: [SendGridEmail], on worker: Worker) throws -> Future { - 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: @@ -39,7 +35,7 @@ public final class SendGridClient: Service { throw error } - }) + } }.flatten(on: worker) } }