Skip to content

Commit b124751

Browse files
committed
SystemStatusRemote: create loadSystemStatus that returns a generic response, and refactor existing remote methods to use this.
1 parent 2eac0e1 commit b124751

File tree

1 file changed

+56
-22
lines changed

1 file changed

+56
-22
lines changed

Modules/Sources/Networking/Remote/SystemStatusRemote.swift

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ import Foundation
33
/// System Status: Remote Endpoint
44
///
55
public class SystemStatusRemote: Remote {
6+
/// Fields that can be requested in the app from the system status endpoint.
7+
/// Reference of all supported fields: https://woocommerce.github.io/woocommerce-rest-api-docs/#system-status-properties
8+
/// TODO: move raw value to private extension
9+
public enum Field: String {
10+
case activePlugins = "active_plugins"
11+
case inactivePlugins = "inactive_plugins"
12+
case environment = "environment"
13+
case settings = "settings"
14+
}
615

716
/// Retrieves information from the system status that belongs to the current site.
817
/// Currently fetching:
@@ -16,19 +25,17 @@ public class SystemStatusRemote: Remote {
1625
///
1726
public func loadSystemInformation(for siteID: Int64,
1827
completion: @escaping (Result<SystemStatus, Error>) -> Void) {
19-
let path = Constants.systemStatusPath
20-
let parameters = [
21-
ParameterKeys.fields: [ParameterValues.environment, ParameterValues.activePlugins, ParameterValues.inactivePlugins]
22-
]
23-
let request = JetpackRequest(wooApiVersion: .mark3,
24-
method: .get,
25-
siteID: siteID,
26-
path: path,
27-
parameters: parameters,
28-
availableAsRESTRequest: true)
29-
let mapper = SystemStatusMapper(siteID: siteID)
30-
31-
enqueue(request, mapper: mapper, completion: completion)
28+
Task { @MainActor in
29+
do {
30+
let mapper = SystemStatusMapper(siteID: siteID)
31+
let systemStatus = try await loadSystemStatus(for: siteID,
32+
fields: [Field.environment, Field.activePlugins, Field.inactivePlugins],
33+
mapper: mapper)
34+
completion(.success(systemStatus))
35+
} catch {
36+
completion(.failure(error))
37+
}
38+
}
3239
}
3340

3441
/// Fetch details about system status for a given site.
@@ -39,15 +46,48 @@ public class SystemStatusRemote: Remote {
3946
///
4047
public func fetchSystemStatusReport(for siteID: Int64,
4148
completion: @escaping (Result<SystemStatusReport, Error>) -> Void) {
49+
Task { @MainActor in
50+
do {
51+
let mapper = SystemStatusReportMapper(siteID: siteID)
52+
let systemStatus = try await loadSystemStatus(for: siteID,
53+
fields: nil,
54+
mapper: mapper)
55+
completion(.success(systemStatus))
56+
} catch {
57+
completion(.failure(error))
58+
}
59+
}
60+
}
61+
62+
/// Loads system status information with configurable fields for a given site.
63+
///
64+
/// - Parameters:
65+
/// - siteID: Site for which the system status is fetched from.
66+
/// - fields: Optional array of fields to fetch. If nil or empty, it fetches all available fields.
67+
/// - mapper: Mapper to transform the response data.
68+
/// - Returns: Mapped object based on the mapper output type.
69+
/// - Throws: Network or parsing errors.
70+
///
71+
public func loadSystemStatus<T, M: Mapper>(for siteID: Int64,
72+
fields: [Field]? = nil,
73+
mapper: M) async throws -> T where M.Output == T {
4274
let path = Constants.systemStatusPath
75+
let parameters: [String: Any]? = {
76+
if let fields, !fields.isEmpty {
77+
return [
78+
ParameterKeys.fields: fields.map(\.rawValue)
79+
]
80+
} else {
81+
return nil
82+
}
83+
}()
4384
let request = JetpackRequest(wooApiVersion: .mark3,
4485
method: .get,
4586
siteID: siteID,
4687
path: path,
47-
parameters: nil,
88+
parameters: parameters,
4889
availableAsRESTRequest: true)
49-
let mapper = SystemStatusReportMapper(siteID: siteID)
50-
enqueue(request, mapper: mapper, completion: completion)
90+
return try await enqueue(request, mapper: mapper)
5191
}
5292
}
5393

@@ -58,12 +98,6 @@ private extension SystemStatusRemote {
5898
static let systemStatusPath: String = "system_status"
5999
}
60100

61-
enum ParameterValues {
62-
static let activePlugins: String = "active_plugins"
63-
static let inactivePlugins: String = "inactive_plugins"
64-
static let environment: String = "environment"
65-
}
66-
67101
enum ParameterKeys {
68102
static let fields: String = "_fields"
69103
}

0 commit comments

Comments
 (0)