Skip to content

Commit 920accc

Browse files
committed
Networking & Yosemite layer changes for loading a WPCOM plan.
1 parent 4ee31c1 commit 920accc

File tree

6 files changed

+52
-1
lines changed

6 files changed

+52
-1
lines changed

Networking/Networking/Remote/PaymentRemote.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
import Foundation
22

33
public protocol PaymentRemoteProtocol {
4+
func loadPlan(thatMatchesID productID: Int64) async throws -> WPComPlan
5+
46
func createCart(siteID: Int64, productID: Int64) async throws -> CreateCartResponse
57
}
68

79
/// WPCOM Payment Endpoints
810
///
911
public class PaymentRemote: Remote, PaymentRemoteProtocol {
12+
public func loadPlan(thatMatchesID productID: Int64) async throws -> WPComPlan {
13+
let path = Path.products
14+
let request = DotcomRequest(wordpressApiVersion: .mark1_5, method: .get, path: path)
15+
let plans: [WPComPlan] = try await enqueue(request)
16+
guard let plan = plans.first(where: { $0.productID == productID }) else {
17+
throw LoadPlanError.noMatchingPlan
18+
}
19+
return plan
20+
}
21+
1022
public func createCart(siteID: Int64, productID: Int64) async throws -> CreateCartResponse {
1123
let path = "\(Path.cartCreation)/\(siteID)"
1224

@@ -24,12 +36,29 @@ public class PaymentRemote: Remote, PaymentRemoteProtocol {
2436
}
2537
}
2638

39+
public struct WPComPlan: Decodable {
40+
public let productID: Int64
41+
public let name: String
42+
public let formattedPrice: String
43+
44+
private enum CodingKeys: String, CodingKey {
45+
case productID = "product_id"
46+
case name = "product_name"
47+
case formattedPrice = "formatted_price"
48+
}
49+
}
50+
51+
public enum LoadPlanError: Error {
52+
case noMatchingPlan
53+
}
54+
2755
public struct CreateCartResponse: Decodable {}
2856

2957
// MARK: - Constants
3058
//
3159
private extension PaymentRemote {
3260
enum Path {
61+
static let products = "plans"
3362
static let cartCreation = "me/shopping-cart"
3463
}
3564
}

Networking/Networking/Requests/DotcomRequest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct DotcomRequest: Request {
5353

5454
func responseDataValidator() -> ResponseDataValidator {
5555
switch wordpressApiVersion {
56-
case .mark1_1, .mark1_2:
56+
case .mark1_1, .mark1_2, .mark1_5:
5757
return DotcomValidator()
5858
case .wpcomMark2, .wpMark2:
5959
return WordPressApiValidator()

Networking/Networking/Settings/WordPressAPIVersion.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ enum WordPressAPIVersion: String {
1313
///
1414
case mark1_2 = "rest/v1.2/"
1515

16+
/// WordPress.com Endpoint Mark 1.5
17+
///
18+
case mark1_5 = "rest/v1.5/"
19+
1620
/// WPcom REST API Endpoint Mark 2
1721
///
1822
case wpcomMark2 = "wpcom/v2/"

Yosemite/Yosemite/Actions/PaymentAction.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import Foundation
33
/// PaymentAction: Defines all of the Actions supported by the PaymentStore.
44
///
55
public enum PaymentAction: Action {
6+
case loadPlan(productID: Int64,
7+
completion: (Result<WPComPlan, Error>) -> Void)
8+
69
/// Creates a cart with a WPCOM product.
710
/// - Parameters:
811
/// - productID: The ID of the WPCOM product.

Yosemite/Yosemite/Model/Model.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public typealias TopEarnerStats = Networking.TopEarnerStats
135135
public typealias TopEarnerStatsItem = Networking.TopEarnerStatsItem
136136
public typealias User = Networking.User
137137
public typealias WooAPIVersion = Networking.WooAPIVersion
138+
public typealias WPComPlan = Networking.WPComPlan
138139
public typealias StoredProductSettings = Networking.StoredProductSettings
139140
public typealias CardReader = Hardware.CardReader
140141
public typealias CardReaderEvent = Hardware.CardReaderEvent

Yosemite/Yosemite/Stores/PaymentStore.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,27 @@ public final class PaymentStore: Store {
3838
return
3939
}
4040
switch action {
41+
case .loadPlan(let productID, let completion):
42+
loadPlan(productID: productID, completion: completion)
4143
case .createCart(let productID, let siteID, let completion):
4244
createCart(productID: productID, siteID: siteID, completion: completion)
4345
}
4446
}
4547
}
4648

4749
private extension PaymentStore {
50+
func loadPlan(productID: Int64,
51+
completion: @escaping (Result<WPComPlan, Error>) -> Void) {
52+
Task { @MainActor in
53+
do {
54+
let plan = try await remote.loadPlan(thatMatchesID: productID)
55+
completion(.success(plan))
56+
} catch {
57+
completion(.failure(error))
58+
}
59+
}
60+
}
61+
4862
func createCart(productID: String,
4963
siteID: Int64,
5064
completion: @escaping (Result<Void, Error>) -> Void) {

0 commit comments

Comments
 (0)