Skip to content

Commit e1d5b27

Browse files
committed
Add load single booking remote request
1 parent ee4eae1 commit e1d5b27

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import Foundation
2+
3+
/// Mapper: Booking
4+
///
5+
struct BookingMapper: Mapper {
6+
let siteID: Int64
7+
8+
func map(response: Data) throws -> Booking {
9+
let decoder = JSONDecoder()
10+
decoder.dateDecodingStrategy = .formatted(DateFormatter.Defaults.dateTimeFormatter)
11+
decoder.userInfo = [
12+
.siteID: siteID
13+
]
14+
if hasDataEnvelope(in: response) {
15+
return try decoder.decode(BookingEnvelope.self, from: response).booking
16+
} else {
17+
return try decoder.decode(Booking.self, from: response)
18+
}
19+
}
20+
}
21+
22+
private struct BookingEnvelope: Decodable {
23+
let booking: Booking
24+
25+
private enum CodingKeys: String, CodingKey {
26+
case booking = "data"
27+
}
28+
}

Modules/Sources/Networking/Remote/BookingsRemote.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ public protocol BookingsRemoteProtocol {
1212
startDateBefore: String?,
1313
startDateAfter: String?,
1414
searchQuery: String?) async throws -> [Booking]
15+
16+
func loadBooking(bookingID: Int64,
17+
siteID: Int64) async throws -> Booking?
1518
}
1619

1720
/// Booking: Remote Endpoints
@@ -59,6 +62,24 @@ public final class BookingsRemote: Remote, BookingsRemoteProtocol {
5962

6063
return try await enqueue(request, mapper: mapper)
6164
}
65+
66+
public func loadBooking(
67+
bookingID: Int64,
68+
siteID: Int64
69+
) async throws -> Booking? {
70+
let path = "\(Path.bookings)/\(bookingID)"
71+
let request = JetpackRequest(
72+
wooApiVersion: .wcBookings,
73+
method: .get,
74+
siteID: siteID,
75+
path: path,
76+
availableAsRESTRequest: true
77+
)
78+
79+
let mapper = BookingMapper(siteID: siteID)
80+
81+
return try await enqueue(request, mapper: mapper)
82+
}
6283
}
6384

6485
// MARK: - Constants

0 commit comments

Comments
 (0)