Skip to content

Commit eeccb43

Browse files
authored
[Local catalog] Use cellular data setting for full syncs (#16320)
2 parents 3b04ef0 + 4b644ab commit eeccb43

File tree

11 files changed

+441
-81
lines changed

11 files changed

+441
-81
lines changed

Modules/Sources/Networking/Remote/POSCatalogSyncRemote.swift

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,39 @@ public protocol POSCatalogSyncRemoteProtocol {
3030
/// - Parameters:
3131
/// - siteID: Site ID to generate catalog for.
3232
/// - forceGeneration: Whether to always generate a catalog.
33+
/// - allowCellular: Should cellular data be used if required.
3334
/// - Returns: Catalog job response with job ID.
3435
///
3536
// periphery:ignore - TODO - remove this periphery ignore comment when this endpoint is integrated with catalog sync
36-
func requestCatalogGeneration(for siteID: Int64, forceGeneration: Bool) async throws -> POSCatalogRequestResponse
37+
func requestCatalogGeneration(for siteID: Int64, forceGeneration: Bool, allowCellular: Bool) async throws -> POSCatalogRequestResponse
3738

3839
/// Downloads the generated catalog at the specified download URL.
3940
/// - Parameters:
4041
/// - siteID: Site ID to download catalog for.
4142
/// - downloadURL: Download URL of the catalog file.
43+
/// - allowCellular: Should cellular data be used if required.
4244
/// - Returns: List of products and variations in the POS catalog.
43-
// periphery:ignore - TODO - remove this periphery ignore comment when this endpoint is integrated with catalog sync
44-
func downloadCatalog(for siteID: Int64, downloadURL: String) async throws -> POSCatalogResponse
45+
func downloadCatalog(for siteID: Int64,
46+
downloadURL: String,
47+
allowCellular: Bool) async throws -> POSCatalogResponse
4548

4649
/// Loads POS products for full sync.
4750
///
4851
/// - Parameters:
4952
/// - siteID: Site ID to load products from.
5053
/// - pageNumber: Page number for pagination.
54+
/// - allowCellular: Should cellular data be used if required.
5155
/// - Returns: Paginated list of POS products.
52-
func loadProducts(siteID: Int64, pageNumber: Int) async throws -> PagedItems<POSProduct>
56+
func loadProducts(siteID: Int64, pageNumber: Int, allowCellular: Bool) async throws -> PagedItems<POSProduct>
5357

5458
/// Loads POS product variations for full sync.
5559
///
5660
/// - Parameters:
5761
/// - siteID: Site ID to load variations from.
5862
/// - pageNumber: Page number for pagination.
63+
/// - allowCellular: Should cellular data be used if required.
5964
/// - Returns: Paginated list of POS product variations.
60-
func loadProductVariations(siteID: Int64, pageNumber: Int) async throws -> PagedItems<POSProductVariation>
65+
func loadProductVariations(siteID: Int64, pageNumber: Int, allowCellular: Bool) async throws -> PagedItems<POSProductVariation>
6166

6267
/// Gets the total count of products for the specified site.
6368
///
@@ -87,7 +92,6 @@ public class POSCatalogSyncRemote: Remote, POSCatalogSyncRemoteProtocol {
8792
/// - pageNumber: Page number for pagination.
8893
/// - Returns: Paginated list of POS products.
8994
///
90-
// periphery:ignore - TODO - remove this periphery ignore comment when this endpoint is integrated with catalog sync
9195
public func loadProducts(modifiedAfter: Date, siteID: Int64, pageNumber: Int)
9296
async throws -> PagedItems<POSProduct> {
9397
let path = Path.products
@@ -120,7 +124,6 @@ public class POSCatalogSyncRemote: Remote, POSCatalogSyncRemoteProtocol {
120124
/// - pageNumber: Page number for pagination.
121125
/// - Returns: Paginated list of POS product variations.
122126
///
123-
// periphery:ignore - TODO - remove this periphery ignore comment when this endpoint is integrated with catalog sync
124127
public func loadProductVariations(modifiedAfter: Date, siteID: Int64, pageNumber: Int) async throws -> PagedItems<POSProductVariation> {
125128
let path = Path.variations
126129
let parameters = [
@@ -151,10 +154,11 @@ public class POSCatalogSyncRemote: Remote, POSCatalogSyncRemoteProtocol {
151154
///
152155
/// - Parameters:
153156
/// - siteID: Site ID to generate catalog for.
157+
/// - forceGeneration: Whether to always generate a catalog.
158+
/// - allowCellular: Should cellular data be used if required.
154159
/// - Returns: Catalog job response with job ID.
155160
///
156-
// periphery:ignore - TODO - remove this periphery ignore comment when this endpoint is integrated with catalog sync
157-
public func requestCatalogGeneration(for siteID: Int64, forceGeneration: Bool) async throws -> POSCatalogRequestResponse {
161+
public func requestCatalogGeneration(for siteID: Int64, forceGeneration: Bool, allowCellular: Bool) async throws -> POSCatalogRequestResponse {
158162
let path = "products/catalog"
159163
let parameters: [String: Any] = [
160164
ParameterKey.fullSyncFields: POSProduct.requestFields,
@@ -166,7 +170,8 @@ public class POSCatalogSyncRemote: Remote, POSCatalogSyncRemoteProtocol {
166170
siteID: siteID,
167171
path: path,
168172
parameters: parameters,
169-
availableAsRESTRequest: true
173+
availableAsRESTRequest: true,
174+
allowsCellularAccess: allowCellular
170175
)
171176
let mapper = SingleItemMapper<POSCatalogRequestResponse>(siteID: siteID)
172177
return try await enqueue(request, mapper: mapper)
@@ -176,14 +181,17 @@ public class POSCatalogSyncRemote: Remote, POSCatalogSyncRemoteProtocol {
176181
/// - Parameters:
177182
/// - siteID: Site ID to download catalog for.
178183
/// - downloadURL: Download URL of the catalog file.
184+
/// - allowCellular: Should cellular data be used if required.
179185
/// - Returns: List of products and variations in the POS catalog.
180-
// periphery:ignore - TODO - remove this periphery ignore comment when this endpoint is integrated with catalog sync
181-
public func downloadCatalog(for siteID: Int64, downloadURL: String) async throws -> POSCatalogResponse {
186+
public func downloadCatalog(for siteID: Int64,
187+
downloadURL: String,
188+
allowCellular: Bool) async throws -> POSCatalogResponse {
182189
// TODO: WOOMOB-1173 - move download task to the background using `URLSessionConfiguration.background`
183190
guard let url = URL(string: downloadURL) else {
184191
throw NetworkError.invalidURL
185192
}
186-
let request = URLRequest(url: url)
193+
var request = URLRequest(url: url)
194+
request.allowsCellularAccess = allowCellular
187195
let mapper = ListMapper<POSProduct>(siteID: siteID)
188196
let items = try await enqueue(request, mapper: mapper)
189197
let variationProductTypeKey = "variation"
@@ -198,10 +206,10 @@ public class POSCatalogSyncRemote: Remote, POSCatalogSyncRemoteProtocol {
198206
/// - Parameters:
199207
/// - siteID: Site ID to load products from.
200208
/// - pageNumber: Page number for pagination.
209+
/// - allowCellular: Should cellular data be used if required.
201210
/// - Returns: Paginated list of POS products.
202211
///
203-
// periphery:ignore - TODO - remove this periphery ignore comment when this endpoint is integrated with catalog sync
204-
public func loadProducts(siteID: Int64, pageNumber: Int) async throws -> PagedItems<POSProduct> {
212+
public func loadProducts(siteID: Int64, pageNumber: Int, allowCellular: Bool) async throws -> PagedItems<POSProduct> {
205213
let path = Path.products
206214
let parameters = [
207215
ParameterKey.page: String(pageNumber),
@@ -215,7 +223,8 @@ public class POSCatalogSyncRemote: Remote, POSCatalogSyncRemoteProtocol {
215223
siteID: siteID,
216224
path: path,
217225
parameters: parameters,
218-
availableAsRESTRequest: true
226+
availableAsRESTRequest: true,
227+
allowsCellularAccess: allowCellular
219228
)
220229
let mapper = ListMapper<POSProduct>(siteID: siteID)
221230
let (products, responseHeaders) = try await enqueueWithResponseHeaders(request, mapper: mapper)
@@ -228,10 +237,10 @@ public class POSCatalogSyncRemote: Remote, POSCatalogSyncRemoteProtocol {
228237
/// - Parameters:
229238
/// - siteID: Site ID to load variations from.
230239
/// - pageNumber: Page number for pagination.
240+
/// - allowCellular: Should cellular data be used if required.
231241
/// - Returns: Paginated list of POS product variations.
232242
///
233-
// periphery:ignore - TODO - remove this periphery ignore comment when this endpoint is integrated with catalog sync
234-
public func loadProductVariations(siteID: Int64, pageNumber: Int) async throws -> PagedItems<POSProductVariation> {
243+
public func loadProductVariations(siteID: Int64, pageNumber: Int, allowCellular: Bool) async throws -> PagedItems<POSProductVariation> {
235244
let path = Path.variations
236245
let parameters = [
237246
ParameterKey.page: String(pageNumber),
@@ -245,7 +254,8 @@ public class POSCatalogSyncRemote: Remote, POSCatalogSyncRemoteProtocol {
245254
siteID: siteID,
246255
path: path,
247256
parameters: parameters,
248-
availableAsRESTRequest: true
257+
availableAsRESTRequest: true,
258+
allowsCellularAccess: allowCellular
249259
)
250260
let mapper = ListMapper<POSProductVariation>(siteID: siteID)
251261
let (variations, responseHeaders) = try await enqueueWithResponseHeaders(request, mapper: mapper)
@@ -331,7 +341,6 @@ private extension POSCatalogSyncRemote {
331341
// MARK: - Response Models
332342

333343
/// Response from catalog generation request.
334-
// periphery:ignore - TODO - remove this periphery ignore comment when this endpoint is integrated with catalog sync
335344
public struct POSCatalogRequestResponse: Decodable {
336345
/// Current status of the catalog generation job.
337346
public let status: POSCatalogStatus
@@ -353,7 +362,6 @@ public enum POSCatalogStatus: String, Decodable {
353362
}
354363

355364
/// POS catalog from download.
356-
// periphery:ignore - TODO - remove this periphery ignore comment when this endpoint is integrated with catalog sync
357365
public struct POSCatalogResponse {
358366
public let products: [POSProduct]
359367
public let variations: [POSProductVariation]

Modules/Sources/NetworkingCore/Requests/JetpackRequest.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public struct JetpackRequest: Request, RESTRequestConvertible {
3838
///
3939
private let availableAsRESTRequest: Bool
4040

41+
/// Whether this request should allow cellular access.
42+
///
43+
private let allowsCellularAccess: Bool
44+
4145

4246
/// Designated Initializer.
4347
///
@@ -48,14 +52,16 @@ public struct JetpackRequest: Request, RESTRequestConvertible {
4852
/// - path: RPC that should be called.
4953
/// - parameters: Collection of Key/Value parameters, to be forwarded to the Jetpack Connected site.
5054
/// - availableAsRESTRequest: Whether the request should be transformed to a REST request if application password is available.
55+
/// - allowsCellularAccess: Whether the request should allow cellular data access.
5156
///
5257
public init(wooApiVersion: WooAPIVersion,
5358
method: HTTPMethod,
5459
siteID: Int64,
5560
locale: String? = nil,
5661
path: String,
5762
parameters: [String: Any]? = nil,
58-
availableAsRESTRequest: Bool = false) {
63+
availableAsRESTRequest: Bool = false,
64+
allowsCellularAccess: Bool = true) {
5965
if [.mark1, .mark2].contains(wooApiVersion) {
6066
DDLogWarn("⚠️ You are using an older version of the Woo REST API: \(wooApiVersion.rawValue), for path: \(path)")
6167
}
@@ -66,14 +72,16 @@ public struct JetpackRequest: Request, RESTRequestConvertible {
6672
self.path = path
6773
self.parameters = parameters ?? [:]
6874
self.availableAsRESTRequest = availableAsRESTRequest
75+
self.allowsCellularAccess = allowsCellularAccess
6976
}
7077

7178

7279
/// Returns a URLRequest instance reprensenting the current Jetpack Request.
7380
///
7481
public func asURLRequest() throws -> URLRequest {
7582
let dotcomEndpoint = DotcomRequest(wordpressApiVersion: JetpackRequest.wordpressApiVersion, method: dotcomMethod, path: dotcomPath)
76-
let dotcomRequest = try dotcomEndpoint.asURLRequest()
83+
var dotcomRequest = try dotcomEndpoint.asURLRequest()
84+
dotcomRequest.allowsCellularAccess = allowsCellularAccess
7785

7886
return try dotcomEncoder.encode(dotcomRequest, with: dotcomParams)
7987
}
@@ -86,7 +94,12 @@ public struct JetpackRequest: Request, RESTRequestConvertible {
8694
guard availableAsRESTRequest else {
8795
return nil
8896
}
89-
return RESTRequest(siteURL: siteURL, wooApiVersion: wooApiVersion, method: method, path: path, parameters: parameters)
97+
return RESTRequest(siteURL: siteURL,
98+
wooApiVersion: wooApiVersion,
99+
method: method,
100+
path: path,
101+
parameters: parameters,
102+
allowsCellularAccess: allowsCellularAccess)
90103
}
91104
}
92105

Modules/Sources/NetworkingCore/Requests/RESTRequest.swift

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,37 @@ public struct RESTRequest: Request {
2424
///
2525
let parameters: [String: Any]?
2626

27+
/// Whether this request should allow cellular access.
28+
///
29+
let allowsCellularAccess: Bool
30+
2731
private init(siteURL: String,
2832
apiVersionPath: String?,
2933
method: HTTPMethod,
3034
path: String,
31-
parameters: [String: Any]? = nil) {
35+
parameters: [String: Any]? = nil,
36+
allowsCellularAccess: Bool = true) {
3237
self.siteURL = siteURL
3338
self.apiVersionPath = apiVersionPath
3439
self.method = method
3540
self.path = path
3641
self.parameters = parameters
42+
self.allowsCellularAccess = allowsCellularAccess
3743
}
3844

3945
/// - Parameters:
4046
/// - siteURL: URL of the site to send the REST request to.
4147
/// - method: HTTP Method we should use.
4248
/// - path: path to the target endpoint.
4349
/// - parameters: Collection of String parameters to be passed over to our target endpoint.
50+
/// - allowsCellularAccess: Whether the request should allow cellular data access.
4451
///
4552
public init(siteURL: String,
4653
method: HTTPMethod,
4754
path: String,
48-
parameters: [String: Any]? = nil) {
49-
self.init(siteURL: siteURL, apiVersionPath: nil, method: method, path: path, parameters: parameters)
55+
parameters: [String: Any]? = nil,
56+
allowsCellularAccess: Bool = true) {
57+
self.init(siteURL: siteURL, apiVersionPath: nil, method: method, path: path, parameters: parameters, allowsCellularAccess: allowsCellularAccess)
5058
}
5159

5260
/// - Parameters:
@@ -55,13 +63,20 @@ public struct RESTRequest: Request {
5563
/// - method: HTTP Method we should use.
5664
/// - path: path to the target endpoint.
5765
/// - parameters: Collection of String parameters to be passed over to our target endpoint.
66+
/// - allowsCellularAccess: Whether the request should allow cellular data access.
5867
///
5968
init(siteURL: String,
6069
wooApiVersion: WooAPIVersion,
6170
method: HTTPMethod,
6271
path: String,
63-
parameters: [String: Any]? = nil) {
64-
self.init(siteURL: siteURL, apiVersionPath: wooApiVersion.path, method: method, path: path, parameters: parameters)
72+
parameters: [String: Any]? = nil,
73+
allowsCellularAccess: Bool = true) {
74+
self.init(siteURL: siteURL,
75+
apiVersionPath: wooApiVersion.path,
76+
method: method,
77+
path: path,
78+
parameters: parameters,
79+
allowsCellularAccess: allowsCellularAccess)
6580
}
6681

6782
/// - Parameters:
@@ -70,13 +85,21 @@ public struct RESTRequest: Request {
7085
/// - method: HTTP Method we should use.
7186
/// - path: path to the target endpoint.
7287
/// - parameters: Collection of String parameters to be passed over to our target endpoint.
88+
/// - allowsCellularAccess: Whether the request should allow cellular data access.
7389
///
90+
// periphery:ignore - we include the cellular parameter for all inits
7491
init(siteURL: String,
7592
wordpressApiVersion: WordPressAPIVersion,
7693
method: HTTPMethod,
7794
path: String,
78-
parameters: [String: Any]? = nil) {
79-
self.init(siteURL: siteURL, apiVersionPath: wordpressApiVersion.path, method: method, path: path, parameters: parameters)
95+
parameters: [String: Any]? = nil,
96+
allowsCellularAccess: Bool = true) {
97+
self.init(siteURL: siteURL,
98+
apiVersionPath: wordpressApiVersion.path,
99+
method: method,
100+
path: path,
101+
parameters: parameters,
102+
allowsCellularAccess: allowsCellularAccess)
80103
}
81104

82105
/// Returns a URLRequest instance representing the current REST API Request.
@@ -87,7 +110,8 @@ public struct RESTRequest: Request {
87110
.map { $0.trimSlashes() }
88111
.filter { $0.isEmpty == false }
89112
let url = try components.joined(separator: "/").asURL()
90-
let request = try URLRequest(url: url, method: method)
113+
var request = try URLRequest(url: url, method: method)
114+
request.allowsCellularAccess = allowsCellularAccess
91115
switch method {
92116
case .post, .put:
93117
return try JSONEncoding.default.encode(request, with: parameters)

0 commit comments

Comments
 (0)