diff --git a/Networking/Networking/Requests/RESTRequest.swift b/Networking/Networking/Requests/RESTRequest.swift index 920b365bccf..1e9e7dc76a8 100644 --- a/Networking/Networking/Requests/RESTRequest.swift +++ b/Networking/Networking/Requests/RESTRequest.swift @@ -8,9 +8,9 @@ struct RESTRequest: Request { /// let siteURL: String - /// WooCommerce API Version + /// WooCommerce / WordPress API Version Path /// - let wooApiVersion: WooAPIVersion + let apiVersionPath: String? /// HTTP Request Method /// @@ -24,30 +24,66 @@ struct RESTRequest: Request { /// let parameters: [String: Any]? - /// Designated Initializer. + private init(siteURL: String, + apiVersionPath: String?, + method: HTTPMethod, + path: String, + parameters: [String: Any]) { + self.siteURL = siteURL + self.apiVersionPath = apiVersionPath + self.method = method + self.path = path + self.parameters = parameters + } + + /// - 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. /// + init(siteURL: String, + method: HTTPMethod, + path: String, + parameters: [String: Any] = [:]) { + self.init(siteURL: siteURL, apiVersionPath: nil, method: method, path: path, parameters: parameters) + } + /// - Parameters: /// - siteURL: URL of the site to send the REST request to. + /// - wooApiVersion: WooCommerce API version. /// - 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. /// init(siteURL: String, - wooApiVersion: WooAPIVersion = .none, + wooApiVersion: WooAPIVersion, method: HTTPMethod, path: String, parameters: [String: Any] = [:]) { - self.siteURL = siteURL - self.wooApiVersion = wooApiVersion - self.method = method - self.path = path - self.parameters = parameters + self.init(siteURL: siteURL, apiVersionPath: wooApiVersion.path, method: method, path: path, parameters: parameters) + } + + /// - Parameters: + /// - siteURL: URL of the site to send the REST request to. + /// - wordpressApiVersion: WordPress API version. + /// - 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. + /// + init(siteURL: String, + wordpressApiVersion: WordPressAPIVersion, + method: HTTPMethod, + path: String, + parameters: [String: Any] = [:]) { + self.init(siteURL: siteURL, apiVersionPath: wordpressApiVersion.path, method: method, path: path, parameters: parameters) } /// Returns a URLRequest instance representing the current REST API Request. /// func asURLRequest() throws -> URLRequest { - let components = [siteURL, Settings.basePath, wooApiVersion.path, path] + let components = [siteURL, Settings.basePath, apiVersionPath, path] + .compactMap { $0 } .map { $0.trimSlashes() } .filter { $0.isEmpty == false } let url = try components.joined(separator: "/").asURL() diff --git a/Networking/NetworkingTests/Requests/JetpackRequestTests.swift b/Networking/NetworkingTests/Requests/JetpackRequestTests.swift index 3f7227cd496..094b3c3391f 100644 --- a/Networking/NetworkingTests/Requests/JetpackRequestTests.swift +++ b/Networking/NetworkingTests/Requests/JetpackRequestTests.swift @@ -140,7 +140,7 @@ final class JetpackRequestTests: XCTestCase { let output = try XCTUnwrap(request.asRESTRequest(with: sampleSiteAddress)) // Then - XCTAssertEqual(output.wooApiVersion, .mark3) + XCTAssertEqual(output.apiVersionPath, WooAPIVersion.mark3.path) XCTAssertEqual(output.method, .post) XCTAssertEqual(output.path, sampleRPC) let params = try XCTUnwrap(output.parameters as? [String: String]) diff --git a/Networking/NetworkingTests/Requests/RESTRequestTests.swift b/Networking/NetworkingTests/Requests/RESTRequestTests.swift index 60f64dd33b8..0717c9fc4a0 100644 --- a/Networking/NetworkingTests/Requests/RESTRequestTests.swift +++ b/Networking/NetworkingTests/Requests/RESTRequestTests.swift @@ -60,6 +60,18 @@ final class RESTRequestTests: XCTestCase { assertEqual(url.absoluteString, expectedURL) } + func test_request_wordPressApiVersion_is_correct() throws { + // Given + let request = RESTRequest(siteURL: sampleSiteAddress, wordpressApiVersion: .wpMark2, method: .get, path: sampleRPC) + + // When + let url = try XCTUnwrap(request.asURLRequest().url) + + // Then + let expectedURL = "https://wordpress.com/wp-json/wp/v2/sample" + assertEqual(url.absoluteString, expectedURL) + } + func test_it_uses_JSON_encoding_for_post_method() throws { // Given let request = RESTRequest(siteURL: sampleSiteAddress, wooApiVersion: sampleWooApiVersion, method: .post, path: sampleRPC, parameters: sampleParameters)