Skip to content

Commit b239a5b

Browse files
committed
Add BookingsRemote
1 parent 17b6355 commit b239a5b

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// periphery:ignore:all
2+
import Foundation
3+
4+
/// Protocol for `BookingsRemote` mainly used for mocking.
5+
///
6+
/// The required methods are intentionally incomplete. Feel free to add the other ones.
7+
///
8+
public protocol BookingsRemoteProtocol {
9+
func loadAllBookings(for siteID: Int64,
10+
pageNumber: Int,
11+
pageSize: Int) async throws -> [Booking]
12+
}
13+
14+
/// Booking: Remote Endpoints
15+
///
16+
public final class BookingsRemote: Remote, BookingsRemoteProtocol {
17+
18+
// MARK: - Bookings
19+
20+
/// Retrieves all of the `Bookings` available.
21+
///
22+
/// - Parameters:
23+
/// - siteID: Site for which we'll fetch remote bookings.
24+
/// - pageNumber: Number of page that should be retrieved.
25+
/// - pageSize: Number of bookings to be retrieved per page.
26+
///
27+
public func loadAllBookings(for siteID: Int64,
28+
pageNumber: Int = Default.pageNumber,
29+
pageSize: Int = Default.pageSize) async throws -> [Booking] {
30+
let parameters = [
31+
ParameterKey.page: String(pageNumber),
32+
ParameterKey.perPage: String(pageSize)
33+
]
34+
35+
let path = Path.bookings
36+
let request = JetpackRequest(wooApiVersion: .wcBookings, method: .get, siteID: siteID, path: path, parameters: parameters, availableAsRESTRequest: true)
37+
let mapper = ListMapper<Booking>(siteID: siteID)
38+
39+
return try await enqueue(request, mapper: mapper)
40+
}
41+
}
42+
43+
// MARK: - Constants
44+
//
45+
public extension BookingsRemote {
46+
enum Default {
47+
public static let pageSize: Int = 25
48+
public static let pageNumber: Int = Remote.Default.firstPageNumber
49+
}
50+
51+
private enum Path {
52+
static let bookings = "bookings"
53+
}
54+
55+
private enum ParameterKey {
56+
static let page: String = "page"
57+
static let perPage: String = "per_page"
58+
}
59+
}

Modules/Sources/NetworkingCore/Settings/WooAPIVersion.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public enum WooAPIVersion: String {
5050
///
5151
case wooShipping = "wcshipping/v1"
5252

53+
/// WooCommerce Bookings Plugin V1.
54+
///
55+
case wcBookings = "wc-bookings/v2"
56+
5357
/// Returns the path for the current API Version
5458
///
5559
var path: String {

0 commit comments

Comments
 (0)