-
Notifications
You must be signed in to change notification settings - Fork 32
/
Network+BaseRequestMaking.swift
43 lines (33 loc) · 1.61 KB
/
Network+BaseRequestMaking.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import Foundation
#if canImport(AlicerceCore)
import AlicerceCore
#endif
extension Network {
/// A base request making witness.
public struct BaseRequestMaking<Request> {
/// A make base request handler closure, invoked when the request generation finishes.
public typealias RequestHandler = (Result<Request, Error>) -> Cancelable
/// The make base request closure, invoked to generate a new base request.
///
/// - Important: The cancelable returned by the `handler` closure *when called asynchronously* should be added
/// as a child of the cancelable returned by this closure, so that the async work gets chained and can be
/// cancelled.
///
/// - Parameter handler: The closure to handle the request generation's result (i.e. either the new request or
/// an error).
/// - Returns: A cancelable to cancel the operation.
public let make: (_ handler: RequestHandler) -> Cancelable
/// Instantiates a new base request maker, with the given make closure.
/// - Parameter make: The make base request closure.
public init(make: @escaping (_ handler: RequestHandler) -> Cancelable) {
self.make = make
}
}
}
extension Network.BaseRequestMaking where Request == URLRequest {
/// Creates a new base request making witness from an `HTTPResourceEndpoint`.
/// - Parameter endpoint: the endpoint to make the base request from.
public static func endpoint(_ endpoint: HTTPResourceEndpoint) -> Self {
.init { handler in handler(Result { try endpoint.makeRequest() }) }
}
}