|
5 | 5 | // SPDX-License-Identifier: Apache-2.0
|
6 | 6 | //
|
7 | 7 |
|
8 |
| -/// The API category provides a solution for making HTTP requests to REST and GraphQL endpoints. |
9 |
| -final public class APICategory: Category { |
10 |
| - public let categoryType = CategoryType.api |
11 |
| - |
12 |
| - var plugins = [PluginKey: APICategoryPlugin]() |
13 |
| - |
14 |
| - /// Returns the plugin added to the category, if only one plugin is added. Accessing this property if no plugins |
15 |
| - /// are added, or if more than one plugin is added, will cause a preconditionFailure. |
16 |
| - var plugin: APICategoryPlugin { |
17 |
| - guard isConfigured else { |
18 |
| - preconditionFailure( |
19 |
| - """ |
20 |
| - \(categoryType.displayName) category is not configured. Call Amplify.configure() before using \ |
21 |
| - any methods on the category. |
22 |
| - """ |
23 |
| - ) |
24 |
| - } |
25 |
| - |
26 |
| - guard !plugins.isEmpty else { |
27 |
| - preconditionFailure("No plugins added to \(categoryType.displayName) category.") |
28 |
| - } |
29 |
| - |
30 |
| - guard plugins.count == 1 else { |
31 |
| - preconditionFailure( |
32 |
| - """ |
33 |
| - More than 1 plugin added to \(categoryType.displayName) category. \ |
34 |
| - You must invoke operations on this category by getting the plugin you want, as in: |
35 |
| - #"Amplify.\(categoryType.displayName).getPlugin(for: "ThePluginKey").foo() |
36 |
| - """ |
37 |
| - ) |
38 |
| - } |
39 |
| - |
40 |
| - return plugins.first!.value |
41 |
| - } |
42 |
| - |
43 |
| - var isConfigured = false |
44 |
| - |
45 |
| - // MARK: - Plugin handling |
46 |
| - |
47 |
| - /// Adds `plugin` to the list of Plugins that implement functionality for this category. |
48 |
| - /// |
49 |
| - /// - Parameter plugin: The Plugin to add |
50 |
| - public func add(plugin: APICategoryPlugin) throws { |
51 |
| - let key = plugin.key |
52 |
| - guard !key.isEmpty else { |
53 |
| - let pluginDescription = String(describing: plugin) |
54 |
| - let error = APIError.invalidConfiguration("Plugin \(pluginDescription) has an empty `key`.", |
55 |
| - "Set the `key` property for \(String(describing: plugin))") |
56 |
| - throw error |
57 |
| - } |
58 |
| - |
59 |
| - plugins[plugin.key] = plugin |
60 |
| - } |
61 |
| - |
62 |
| - /// Returns the added plugin with the specified `key` property. |
63 |
| - /// |
64 |
| - /// - Parameter key: The PluginKey (String) of the plugin to retrieve |
65 |
| - /// - Returns: The wrapped plugin |
66 |
| - public func getPlugin(for key: PluginKey) throws -> APICategoryPlugin { |
67 |
| - guard let plugin = plugins[key] else { |
68 |
| - let keys = plugins.keys.joined(separator: ", ") |
69 |
| - let error = APIError.invalidConfiguration("No plugin has been added for '\(key)'.", |
70 |
| - "Either add a plugin for '\(key)', or use one of the known keys: \(keys)") |
71 |
| - throw error |
72 |
| - } |
73 |
| - return plugin |
74 |
| - } |
75 |
| - |
76 |
| - /// Removes the plugin registered for `key` from the list of Plugins that implement functionality for this category. |
77 |
| - /// If no plugin has been added for `key`, no action is taken, making this method safe to call multiple times. |
78 |
| - /// |
79 |
| - /// - Parameter key: The key used to `add` the plugin |
80 |
| - public func removePlugin(for key: PluginKey) { |
81 |
| - plugins.removeValue(forKey: key) |
82 |
| - } |
83 |
| - |
| 8 | +public protocol APICategory: Category, APICategoryClientBehavior { |
| 9 | + func add(plugin: APICategoryPlugin) throws |
| 10 | + func getPlugin(for key: PluginKey) throws -> APICategoryPlugin |
84 | 11 | }
|
0 commit comments