|
| 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 | +} |
0 commit comments