Skip to content

Commit b862ac2

Browse files
committed
Merge branch 'develop' into feature/stripe-sdk-2
2 parents b22d3bc + 032ca5c commit b862ac2

File tree

80 files changed

+1479
-573
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+1479
-573
lines changed

Networking/Networking.xcodeproj/project.pbxproj

Lines changed: 45 additions & 20 deletions
Large diffs are not rendered by default.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Foundation
2+
3+
4+
/// Mapper: Ignore responses
5+
///
6+
struct IgnoringResponseMapper: Mapper {
7+
8+
func map(response: Data) throws -> Void {
9+
// Do nothing, accept any type of response, including null
10+
}
11+
}

Networking/Networking/Mapper/SystemPluginsMapper.swift

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import Foundation
2+
3+
/// Mapper: System Status
4+
///
5+
struct SystemStatusMapper: Mapper {
6+
7+
/// Site Identifier associated to the system plugins that will be parsed.
8+
/// We're injecting this field via `JSONDecoder.userInfo` because the remote endpoints don't return the SiteID in the system plugin endpoint.
9+
///
10+
let siteID: Int64
11+
12+
/// (Attempts) to convert a dictionary into [SystemPlugin].
13+
///
14+
func map(response: Data) throws -> [SystemPlugin] {
15+
let decoder = JSONDecoder()
16+
decoder.userInfo = [
17+
.siteID: siteID
18+
]
19+
20+
let systemStatus = try decoder.decode(SystemStatusEnvelope.self, from: response).systemStatus
21+
22+
/// For now, we're going to override the networkActivated Bool in each plugin to convey active or inactive -- in order to
23+
/// avoid a core data change to add a Bool for activated
24+
/// This will be undone in #5269
25+
let activePlugins = systemStatus.activePlugins.map {
26+
$0.overrideNetworkActivated(isNetworkActivated: true)
27+
}
28+
let inactivePlugins = systemStatus.inactivePlugins.map {
29+
$0.overrideNetworkActivated(isNetworkActivated: false)
30+
}
31+
32+
return activePlugins + inactivePlugins
33+
}
34+
}
35+
36+
/// System Status endpoint returns the requested account in the `data` key. This entity
37+
/// allows us to parse it with JSONDecoder.
38+
///
39+
private struct SystemStatusEnvelope: Decodable {
40+
let systemStatus: SystemStatus
41+
42+
private enum CodingKeys: String, CodingKey {
43+
case systemStatus = "data"
44+
}
45+
}

Networking/Networking/Model/SiteAPI.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ public struct SiteAPI: Decodable, Equatable, GeneratedFakeable {
2727
return .none
2828
}
2929

30+
/// Check if telemetry reporting namespace is available
31+
///
32+
public var telemetryIsAvailable: Bool {
33+
return namespaces.contains(WooAPIVersion.wcTelemetry.rawValue)
34+
}
35+
3036
/// Decodable Conformance.
3137
///
3238
public init(from decoder: Decoder) throws {

Networking/Networking/Model/SystemPlugin.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ public struct SystemPlugin: Decodable, GeneratedFakeable, GeneratedCopiable {
8888
}
8989
}
9090

