Skip to content

Commit 9795dbc

Browse files
authored
Merge pull request #39 from MihaelIsaev/master
Fix `IncomingMessage` model and update to Vapor4 RC with Swift 5.2
2 parents 78a756c + 8853a66 commit 9795dbc

File tree

3 files changed

+63
-19
lines changed

3 files changed

+63
-19
lines changed

Package.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
// swift-tools-version:5.1
1+
// swift-tools-version:5.2
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
55

66
let package = Package(
7-
name: "Mailgun",
7+
name: "VaporMailgunService",
88
platforms: [
9-
.macOS(.v10_14)
9+
.macOS(.v10_15)
1010
],
1111
products: [
1212
// Products define the executables and libraries produced by a package, and make them visible to other packages.
@@ -15,18 +15,20 @@ let package = Package(
1515
targets: ["Mailgun"]),
1616
],
1717
dependencies: [
18-
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-beta.3")
18+
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-rc")
1919
],
2020
targets: [
2121
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
2222
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
2323
.target(
2424
name: "Mailgun",
2525
dependencies: [
26-
"Vapor"
26+
.product(name: "Vapor", package: "vapor"),
2727
]),
2828
.testTarget(
2929
name: "MailgunTests",
30-
dependencies: ["Mailgun"]),
30+
dependencies: [
31+
.target(name: "Mailgun"),
32+
]),
3133
]
3234
)

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Vapor Mailgun Service
22

33
[![Discord](https://img.shields.io/badge/join-discord-745EAF.svg?style=flat)](https://vapor.team)
4-
[![Platforms](https://img.shields.io/badge/platforms-macOS%2010.14%20|%20Ubuntu%2016.04%20LTS-ff0000.svg?style=flat)](http://cocoapods.org/pods/FASwift)
5-
[![Swift 5.1](https://img.shields.io/badge/swift-5.1-orange.svg?style=flat)](http://swift.org)
4+
[![Platforms](https://img.shields.io/badge/platforms-macOS%2010.15%20|%20Ubuntu%2016.04%20LTS-ff0000.svg?style=flat)](http://cocoapods.org/pods/FASwift)
5+
[![Swift 5.2](https://img.shields.io/badge/swift-5.2-orange.svg?style=flat)](http://swift.org)
66
[![Vapor 4](https://img.shields.io/badge/vapor-4.0-blue.svg?style=flat)](https://vapor.codes)
77

88
##
@@ -15,10 +15,12 @@
1515
Vapor Mailgun Service can be installed with Swift Package Manager
1616

1717
```swift
18-
.package(url: "https://github.com/twof/VaporMailgunService.git", from: "4.0.0")
18+
.package(url: "https://github.com/twof/VaporMailgunService.git", from: "4.0.0-rc")
1919

20-
//and in targets add
21-
//"Mailgun"
20+
.target(name: "App", dependencies: [
21+
.product(name: "Vapor", package: "vapor"),
22+
.product(name: "Mailgun", package: "VaporMailgunService")
23+
])
2224
```
2325

2426
## Usage
@@ -43,7 +45,7 @@ func configure(_ app: Application) throws {
4345
/// case 2
4446
/// manually
4547
app.mailgun.configuration = .init(apiKey: "<api key>")
46-
}
48+
}
4749
```
4850

4951
> Note: If your private api key begins with `key-`, be sure to include it
@@ -87,12 +89,12 @@ import Mailgun
8789
// Called before your application initializes.
8890
func configure(_ app: Application) throws {
8991
/// configure mailgun
90-
92+
9193
/// then you're ready to use it
9294
app.mailgun(.myApp1).send(...).whenSuccess { response in
9395
print("just sent: \(response)")
9496
}
95-
}
97+
}
9698
```
9799

98100
> 💡 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 {
197199
text: "",
198200
html: content
199201
)
200-
202+
201203
return req.mailgun().send(message)
202204
}
203205
}

Sources/Mailgun/Models/IncomingMessage.swift

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Vapor
33
public struct MailgunIncomingMessage: Content {
44
public static var defaultContentType: HTTPMediaType = .formData
55

6-
public let recipients: String
6+
public let recipient: String
77
public let sender: String
88
public let from: String
99
public let subject: String
@@ -14,22 +14,62 @@ public struct MailgunIncomingMessage: Content {
1414
public let strippedHTML: String
1515
public let messageHeaders: String
1616
public let contentIdMap: String
17-
public let attachments: [Attachment]?
17+
public let attachments: [File]
1818

1919
enum CodingKeys: String, CodingKey {
20-
case recipients
20+
case recipient
2121
case sender
2222
case from
2323
case subject
2424
case bodyPlain = "body-plain"
2525
case strippedText = "stripped-text"
26-
case strippedSignature = "stripped-signiture"
26+
case strippedSignature = "stripped-signature"
2727
case bodyHTML = "body-html"
2828
case strippedHTML = "stripped-html"
2929
case messageHeaders = "message-headers"
3030
case contentIdMap = "content-id-map"
3131
case attachments
3232
}
33+
34+
struct DynamicAttachmentKey: CodingKey {
35+
var stringValue: String
36+
37+
init?(stringValue: String) {
38+
guard stringValue.hasPrefix("attachment-") else { return nil }
39+
guard let lastKey = stringValue.components(separatedBy: "-").last,
40+
let _ = Int(lastKey)
41+
else { return nil}
42+
self.stringValue = stringValue
43+
}
44+
45+
var intValue: Int?
46+
47+
init?(intValue: Int) {
48+
return nil
49+
}
50+
}
51+
52+
public init(from decoder: Decoder) throws {
53+
let container = try decoder.container(keyedBy: CodingKeys.self)
54+
recipient = try container.decode(String.self, forKey: .recipient)
55+
sender = try container.decode(String.self, forKey: .sender)
56+
from = try container.decode(String.self, forKey: .from)
57+
subject = try container.decode(String.self, forKey: .subject)
58+
bodyPlain = try container.decode(String.self, forKey: .bodyPlain)
59+
strippedText = try container.decode(String.self, forKey: .strippedText)
60+
strippedSignature = try container.decodeIfPresent(String.self, forKey: .strippedSignature)
61+
bodyHTML = try container.decode(String.self, forKey: .bodyHTML)
62+
strippedHTML = try container.decode(String.self, forKey: .strippedHTML)
63+
messageHeaders = try container.decode(String.self, forKey: .messageHeaders)
64+
contentIdMap = try container.decode(String.self, forKey: .contentIdMap)
65+
66+
var _attachments: [File] = []
67+
let attachmentsContainer = try decoder.container(keyedBy: DynamicAttachmentKey.self)
68+
try attachmentsContainer.allKeys.forEach { attachmentKey in
69+
_attachments.append(try attachmentsContainer.decode(File.self, forKey: attachmentKey))
70+
}
71+
attachments = _attachments
72+
}
3373
}
3474

3575
extension MailgunIncomingMessage {

0 commit comments

Comments
 (0)