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
56 changes: 46 additions & 10 deletions Networking/Networking/Requests/RESTRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
12 changes: 12 additions & 0 deletions Networking/NetworkingTests/Requests/RESTRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down