91+
extension SystemPlugin {
92+
func overrideNetworkActivated(isNetworkActivated: Bool) -> SystemPlugin {
93+
SystemPlugin(
94+
siteID: self.siteID,
95+
plugin: self.plugin,
96+
name: self.name,
97+
version: self.version,
98+
versionLatest: self.versionLatest,
99+
url: self.url,
100+
authorName: self.authorName,
101+
authorUrl: self.authorUrl,
102+
networkActivated: isNetworkActivated
103+
)
104+
}
105+
}
106+
91107
/// Defines all of the SystemPlugin CodingKeys.
92108
///
93109
private extension SystemPlugin {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/// Represent a System Status.
2+
/// Note: We are only decoding the active and inactive plugins portions at the moment.
3+
///
4+
public struct SystemStatus: Decodable {
5+
let activePlugins: [SystemPlugin]
6+
let inactivePlugins: [SystemPlugin]
7+
8+
public init(
9+
activePlugins: [SystemPlugin],
10+
inactivePlugins: [SystemPlugin]
11+
) {
12+
self.activePlugins = activePlugins
13+
self.inactivePlugins = inactivePlugins
14+
}
15+
16+
/// The public initializer for System Status.
17+
///
18+
public init(from decoder: Decoder) throws {
19+
let container = try decoder.container(keyedBy: CodingKeys.self)
20+
let activePlugins = try container.decode([SystemPlugin].self, forKey: .activePlugins)
21+
let inactivePlugins = try container.decode([SystemPlugin].self, forKey: .inactivePlugins)
22+
23+
self.init(
24+
activePlugins: activePlugins,
25+
inactivePlugins: inactivePlugins
26+
)
27+
}
28+
}
29+
30+
private extension SystemStatus {
31+
enum CodingKeys: String, CodingKey {
32+
case activePlugins = "active_plugins"
33+
case inactivePlugins = "inactive_plugins"
34+
}
35+
}

Networking/Networking/Remote/SystemPluginsRemote.swift renamed to Networking/Networking/Remote/SystemStatusRemote.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import Foundation
22

3-
/// SystemPlugins: Remote Endpoints
3+
/// System Status: Remote Endpoint
44
///
5-
public class SystemPluginsRemote: Remote {
5+
public class SystemStatusRemote: Remote {
66

77
/// Retrieves all of the `SystemPlugin`s for a given site.
88
///
@@ -12,26 +12,27 @@ public class SystemPluginsRemote: Remote {
1212
///
1313
public func loadSystemPlugins(for siteID: Int64,
1414
completion: @escaping (Result<[SystemPlugin], Error>) -> Void) {
15-
let path = Constants.systemPluginsPath
15+
let path = Constants.systemStatusPath
1616
let parameters = [
17-
ParameterKeys.fields: ParameterValues.activeFieldValues
17+
ParameterKeys.fields: [ParameterValues.activePlugins, ParameterValues.inactivePlugins]
1818
]
1919
let request = JetpackRequest(wooApiVersion: .mark3, method: .get, siteID: siteID, path: path, parameters: parameters)
20-
let mapper = SystemPluginsMapper(siteID: siteID)
20+
let mapper = SystemStatusMapper(siteID: siteID)
2121

2222
enqueue(request, mapper: mapper, completion: completion)
2323
}
2424
}
2525

2626
// MARK: - Constants!
2727
//
28-
private extension SystemPluginsRemote {
28+
private extension SystemStatusRemote {
2929
enum Constants {
30-
static let systemPluginsPath: String = "system_status"
30+
static let systemStatusPath: String = "system_status"
3131
}
3232

3333
enum ParameterValues {
34-
static let activeFieldValues: String = "active_plugins"
34+
static let activePlugins: String = "active_plugins"
35+
static let inactivePlugins: String = "inactive_plugins"
3536
}
3637

3738
enum ParameterKeys {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Foundation
2+
3+
4+
/// Site API: Remote Endpoints
5+
///
6+
public class TelemetryRemote: Remote {
7+
8+
/// Sends data to the telemetry endpoint via the Jetpack tunnel for the provided siteID.
9+
/// Response is expected to be null.
10+
///
11+
/// - Parameters:
12+
/// - siteID: Site for which we'll fetch the API settings.
13+
/// - versionString: App version to report.
14+
/// - completion: Closure to be executed upon completion.
15+
///
16+
public func sendTelemetry(for siteID: Int64, versionString: String, completion: @escaping (Result<Void, Error>) -> Void) {
17+
let path = "tracker"
18+
let parameters = ["platform": "ios", "version": versionString]
19+
let request = JetpackRequest(wooApiVersion: .wcTelemetry, method: .post, siteID: siteID, path: path, parameters: parameters)
20+
let mapper = IgnoringResponseMapper()
21+
22+
enqueue(request, mapper: mapper, completion: completion)
23+
}
24+
}

Networking/Networking/Settings/WooAPIVersion.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public enum WooAPIVersion: String {
3838
///
3939
case addOnsV1 = "wc-product-add-ons/v1"
4040

41+
/// WooCommerce Telemetry.
42+
/// Only works on WC 5.9.0 and up.
43+
///
44+
case wcTelemetry = "wc-telemetry"
45+
4146
/// Returns the path for the current API Version
4247
///
4348
var path: String {

0 commit comments

Comments
 (0)