Skip to content

Commit 60a9964

Browse files
Make RESTRequest work with both WooCommerce and WordPress API.
1 parent b90ca32 commit 60a9964

File tree

3 files changed

+59
-11
lines changed

3 files changed

+59
-11
lines changed

Networking/Networking/Requests/RESTRequest.swift

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ struct RESTRequest: Request {
88
///
99
let siteURL: String
1010

11-
/// WooCommerce API Version
11+
/// WooCommerce / WordPress API Version Path
1212
///
13-
let wooApiVersion: WooAPIVersion
13+
let apiVersionPath: String?
1414

1515
/// HTTP Request Method
1616
///
@@ -24,30 +24,66 @@ struct RESTRequest: Request {
2424
///
2525
let parameters: [String: Any]?
2626

27-
/// Designated Initializer.
27+
private init(siteURL: String,
28+
apiVersionPath: String?,
29+
method: HTTPMethod,
30+
path: String,
31+
parameters: [String: Any]) {
32+
self.siteURL = siteURL
33+
self.apiVersionPath = apiVersionPath
34+
self.method = method
35+
self.path = path
36+
self.parameters = parameters
37+
}
38+
39+
/// - Parameters:
40+
/// - siteURL: URL of the site to send the REST request to.
41+
/// - method: HTTP Method we should use.
42+
/// - path: path to the target endpoint.
43+
/// - parameters: Collection of String parameters to be passed over to our target endpoint.
2844
///
45+
init(siteURL: String,
46+
method: HTTPMethod,
47+
path: String,
48+
parameters: [String: Any] = [:]) {
49+
self.init(siteURL: siteURL, apiVersionPath: nil, method: method, path: path, parameters: parameters)
50+
}
51+
2952
/// - Parameters:
3053
/// - siteURL: URL of the site to send the REST request to.
54+
/// - wooApiVersion: WooCommerce API version.
3155
/// - method: HTTP Method we should use.
3256
/// - path: path to the target endpoint.
3357
/// - parameters: Collection of String parameters to be passed over to our target endpoint.
3458
///
3559
init(siteURL: String,
36-
wooApiVersion: WooAPIVersion = .none,
60+
wooApiVersion: WooAPIVersion,
3761
method: HTTPMethod,
3862
path: String,
3963
parameters: [String: Any] = [:]) {
40-
self.siteURL = siteURL
41-
self.wooApiVersion = wooApiVersion
42-
self.method = method
43-
self.path = path
44-
self.parameters = parameters
64+
self.init(siteURL: siteURL, apiVersionPath: wooApiVersion.path, method: method, path: path, parameters: parameters)
65+
}
66+
67+
/// - Parameters:
68+
/// - siteURL: URL of the site to send the REST request to.
69+
/// - wordpressApiVersion: WordPress API version.
70+
/// - method: HTTP Method we should use.
71+
/// - path: path to the target endpoint.
72+
/// - parameters: Collection of String parameters to be passed over to our target endpoint.
73+
///
74+
init(siteURL: String,
75+
wordpressApiVersion: WordPressAPIVersion,
76+
method: HTTPMethod,
77+
path: String,
78+
parameters: [String: Any] = [:]) {
79+
self.init(siteURL: siteURL, apiVersionPath: wordpressApiVersion.path, method: method, path: path, parameters: parameters)
4580
}
4681

4782
/// Returns a URLRequest instance representing the current REST API Request.
4883
///
4984
func asURLRequest() throws -> URLRequest {
50-
let components = [siteURL, Settings.basePath, wooApiVersion.path, path]
85+
let components = [siteURL, Settings.basePath, apiVersionPath, path]
86+
.compactMap { $0 }
5187
.map { $0.trimSlashes() }
5288
.filter { $0.isEmpty == false }
5389
let url = try components.joined(separator: "/").asURL()

Networking/NetworkingTests/Requests/JetpackRequestTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ final class JetpackRequestTests: XCTestCase {
140140
let output = try XCTUnwrap(request.asRESTRequest(with: sampleSiteAddress))
141141

142142
// Then
143-
XCTAssertEqual(output.wooApiVersion, .mark3)
143+
XCTAssertEqual(output.apiVersionPath, WooAPIVersion.mark3.path)
144144
XCTAssertEqual(output.method, .post)
145145
XCTAssertEqual(output.path, sampleRPC)
146146
let params = try XCTUnwrap(output.parameters as? [String: String])

Networking/NetworkingTests/Requests/RESTRequestTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ final class RESTRequestTests: XCTestCase {
6060
assertEqual(url.absoluteString, expectedURL)
6161
}
6262

63+
func test_request_wordPressApiVersion_is_correct() throws {
64+
// Given
65+
let request = RESTRequest(siteURL: sampleSiteAddress, wordpressApiVersion: .wpMark2, method: .get, path: sampleRPC)
66+
67+
// When
68+
let url = try XCTUnwrap(request.asURLRequest().url)
69+
70+
// Then
71+
let expectedURL = "https://wordpress.com/wp-json/wp/v2/sample"
72+
assertEqual(url.absoluteString, expectedURL)
73+
}
74+
6375
func test_it_uses_JSON_encoding_for_post_method() throws {
6476
// Given
6577
let request = RESTRequest(siteURL: sampleSiteAddress, wooApiVersion: sampleWooApiVersion, method: .post, path: sampleRPC, parameters: sampleParameters)

0 commit comments

Comments
 (0)