Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
twof committed Mar 10, 2018
0 parents commit 273c9c1
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
124 changes: 124 additions & 0 deletions Package.resolved
@@ -0,0 +1,124 @@
{
"object": {
"pins": [
{
"package": "Async",
"repositoryURL": "https://github.com/vapor/async.git",
"state": {
"branch": null,
"revision": "76ed7841b32dcc47168e8d9040ba03c024224d0c",
"version": "1.0.0-rc.1.1"
}
},
{
"package": "Console",
"repositoryURL": "https://github.com/vapor/console.git",
"state": {
"branch": null,
"revision": "26812edd1d4af8129d194ee25483159b92fed4d5",
"version": "3.0.0-rc.1"
}
},
{
"package": "Core",
"repositoryURL": "https://github.com/vapor/core.git",
"state": {
"branch": null,
"revision": "0dd54c6e369f575a13f2c9964609b6c7d2dab909",
"version": "3.0.0-rc.1.0.1"
}
},
{
"package": "Crypto",
"repositoryURL": "https://github.com/vapor/crypto.git",
"state": {
"branch": null,
"revision": "e5dbff5eac700fe925f95a4e6eee5424f609d0d6",
"version": "3.0.0-rc.1.1.1"
}
},
{
"package": "DatabaseKit",
"repositoryURL": "https://github.com/vapor/database-kit.git",
"state": {
"branch": null,
"revision": "5f247a63f392d260cc7c8e9b916131a67f73dbe4",
"version": "1.0.0-rc.1"
}
},
{
"package": "Engine",
"repositoryURL": "https://github.com/vapor/engine.git",
"state": {
"branch": null,
"revision": "e12531d2b37895d21cd8b8d13ad930fa85217e9e",
"version": "3.0.0-rc.1.0.1"
}
},
{
"package": "Routing",
"repositoryURL": "https://github.com/vapor/routing.git",
"state": {
"branch": null,
"revision": "8210d28691a11d0d27b7a0b32117d7fd81099916",
"version": "3.0.0-rc.1"
}
},
{
"package": "Service",
"repositoryURL": "https://github.com/vapor/service.git",
"state": {
"branch": null,
"revision": "90426d62cf1b028c0eef8285cb0293b527e45393",
"version": "1.0.0-rc.1"
}
},
{
"package": "Sockets",
"repositoryURL": "https://github.com/vapor/sockets.git",
"state": {
"branch": null,
"revision": "738f3ebf3ebb8ea886d5dd8588dd46a54f66af1c",
"version": "3.0.0-rc.1"
}
},
{
"package": "TemplateKit",
"repositoryURL": "https://github.com/vapor/template-kit.git",
"state": {
"branch": null,
"revision": "cda3174c5e029526a5b11b19fcfd83bd65b6876a",
"version": "1.0.0-rc.1.1"
}
},
{
"package": "TLS",
"repositoryURL": "https://github.com/vapor/tls.git",
"state": {
"branch": null,
"revision": "cfd8cc0f06841ee15469c5fc98131e47511f8e94",
"version": "3.0.0-rc.1.0.1"
}
},
{
"package": "Validation",
"repositoryURL": "https://github.com/vapor/validation.git",
"state": {
"branch": null,
"revision": "683af11976a732584baf339483aa3206734c913b",
"version": "2.0.0-rc.1"
}
},
{
"package": "Vapor",
"repositoryURL": "https://github.com/vapor/vapor.git",
"state": {
"branch": null,
"revision": "d8254ba464b211ca6d465703f7f5d37eb0c95b65",
"version": "3.0.0-rc.1.1"
}
}
]
},
"version": 1
}
29 changes: 29 additions & 0 deletions Package.swift
@@ -0,0 +1,29 @@
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Mailgun",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "Mailgun",
targets: ["Mailgun"]),
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0-beta"),
],
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"
]),
.testTarget(
name: "MailgunTests",
dependencies: ["Mailgun"]),
]
)
3 changes: 3 additions & 0 deletions README.md
@@ -0,0 +1,3 @@
# Mailgun

A description of this package.
49 changes: 49 additions & 0 deletions Sources/Mailgun/Mailgun.swift
@@ -0,0 +1,49 @@
import Vapor
import Crypto

public struct MailgunFormData: Content {
public static let defaultMediaType: MediaType = MediaType.urlEncodedForm

let from: String
let to: String
let subject: String
let text: String
}

public protocol Mailgun: class, Service {
var apiKey: String { get }
var customURL: String { get }
func sendMail(data content: MailgunFormData, on req: Request) throws -> Future<Response>
}

public class MailgunEngine: Mailgun {
public var apiKey: String
public var customURL: String

init(apiKey: String, customURL: String) {
self.apiKey = apiKey
self.customURL = customURL
}

public func sendMail(data content: MailgunFormData, on req: Request) throws -> Future<Response> {
let client = try req.make(EngineClient.self)
let encode: (String) -> String = Base64Encoder(encoding: .base64).encode
let authKey = encode("api:key-\(self.apiKey)")

let headers = HTTPHeaders(dictionaryLiteral:
(HTTPHeaderName.authorization, "Basic \(authKey)"),
(HTTPHeaderName.contentType, "application/x-www-form-urlencoded")
)

let mailgunURL = "https://api.mailgun.net/v3/\(self.customURL)/messages"


return client
.post(mailgunURL, headers: headers, content: content)
.map(to: Response.self) { (response) in
return response
}.catch { (err) in
print(err)
}
}
}
7 changes: 7 additions & 0 deletions Tests/LinuxMain.swift
@@ -0,0 +1,7 @@
import XCTest

import MailgunTests

var tests = [XCTestCaseEntry]()
tests += MailgunTests.allTests()
XCTMain(tests)
16 changes: 16 additions & 0 deletions Tests/MailgunTests/MailgunTests.swift
@@ -0,0 +1,16 @@
import XCTest
@testable import Mailgun

final class MailgunTests: XCTestCase {
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct
// results.
XCTAssertEqual(Mailgun().text, "Hello, World!")
}


static var allTests = [
("testExample", testExample),
]
}
9 changes: 9 additions & 0 deletions Tests/MailgunTests/XCTestManifests.swift
@@ -0,0 +1,9 @@
import XCTest

#if !os(macOS)
public func allTests() -> [XCTestCaseEntry] {
return [
testCase(MailgunTests.allTests),
]
}
#endif

0 comments on commit 273c9c1

Please sign in to comment.