Skip to content

Commit

Permalink
Merge pull request #39 from MihaelIsaev/master
Browse files Browse the repository at this point in the history
Fix `IncomingMessage` model and update to Vapor4 RC with Swift 5.2
  • Loading branch information
twof authored Mar 3, 2020
2 parents 78a756c + 8853a66 commit 9795dbc
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 19 deletions.
14 changes: 8 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -15,18 +15,20 @@ 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.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "Mailgun",
dependencies: [
"Vapor"
.product(name: "Vapor", package: "vapor"),
]),
.testTarget(
name: "MailgunTests",
dependencies: ["Mailgun"]),
dependencies: [
.target(name: "Mailgun"),
]),
]
)
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

##
Expand All @@ -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
Expand All @@ -43,7 +45,7 @@ func configure(_ app: Application) throws {
/// case 2
/// manually
app.mailgun.configuration = .init(apiKey: "<api key>")
}
}
```

> Note: If your private api key begins with `key-`, be sure to include it
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -197,7 +199,7 @@ func routes(_ app: Application) throws {
text: "",
html: content
)

return req.mailgun().send(message)
}
}
Expand Down
48 changes: 44 additions & 4 deletions Sources/Mailgun/Models/IncomingMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 9795dbc

Please sign in to comment.