diff --git a/Package.swift b/Package.swift index 3c709f2..fce5165 100644 --- a/Package.swift +++ b/Package.swift @@ -1,12 +1,12 @@ -// swift-tools-version:5.1 +// swift-tools-version:5.2 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription let package = Package( - name: "Mailgun", + name: "VaporMailgunService", platforms: [ - .macOS(.v10_14) + .macOS(.v10_15) ], products: [ // Products define the executables and libraries produced by a package, and make them visible to other packages. @@ -15,7 +15,7 @@ let package = Package( targets: ["Mailgun"]), ], dependencies: [ - .package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-beta.3") + .package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-rc") ], targets: [ // Targets are the basic building blocks of a package. A target can define a module or a test suite. @@ -23,10 +23,12 @@ let package = Package( .target( name: "Mailgun", dependencies: [ - "Vapor" + .product(name: "Vapor", package: "vapor"), ]), .testTarget( name: "MailgunTests", - dependencies: ["Mailgun"]), + dependencies: [ + .target(name: "Mailgun"), + ]), ] ) diff --git a/README.md b/README.md index 809ab91..195aace 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # Vapor Mailgun Service [![Discord](https://img.shields.io/badge/join-discord-745EAF.svg?style=flat)](https://vapor.team) -[![Platforms](https://img.shields.io/badge/platforms-macOS%2010.14%20|%20Ubuntu%2016.04%20LTS-ff0000.svg?style=flat)](http://cocoapods.org/pods/FASwift) -[![Swift 5.1](https://img.shields.io/badge/swift-5.1-orange.svg?style=flat)](http://swift.org) +[![Platforms](https://img.shields.io/badge/platforms-macOS%2010.15%20|%20Ubuntu%2016.04%20LTS-ff0000.svg?style=flat)](http://cocoapods.org/pods/FASwift) +[![Swift 5.2](https://img.shields.io/badge/swift-5.2-orange.svg?style=flat)](http://swift.org) [![Vapor 4](https://img.shields.io/badge/vapor-4.0-blue.svg?style=flat)](https://vapor.codes) ## @@ -15,10 +15,12 @@ Vapor Mailgun Service can be installed with Swift Package Manager ```swift -.package(url: "https://github.com/twof/VaporMailgunService.git", from: "4.0.0") +.package(url: "https://github.com/twof/VaporMailgunService.git", from: "4.0.0-rc") -//and in targets add -//"Mailgun" +.target(name: "App", dependencies: [ + .product(name: "Vapor", package: "vapor"), + .product(name: "Mailgun", package: "VaporMailgunService") +]) ``` ## Usage @@ -43,7 +45,7 @@ func configure(_ app: Application) throws { /// case 2 /// manually app.mailgun.configuration = .init(apiKey: "") -} +} ``` > Note: If your private api key begins with `key-`, be sure to include it @@ -87,12 +89,12 @@ import Mailgun // Called before your application initializes. func configure(_ app: Application) throws { /// configure mailgun - + /// then you're ready to use it app.mailgun(.myApp1).send(...).whenSuccess { response in print("just sent: \(response)") } -} +} ``` > 💡 NOTE: All the examples below will be with `Request`, but you could do the same with `Application` as in example above. @@ -197,7 +199,7 @@ func routes(_ app: Application) throws { text: "", html: content ) - + return req.mailgun().send(message) } } diff --git a/Sources/Mailgun/Models/IncomingMessage.swift b/Sources/Mailgun/Models/IncomingMessage.swift index c6101ca..2313c49 100644 --- a/Sources/Mailgun/Models/IncomingMessage.swift +++ b/Sources/Mailgun/Models/IncomingMessage.swift @@ -3,7 +3,7 @@ import Vapor public struct MailgunIncomingMessage: Content { public static var defaultContentType: HTTPMediaType = .formData - public let recipients: String + public let recipient: String public let sender: String public let from: String public let subject: String @@ -14,22 +14,62 @@ public struct MailgunIncomingMessage: Content { public let strippedHTML: String public let messageHeaders: String public let contentIdMap: String - public let attachments: [Attachment]? + public let attachments: [File] enum CodingKeys: String, CodingKey { - case recipients + case recipient case sender case from case subject case bodyPlain = "body-plain" case strippedText = "stripped-text" - case strippedSignature = "stripped-signiture" + case strippedSignature = "stripped-signature" case bodyHTML = "body-html" case strippedHTML = "stripped-html" case messageHeaders = "message-headers" case contentIdMap = "content-id-map" case attachments } + + struct DynamicAttachmentKey: CodingKey { + var stringValue: String + + init?(stringValue: String) { + guard stringValue.hasPrefix("attachment-") else { return nil } + guard let lastKey = stringValue.components(separatedBy: "-").last, + let _ = Int(lastKey) + else { return nil} + self.stringValue = stringValue + } + + var intValue: Int? + + init?(intValue: Int) { + return nil + } + } + + public init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + recipient = try container.decode(String.self, forKey: .recipient) + sender = try container.decode(String.self, forKey: .sender) + from = try container.decode(String.self, forKey: .from) + subject = try container.decode(String.self, forKey: .subject) + bodyPlain = try container.decode(String.self, forKey: .bodyPlain) + strippedText = try container.decode(String.self, forKey: .strippedText) + strippedSignature = try container.decodeIfPresent(String.self, forKey: .strippedSignature) + bodyHTML = try container.decode(String.self, forKey: .bodyHTML) + strippedHTML = try container.decode(String.self, forKey: .strippedHTML) + messageHeaders = try container.decode(String.self, forKey: .messageHeaders) + contentIdMap = try container.decode(String.self, forKey: .contentIdMap) + + var _attachments: [File] = [] + let attachmentsContainer = try decoder.container(keyedBy: DynamicAttachmentKey.self) + try attachmentsContainer.allKeys.forEach { attachmentKey in + _attachments.append(try attachmentsContainer.decode(File.self, forKey: attachmentKey)) + } + attachments = _attachments + } } extension MailgunIncomingMessage {