-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathAuthCategory.swift
101 lines (86 loc) · 3.64 KB
/
AuthCategory.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
final public class AuthCategory: Category {
public let categoryType = CategoryType.auth
var plugins = [PluginKey: AuthCategoryPlugin]()
/// Returns the plugin added to the category, if only one plugin is added. Accessing this property if no plugins
/// are added, or if more than one plugin is added, will cause a preconditionFailure.
var plugin: AuthCategoryPlugin {
guard isConfigured else {
return Fatal.preconditionFailure(
"""
\(categoryType.displayName) category is not configured. Call Amplify.configure() before using \
any methods on the category.
"""
)
}
guard !plugins.isEmpty else {
return Fatal.preconditionFailure("No plugins added to \(categoryType.displayName) category.")
}
guard plugins.count == 1 else {
return Fatal.preconditionFailure(
"""
More than 1 plugin added to \(categoryType.displayName) category. \
You must invoke operations on this category by getting the plugin you want, as in:
#"Amplify.\(categoryType.displayName).getPlugin(for: "ThePluginKey").foo()
"""
)
}
return plugins.first!.value
}
var isConfigured = false
// MARK: - Plugin handling
/// Adds `plugin` to the list of Plugins that implement functionality for this category.
///
/// - Parameter plugin: The Plugin to add
public func add(plugin: AuthCategoryPlugin) throws {
let key = plugin.key
guard !key.isEmpty else {
let pluginDescription = String(describing: plugin)
let error = AuthError.configuration("Plugin \(pluginDescription) has an empty `key`.",
"Set the `key` property for \(String(describing: plugin))")
throw error
}
guard !isConfigured else {
let pluginDescription = String(describing: plugin)
let error = ConfigurationError.amplifyAlreadyConfigured(
"\(pluginDescription) cannot be added after `Amplify.configure()`.",
"Do not add plugins after calling `Amplify.configure()`."
)
throw error
}
plugins[plugin.key] = plugin
}
/// Returns the added plugin with the specified `key` property.
///
/// - Parameter key: The PluginKey (String) of the plugin to retrieve
/// - Returns: The wrapped plugin
public func getPlugin(for key: PluginKey) throws -> AuthCategoryPlugin {
guard let plugin = plugins[key] else {
let keys = plugins.keys.joined(separator: ", ")
let error = AuthError.configuration("No plugin has been added for '\(key)'.",
"Either add a plugin for '\(key)', or use one of the known keys: \(keys)")
throw error
}
return plugin
}
/// Removes the plugin registered for `key` from the list of Plugins that implement functionality for this category.
/// If no plugin has been added for `key`, no action is taken, making this method safe to call multiple times.
///
/// - Parameter key: The key used to `add` the plugin
public func removePlugin(for key: PluginKey) {
plugins.removeValue(forKey: key)
}
}
extension AuthCategory: DefaultLogger {
public static var log: Logger {
Amplify.Logging.logger(forCategory: CategoryType.auth.displayName, forNamespace: String(describing: self))
}
public var log: Logger {
Self.log
}
}