Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 29 additions & 21 deletions Modules/Sources/Networking/Remote/POSCatalogSyncRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,39 @@ public protocol POSCatalogSyncRemoteProtocol {
/// - Parameters:
/// - siteID: Site ID to generate catalog for.
/// - forceGeneration: Whether to always generate a catalog.
/// - allowCellular: Should cellular data be used if required.
/// - Returns: Catalog job response with job ID.
///
// periphery:ignore - TODO - remove this periphery ignore comment when this endpoint is integrated with catalog sync
func requestCatalogGeneration(for siteID: Int64, forceGeneration: Bool) async throws -> POSCatalogRequestResponse
func requestCatalogGeneration(for siteID: Int64, forceGeneration: Bool, allowCellular: Bool) async throws -> POSCatalogRequestResponse

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

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

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

/// Gets the total count of products for the specified site.
///
Expand Down Expand Up @@ -87,7 +92,6 @@ public class POSCatalogSyncRemote: Remote, POSCatalogSyncRemoteProtocol {
/// - pageNumber: Page number for pagination.
/// - Returns: Paginated list of POS products.
///
// periphery:ignore - TODO - remove this periphery ignore comment when this endpoint is integrated with catalog sync
public func loadProducts(modifiedAfter: Date, siteID: Int64, pageNumber: Int)
async throws -> PagedItems<POSProduct> {
let path = Path.products
Expand Down Expand Up @@ -120,7 +124,6 @@ public class POSCatalogSyncRemote: Remote, POSCatalogSyncRemoteProtocol {
/// - pageNumber: Page number for pagination.
/// - Returns: Paginated list of POS product variations.
///
// periphery:ignore - TODO - remove this periphery ignore comment when this endpoint is integrated with catalog sync
public func loadProductVariations(modifiedAfter: Date, siteID: Int64, pageNumber: Int) async throws -> PagedItems<POSProductVariation> {
let path = Path.variations
let parameters = [
Expand Down Expand Up @@ -151,10 +154,11 @@ public class POSCatalogSyncRemote: Remote, POSCatalogSyncRemoteProtocol {
///
/// - Parameters:
/// - siteID: Site ID to generate catalog for.
/// - forceGeneration: Whether to always generate a catalog.
/// - allowCellular: Should cellular data be used if required.
/// - Returns: Catalog job response with job ID.
///
// periphery:ignore - TODO - remove this periphery ignore comment when this endpoint is integrated with catalog sync
public func requestCatalogGeneration(for siteID: Int64, forceGeneration: Bool) async throws -> POSCatalogRequestResponse {
public func requestCatalogGeneration(for siteID: Int64, forceGeneration: Bool, allowCellular: Bool) async throws -> POSCatalogRequestResponse {
let path = "products/catalog"
let parameters: [String: Any] = [
ParameterKey.fullSyncFields: POSProduct.requestFields,
Expand All @@ -166,7 +170,8 @@ public class POSCatalogSyncRemote: Remote, POSCatalogSyncRemoteProtocol {
siteID: siteID,
path: path,
parameters: parameters,
availableAsRESTRequest: true
availableAsRESTRequest: true,
allowsCellularAccess: allowCellular
)
let mapper = SingleItemMapper<POSCatalogRequestResponse>(siteID: siteID)
return try await enqueue(request, mapper: mapper)
Expand All @@ -176,14 +181,17 @@ public class POSCatalogSyncRemote: Remote, POSCatalogSyncRemoteProtocol {
/// - Parameters:
/// - siteID: Site ID to download catalog for.
/// - downloadURL: Download URL of the catalog file.
/// - allowCellular: Should cellular data be used if required.
/// - Returns: List of products and variations in the POS catalog.
// periphery:ignore - TODO - remove this periphery ignore comment when this endpoint is integrated with catalog sync
public func downloadCatalog(for siteID: Int64, downloadURL: String) async throws -> POSCatalogResponse {
public func downloadCatalog(for siteID: Int64,
downloadURL: String,
allowCellular: Bool) async throws -> POSCatalogResponse {
// TODO: WOOMOB-1173 - move download task to the background using `URLSessionConfiguration.background`
guard let url = URL(string: downloadURL) else {
throw NetworkError.invalidURL
}
let request = URLRequest(url: url)
var request = URLRequest(url: url)
request.allowsCellularAccess = allowCellular
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL: allowsCellularAccess property in the URLRequest

let mapper = ListMapper<POSProduct>(siteID: siteID)
let items = try await enqueue(request, mapper: mapper)
let variationProductTypeKey = "variation"
Expand All @@ -198,10 +206,10 @@ public class POSCatalogSyncRemote: Remote, POSCatalogSyncRemoteProtocol {
/// - Parameters:
/// - siteID: Site ID to load products from.
/// - pageNumber: Page number for pagination.
/// - allowCellular: Should cellular data be used if required.
/// - Returns: Paginated list of POS products.
///
// periphery:ignore - TODO - remove this periphery ignore comment when this endpoint is integrated with catalog sync
public func loadProducts(siteID: Int64, pageNumber: Int) async throws -> PagedItems<POSProduct> {
public func loadProducts(siteID: Int64, pageNumber: Int, allowCellular: Bool) async throws -> PagedItems<POSProduct> {
let path = Path.products
let parameters = [
ParameterKey.page: String(pageNumber),
Expand All @@ -215,7 +223,8 @@ public class POSCatalogSyncRemote: Remote, POSCatalogSyncRemoteProtocol {
siteID: siteID,
path: path,
parameters: parameters,
availableAsRESTRequest: true
availableAsRESTRequest: true,
allowsCellularAccess: allowCellular
)
let mapper = ListMapper<POSProduct>(siteID: siteID)
let (products, responseHeaders) = try await enqueueWithResponseHeaders(request, mapper: mapper)
Expand All @@ -228,10 +237,10 @@ public class POSCatalogSyncRemote: Remote, POSCatalogSyncRemoteProtocol {
/// - Parameters:
/// - siteID: Site ID to load variations from.
/// - pageNumber: Page number for pagination.
/// - allowCellular: Should cellular data be used if required.
/// - Returns: Paginated list of POS product variations.
///
// periphery:ignore - TODO - remove this periphery ignore comment when this endpoint is integrated with catalog sync
public func loadProductVariations(siteID: Int64, pageNumber: Int) async throws -> PagedItems<POSProductVariation> {
public func loadProductVariations(siteID: Int64, pageNumber: Int, allowCellular: Bool) async throws -> PagedItems<POSProductVariation> {
let path = Path.variations
let parameters = [
ParameterKey.page: String(pageNumber),
Expand All @@ -245,7 +254,8 @@ public class POSCatalogSyncRemote: Remote, POSCatalogSyncRemoteProtocol {
siteID: siteID,
path: path,
parameters: parameters,
availableAsRESTRequest: true
availableAsRESTRequest: true,
allowsCellularAccess: allowCellular
)
let mapper = ListMapper<POSProductVariation>(siteID: siteID)
let (variations, responseHeaders) = try await enqueueWithResponseHeaders(request, mapper: mapper)
Expand Down Expand Up @@ -331,7 +341,6 @@ private extension POSCatalogSyncRemote {
// MARK: - Response Models

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

/// POS catalog from download.
// periphery:ignore - TODO - remove this periphery ignore comment when this endpoint is integrated with catalog sync
public struct POSCatalogResponse {
public let products: [POSProduct]
public let variations: [POSProductVariation]
Expand Down
19 changes: 16 additions & 3 deletions Modules/Sources/NetworkingCore/Requests/JetpackRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public struct JetpackRequest: Request, RESTRequestConvertible {
///
private let availableAsRESTRequest: Bool

/// Whether this request should allow cellular access.
///
private let allowsCellularAccess: Bool


/// Designated Initializer.
///
Expand All @@ -48,14 +52,16 @@ public struct JetpackRequest: Request, RESTRequestConvertible {
/// - path: RPC that should be called.
/// - parameters: Collection of Key/Value parameters, to be forwarded to the Jetpack Connected site.
/// - availableAsRESTRequest: Whether the request should be transformed to a REST request if application password is available.
/// - allowsCellularAccess: Whether the request should allow cellular data access.
///
public init(wooApiVersion: WooAPIVersion,
method: HTTPMethod,
siteID: Int64,
locale: String? = nil,
path: String,
parameters: [String: Any]? = nil,
availableAsRESTRequest: Bool = false) {
availableAsRESTRequest: Bool = false,
allowsCellularAccess: Bool = true) {
if [.mark1, .mark2].contains(wooApiVersion) {
DDLogWarn("⚠️ You are using an older version of the Woo REST API: \(wooApiVersion.rawValue), for path: \(path)")
}
Expand All @@ -66,14 +72,16 @@ public struct JetpackRequest: Request, RESTRequestConvertible {
self.path = path
self.parameters = parameters ?? [:]
self.availableAsRESTRequest = availableAsRESTRequest
self.allowsCellularAccess = allowsCellularAccess
}


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

return try dotcomEncoder.encode(dotcomRequest, with: dotcomParams)
}
Expand All @@ -86,7 +94,12 @@ public struct JetpackRequest: Request, RESTRequestConvertible {
guard availableAsRESTRequest else {
return nil
}
return RESTRequest(siteURL: siteURL, wooApiVersion: wooApiVersion, method: method, path: path, parameters: parameters)
return RESTRequest(siteURL: siteURL,
wooApiVersion: wooApiVersion,
method: method,
path: path,
parameters: parameters,
allowsCellularAccess: allowsCellularAccess)
}
}

Expand Down
40 changes: 32 additions & 8 deletions Modules/Sources/NetworkingCore/Requests/RESTRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,37 @@ public struct RESTRequest: Request {
///
let parameters: [String: Any]?

/// Whether this request should allow cellular access.
///
let allowsCellularAccess: Bool

private init(siteURL: String,
apiVersionPath: String?,
method: HTTPMethod,
path: String,
parameters: [String: Any]? = nil) {
parameters: [String: Any]? = nil,
allowsCellularAccess: Bool = true) {
self.siteURL = siteURL
self.apiVersionPath = apiVersionPath
self.method = method
self.path = path
self.parameters = parameters
self.allowsCellularAccess = allowsCellularAccess
}

/// - Parameters:
/// - siteURL: URL of the site to send the REST request to.
/// - method: HTTP Method we should use.
/// - path: path to the target endpoint.
/// - parameters: Collection of String parameters to be passed over to our target endpoint.
/// - allowsCellularAccess: Whether the request should allow cellular data access.
///
public init(siteURL: String,
method: HTTPMethod,
path: String,
parameters: [String: Any]? = nil) {
self.init(siteURL: siteURL, apiVersionPath: nil, method: method, path: path, parameters: parameters)
parameters: [String: Any]? = nil,
allowsCellularAccess: Bool = true) {
self.init(siteURL: siteURL, apiVersionPath: nil, method: method, path: path, parameters: parameters, allowsCellularAccess: allowsCellularAccess)
}

/// - Parameters:
Expand All @@ -55,13 +63,20 @@ public struct RESTRequest: Request {
/// - method: HTTP Method we should use.
/// - path: path to the target endpoint.
/// - parameters: Collection of String parameters to be passed over to our target endpoint.
/// - allowsCellularAccess: Whether the request should allow cellular data access.
///
init(siteURL: String,
wooApiVersion: WooAPIVersion,
method: HTTPMethod,
path: String,
parameters: [String: Any]? = nil) {
self.init(siteURL: siteURL, apiVersionPath: wooApiVersion.path, method: method, path: path, parameters: parameters)
parameters: [String: Any]? = nil,
allowsCellularAccess: Bool = true) {
self.init(siteURL: siteURL,
apiVersionPath: wooApiVersion.path,
method: method,
path: path,
parameters: parameters,
allowsCellularAccess: allowsCellularAccess)
}

/// - Parameters:
Expand All @@ -70,13 +85,21 @@ public struct RESTRequest: Request {
/// - method: HTTP Method we should use.
/// - path: path to the target endpoint.
/// - parameters: Collection of String parameters to be passed over to our target endpoint.
/// - allowsCellularAccess: Whether the request should allow cellular data access.
///
// periphery:ignore - we include the cellular parameter for all inits
init(siteURL: String,
wordpressApiVersion: WordPressAPIVersion,
method: HTTPMethod,
path: String,
parameters: [String: Any]? = nil) {
self.init(siteURL: siteURL, apiVersionPath: wordpressApiVersion.path, method: method, path: path, parameters: parameters)
parameters: [String: Any]? = nil,
allowsCellularAccess: Bool = true) {
self.init(siteURL: siteURL,
apiVersionPath: wordpressApiVersion.path,
method: method,
path: path,
parameters: parameters,
allowsCellularAccess: allowsCellularAccess)
}

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