/
Application+SendGrid.swift
40 lines (30 loc) · 1.07 KB
/
Application+SendGrid.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import Vapor
import SendGridKit
extension Application {
public struct Sendgrid {
private final class Storage {
let apiKey: String
init(apiKey: String) {
self.apiKey = apiKey
}
}
private struct Key: StorageKey {
typealias Value = Storage
}
private var storage: Storage {
if self.application.storage[Key.self] == nil {
self.initialize()
}
return self.application.storage[Key.self]!
}
public func initialize() {
guard let apiKey = Environment.process.SENDGRID_API_KEY else {
fatalError("No sendgrid API key provided")
}
self.application.storage[Key.self] = .init(apiKey: apiKey)
}
fileprivate let application: Application
public var client: SendGridClient { .init(httpClient: self.application.client.http, apiKey: self.storage.apiKey) }
}
public var sendgrid: Sendgrid { .init(application: self) }
}