Skip to content

Commit

Permalink
Merge pull request #31 from madsodgaard/feature/eu-support
Browse files Browse the repository at this point in the history
Add support for region selection
  • Loading branch information
twof authored Jun 19, 2019
2 parents 4270d8e + 267e19f commit f9a86d6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Make sure you get an API key and register a custom domain
In `configure.swift`:

```swift
let mailgun = Mailgun(apiKey: "<api key>", domain: "mg.example.com")
let mailgun = Mailgun(apiKey: "<api key>", domain: "mg.example.com", region: .eu)
services.register(mailgun, as: Mailgun.self)
```

Expand Down
20 changes: 17 additions & 3 deletions Sources/Mailgun/Mailgun.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Foundation
public protocol MailgunProvider: Service {
var apiKey: String { get }
var domain: String { get }
var region: Mailgun.Region { get }
func send(_ content: Mailgun.Message, on container: Container) throws -> Future<Response>
func setup(forwarding: RouteSetup, with container: Container) throws -> Future<Response>
}
Expand All @@ -15,6 +16,12 @@ public protocol MailgunProvider: Service {

public struct Mailgun: MailgunProvider {

/// Describes a region: US or EU
public enum Region {
case us
case eu
}

public enum Error: Debuggable {

/// Encoding problem
Expand Down Expand Up @@ -72,6 +79,9 @@ public struct Mailgun: MailgunProvider {
/// Domain
public let domain: String

/// Region
public let region: Mailgun.Region

// MARK: Initialization


Expand All @@ -80,9 +90,10 @@ public struct Mailgun: MailgunProvider {
/// - Parameters:
/// - apiKey: API key including "key-" prefix
/// - domain: API domain
public init(apiKey: String, domain: String) {
public init(apiKey: String, domain: String, region: Mailgun.Region) {
self.apiKey = apiKey
self.domain = domain
self.region = region
}

// MARK: Send message
Expand All @@ -99,7 +110,7 @@ public struct Mailgun: MailgunProvider {
var headers = HTTPHeaders([])
headers.add(name: HTTPHeaderName.authorization, value: "Basic \(authKeyEncoded)")

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

let client = try container.make(Client.self)

Expand All @@ -123,7 +134,7 @@ public struct Mailgun: MailgunProvider {
var headers = HTTPHeaders([])
headers.add(name: HTTPHeaderName.authorization, value: "Basic \(authKeyEncoded)")

let mailgunURL = "https://api.mailgun.net/v3/routes"
let mailgunURL = "\(baseApiUrl)/v3/routes"

let client = try container.make(Client.self)

Expand All @@ -138,6 +149,9 @@ public struct Mailgun: MailgunProvider {
// MARK: Private

fileprivate extension Mailgun {
private var baseApiUrl: String {
return region == .eu ? "https://api.eu.mailgun.net/v3" : "https://api.mailgun.net/v3"
}

func encode(apiKey: String) throws -> String {
guard let apiKeyData = "api:\(apiKey)".data(using: .utf8) else {
Expand Down

0 comments on commit f9a86d6

Please sign in to comment.