-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteCounterDecrementer.swift
More file actions
41 lines (33 loc) · 1.13 KB
/
RemoteCounterDecrementer.swift
File metadata and controls
41 lines (33 loc) · 1.13 KB
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
//
// RemoteCounterDecrementer.swift
//
//
// Created by Thales Frigo on 27/05/21.
//
import Foundation
import CounterCore
public final class RemoteCounterDecrementer: CounterDecrementer {
private let client: HTTPClient
public init(client: HTTPClient) {
self.client = client
}
public struct DecrementerError: Error {}
public func decrement(_ id: Counter.ID, completion: @escaping (CounterDecrementer.Result) -> Void) {
let endpoint = CounterAPI.decrement(id: id).request()
client.execute(endpoint) { (result) in
completion(result.flatMap({ (data, response) -> Result<Counter, Error> in
do {
guard let counter = try CounterMapper
.map(data, from: response)
.first(where: { $0.id == id })
else {
throw DecrementerError()
}
return .success(counter)
} catch {
return .failure(error)
}
}))
}
}
}