-
Notifications
You must be signed in to change notification settings - Fork 198
/
Amplify.swift
121 lines (104 loc) · 4.47 KB
/
Amplify.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
import Foundation
/// At its core, the Amplify class is simply a router that provides clients top-level access to categories and
/// configuration methods. It provides convenient access to default plugins via the top-level category properties,
/// but clients can access specific plugins by invoking `getPlugin` on a category and issuing methods directly to
/// that plugin.
///
/// - Warning: It is a serious error to invoke any of the category APIs (like `Analytics.record()` or
/// `API.mutate()`) without first registering plugins via `Amplify.add(plugin:)` and configuring Amplify via
/// `Amplify.configure()`. Such access will cause a preconditionFailure.
///
/// There are two exceptions to this. The `Logging` and `Hub` categories are configured with a default plugin that is
/// available at initialization.
///
/// - Tag: Amplify
public class Amplify {
/// If `true`, `configure()` has already been invoked, and subsequent calls to `configure` will throw a
/// ConfigurationError.amplifyAlreadyConfigured error.
///
/// - Tag: Amplify.isConfigured
static var isConfigured = false
// Storage for the categories themselves, which will be instantiated during configuration, and cleared during reset.
// It is not supported to mutate these category properties. They are `var` to support the `reset()` method for
// ease of testing.
/// - Tag: Amplify.Analytics
public static internal(set) var Analytics = AnalyticsCategory()
/// - Tag: Amplify.API
public static internal(set) var API: APICategory = APICategory()
/// - Tag: Amplify.Auth
public static internal(set) var Auth = AuthCategory()
/// - Tag: Amplify.DataStore
public static internal(set) var DataStore = DataStoreCategory()
/// - Tag: Amplify.Geo
public static internal(set) var Geo = GeoCategory()
/// - Tag: Amplify.Hub
public static internal(set) var Hub = HubCategory()
/// - Tag: Amplify.Notifications
public static internal(set) var Notifications = NotificationsCategory()
/// - Tag: Amplify.Predictions
public static internal(set) var Predictions = PredictionsCategory()
/// - Tag: Amplify.Storage
public static internal(set) var Storage = StorageCategory()
/// Special case category. We protect this with an AtomicValue because it is used by reset()
/// methods during setup & teardown of tests
///
/// - Tag: Amplify.Logging
public static internal(set) var Logging: LoggingCategory {
get {
loggingAtomic.get()
}
set {
loggingAtomic.set(newValue)
}
}
private static let loggingAtomic = AtomicValue<LoggingCategory>(initialValue: LoggingCategory())
/// Adds `plugin` to the category
///
/// See: [Category.removePlugin(for:)](x-source-tag://Category.removePlugin)
///
/// - Parameter plugin: The plugin to add
/// - Tag: Amplify.add_plugin
public static func add<P: Plugin>(plugin: P) throws {
log.debug("Adding plugin: \(plugin))")
switch plugin {
case let plugin as AnalyticsCategoryPlugin:
try Analytics.add(plugin: plugin)
case let plugin as APICategoryPlugin:
try API.add(plugin: plugin)
case let plugin as AuthCategoryPlugin:
try Auth.add(plugin: plugin)
case let plugin as DataStoreCategoryPlugin:
try DataStore.add(plugin: plugin)
case let plugin as GeoCategoryPlugin:
try Geo.add(plugin: plugin)
case let plugin as HubCategoryPlugin:
try Hub.add(plugin: plugin)
case let plugin as LoggingCategoryPlugin:
try Logging.add(plugin: plugin)
case let plugin as PredictionsCategoryPlugin:
try Predictions.add(plugin: plugin)
case let plugin as PushNotificationsCategoryPlugin:
try Notifications.Push.add(plugin: plugin)
case let plugin as StorageCategoryPlugin:
try Storage.add(plugin: plugin)
default:
throw PluginError.pluginConfigurationError(
"Plugin category does not exist.",
"Verify that the library version is correct and supports the plugin's category.")
}
}
}
extension Amplify: DefaultLogger {
public static var log: Logger {
Amplify.Logging.logger(forCategory: String(describing: self))
}
public var log: Logger {
Self.log
}
}