Skip to content

Commit f9d6100

Browse files
committed
Enable setting cellular access on API requests
1 parent b26626e commit f9d6100

File tree

4 files changed

+199
-11
lines changed

4 files changed

+199
-11
lines changed

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: 31 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,20 @@ 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
///
7490
init(siteURL: String,
7591
wordpressApiVersion: WordPressAPIVersion,
7692
method: HTTPMethod,
7793
path: String,
78-
parameters: [String: Any]? = nil) {
79-
self.init(siteURL: siteURL, apiVersionPath: wordpressApiVersion.path, method: method, path: path, parameters: parameters)
94+
parameters: [String: Any]? = nil,
95+
allowsCellularAccess: Bool = true) {
96+
self.init(siteURL: siteURL,
97+
apiVersionPath: wordpressApiVersion.path,
98+
method: method,
99+
path: path,
100+
parameters: parameters,
101+
allowsCellularAccess: allowsCellularAccess)
80102
}
81103

82104
/// Returns a URLRequest instance representing the current REST API Request.
@@ -87,7 +109,8 @@ public struct RESTRequest: Request {
87109
.map { $0.trimSlashes() }
88110
.filter { $0.isEmpty == false }
89111
let url = try components.joined(separator: "/").asURL()
90-
let request = try URLRequest(url: url, method: method)
112+
var request = try URLRequest(url: url, method: method)
113+
request.allowsCellularAccess = allowsCellularAccess
91114
switch method {
92115
case .post, .put:
93116
return try JSONEncoding.default.encode(request, with: parameters)

Modules/Tests/NetworkingTests/Requests/JetpackRequestTests.swift

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,72 @@ final class JetpackRequestTests: XCTestCase {
161161
// Then
162162
XCTAssertNil(request.asRESTRequest(with: sampleSiteAddress))
163163
}
164+
165+
// MARK: - allowsCellularAccess Tests
166+
167+
func test_request_with_allowsCellularAccess_true_sets_URLRequest_property() throws {
168+
// Given
169+
let request = JetpackRequest(wooApiVersion: .mark3,
170+
method: .get,
171+
siteID: sampleSiteID,
172+
path: sampleRPC,
173+
parameters: sampleParameters,
174+
allowsCellularAccess: true)
175+
176+
// When
177+
let urlRequest = try request.asURLRequest()
178+
179+
// Then
180+
XCTAssertTrue(urlRequest.allowsCellularAccess)
181+
}
182+
183+
func test_request_with_allowsCellularAccess_false_sets_URLRequest_property() throws {
184+
// Given
185+
let request = JetpackRequest(wooApiVersion: .mark3,
186+
method: .get,
187+
siteID: sampleSiteID,
188+
path: sampleRPC,
189+
parameters: sampleParameters,
190+
allowsCellularAccess: false)
191+
192+
// When
193+
let urlRequest = try request.asURLRequest()
194+
195+
// Then
196+
XCTAssertFalse(urlRequest.allowsCellularAccess)
197+
}
198+
199+
func test_request_defaults_to_allowsCellularAccess_true() throws {
200+
// Given - no explicit allowsCellularAccess parameter
201+
let request = JetpackRequest(wooApiVersion: .mark3,
202+
method: .get,
203+
siteID: sampleSiteID,
204+
path: sampleRPC,
205+
parameters: sampleParameters)
206+
207+
// When
208+
let urlRequest = try request.asURLRequest()
209+
210+
// Then
211+
XCTAssertTrue(urlRequest.allowsCellularAccess)
212+
}
213+
214+
func test_converting_to_RESTRequest_preserves_allowsCellularAccess() throws {
215+
// Given
216+
let request = JetpackRequest(wooApiVersion: .mark3,
217+
method: .post,
218+
siteID: sampleSiteID,
219+
path: sampleRPC,
220+
parameters: sampleParameters,
221+
availableAsRESTRequest: true,
222+
allowsCellularAccess: false)
223+
224+
// When
225+
let restRequest = try XCTUnwrap(request.asRESTRequest(with: sampleSiteAddress))
226+
227+
// Then
228+
XCTAssertFalse(restRequest.allowsCellularAccess)
229+
}
164230
}
165231

166232

Modules/Tests/NetworkingTests/Requests/RESTRequestTests.swift

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,90 @@ final class RESTRequestTests: XCTestCase {
146146
XCTAssertNotNil(urlRequest.httpBody)
147147
}
148148
}
149+
150+
// MARK: - allowsCellularAccess Tests
151+
152+
func test_request_with_allowsCellularAccess_true_sets_URLRequest_property() throws {
153+
// Given
154+
let request = RESTRequest(siteURL: sampleSiteAddress,
155+
wooApiVersion: sampleWooApiVersion,
156+
method: .get,
157+
path: sampleRPC,
158+
parameters: sampleParameters,
159+
allowsCellularAccess: true)
160+
161+
// When
162+
let urlRequest = try request.asURLRequest()
163+
164+
// Then
165+
XCTAssertTrue(urlRequest.allowsCellularAccess)
166+
}
167+
168+
func test_request_with_allowsCellularAccess_false_sets_URLRequest_property() throws {
169+
// Given
170+
let request = RESTRequest(siteURL: sampleSiteAddress,
171+
wooApiVersion: sampleWooApiVersion,
172+
method: .get,
173+
path: sampleRPC,
174+
parameters: sampleParameters,
175+
allowsCellularAccess: false)
176+
177+
// When
178+
let urlRequest = try request.asURLRequest()
179+
180+
// Then
181+
XCTAssertFalse(urlRequest.allowsCellularAccess)
182+
}
183+
184+
func test_request_defaults_to_allowsCellularAccess_true() throws {
185+
// Given - no explicit allowsCellularAccess parameter
186+
let request = RESTRequest(siteURL: sampleSiteAddress,
187+
wooApiVersion: sampleWooApiVersion,
188+
method: .get,
189+
path: sampleRPC,
190+
parameters: sampleParameters)
191+
192+
// When
193+
let urlRequest = try request.asURLRequest()
194+
195+
// Then
196+
XCTAssertTrue(urlRequest.allowsCellularAccess)
197+
}
198+
199+
func test_request_with_allowsCellularAccess_works_for_all_initializers() throws {
200+
// Given - Test all three initializers
201+
202+
// 1. Simple initializer
203+
let simpleRequest = RESTRequest(siteURL: sampleSiteAddress,
204+
method: .get,
205+
path: sampleRPC,
206+
parameters: sampleParameters,
207+
allowsCellularAccess: false)
208+
209+
// 2. WooApiVersion initializer
210+
let wooRequest = RESTRequest(siteURL: sampleSiteAddress,
211+
wooApiVersion: sampleWooApiVersion,
212+
method: .get,
213+
path: sampleRPC,
214+
parameters: sampleParameters,
215+
allowsCellularAccess: false)
216+
217+
// 3. WordPressApiVersion initializer
218+
let wpRequest = RESTRequest(siteURL: sampleSiteAddress,
219+
wordpressApiVersion: .wpMark2,
220+
method: .get,
221+
path: sampleRPC,
222+
parameters: sampleParameters,
223+
allowsCellularAccess: false)
224+
225+
// When/Then
226+
let simpleURLRequest = try simpleRequest.asURLRequest()
227+
XCTAssertFalse(simpleURLRequest.allowsCellularAccess)
228+
229+
let wooURLRequest = try wooRequest.asURLRequest()
230+
XCTAssertFalse(wooURLRequest.allowsCellularAccess)
231+
232+
let wpURLRequest = try wpRequest.asURLRequest()
233+
XCTAssertFalse(wpURLRequest.allowsCellularAccess)
234+
}
149235
}

0 commit comments

Comments
 (0)