-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpotifyEnvironment.swift
39 lines (33 loc) · 1.15 KB
/
SpotifyEnvironment.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 Foundation
import Vapor
struct SpotifyEnvironment {
public let clientId: String
public let clientSecret: String
public let encryptionSecret: String
public let clientCallbackUrl: String
public let accountsEndpoint: String // currently "https://accounts.spotify.com"
init() throws {
self.clientId = try Environment.getOrThrow(key: "SPOTIFY_CLIENT_ID")
self.clientSecret = try Environment.getOrThrow(key: "SPOTIFY_CLIENT_SECRET")
self.encryptionSecret = try Environment.getOrThrow(key: "SPOTIFY_ENCRYPTION_SECRET")
self.clientCallbackUrl = try Environment.getOrThrow(key: "SPOTIFY_CLIENT_CALLBACK_URL")
self.accountsEndpoint = try Environment.getOrThrow(key: "SPOTIFY_ACCOUNTS_ENDPOINT")
}
var encodedBasicCredentials: String? {
return "\(clientId):\(clientSecret)".data(using: .utf8)?.base64EncodedString()
}
var tokenEndpoint: URI {
return URI(string: accountsEndpoint + "/api/token")
}
}
extension Environment {
enum EnvironmentError: Error {
case missingEnvironmentValue
}
static func getOrThrow(key: String) throws -> String {
guard let val = get(key) else {
throw EnvironmentError.missingEnvironmentValue
}
return val
}
}