Skip to content

Commit aa1889e

Browse files
committed
Update combine client
1 parent 608ba81 commit aa1889e

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

Sources/Apexy/Clients/CombineClient.swift

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,40 @@ public protocol CombineClient: AnyObject {
99
/// - endpoint: endpoint of remote content.
1010
/// - Returns: Publisher which you can subscribe to
1111
func request<T>(_ endpoint: T) -> AnyPublisher<T.Content, Error> where T: Endpoint
12+
13+
/// Upload data to specified endpoint.
14+
/// - Parameters:
15+
/// - endpoint: endpoint of remote content.
16+
/// - Returns: Publisher which you can subscribe to
17+
func upload<T>(_ endpoint: T) -> AnyPublisher<T.Content, Error> where T: UploadEndpoint
1218
}
1319

1420
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
1521
public extension Client where Self: CombineClient {
1622
func request<T>(_ endpoint: T) -> AnyPublisher<T.Content, Error> where T: Endpoint {
17-
Deferred<AnyPublisher<T.Content, Error>> {
18-
let subject = PassthroughSubject<T.Content, Error>()
19-
20-
let progress = self.request(endpoint) { (result: Result<T.Content, Error>) in
21-
switch result {
22-
case .success(let content):
23-
subject.send(content)
24-
subject.send(completion: .finished)
25-
case .failure(let error):
26-
subject.send(completion: .failure(error))
23+
Future<T.Content, Error> { promise in
24+
Task {
25+
do {
26+
let content = try await self.request(endpoint)
27+
promise(.success(content))
28+
} catch {
29+
promise(.failure(error))
30+
}
31+
}
32+
}
33+
.eraseToAnyPublisher()
34+
}
35+
36+
func upload<T>(_ endpoint: T) -> AnyPublisher<T.Content, Error> where T: UploadEndpoint {
37+
Future<T.Content, Error> { promise in
38+
Task {
39+
do {
40+
let content = try await self.upload(endpoint)
41+
promise(.success(content))
42+
} catch {
43+
promise(.failure(error))
2744
}
2845
}
29-
30-
return subject.handleEvents(receiveCancel: {
31-
progress.cancel()
32-
subject.send(completion: .finished)
33-
}).eraseToAnyPublisher()
3446
}
3547
.eraseToAnyPublisher()
3648
}

0 commit comments

Comments
 (0)