|
| 1 | +// |
| 2 | +// SmartNet+Async.swift |
| 3 | +// |
| 4 | +// Copyright (c) 2021 Valerio69 ( [email protected]) |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in |
| 14 | +// all copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +// THE SOFTWARE. |
| 23 | +// |
| 24 | + |
| 25 | +import Combine |
| 26 | +import Foundation |
| 27 | + |
| 28 | +// MARK: - Networking Closure |
| 29 | + |
| 30 | +public extension SmartNet { |
| 31 | + func request<D, E>( |
| 32 | + with endpoint: E, |
| 33 | + decoder: JSONDecoder = .default, |
| 34 | + queue: DispatchQueue = .main, |
| 35 | + progressHUD: SNProgressHUD? = nil |
| 36 | + ) -> AnyPublisher<D, Error> where D: Decodable, D == E.Response, E: Requestable { |
| 37 | + dataRequest(with: endpoint) |
| 38 | + .decode(type: D.self, decoder: decoder) |
| 39 | + .eraseToAnyPublisher() |
| 40 | + } |
| 41 | + |
| 42 | + func request<E>( |
| 43 | + with endpoint: E, |
| 44 | + queue: DispatchQueue = .main, |
| 45 | + progressHUD: SNProgressHUD? = nil |
| 46 | + ) -> AnyPublisher<E.Response, Error> where E: Requestable, E.Response == Data { |
| 47 | + dataRequest(with: endpoint) |
| 48 | + } |
| 49 | + |
| 50 | + func request<E>( |
| 51 | + with endpoint: E, |
| 52 | + queue: DispatchQueue = .main, |
| 53 | + progressHUD: SNProgressHUD? = nil |
| 54 | + ) -> AnyPublisher<E.Response, Error> where E: Requestable, E.Response == String { |
| 55 | + dataRequest(with: endpoint) |
| 56 | + .tryMap { data in |
| 57 | + guard |
| 58 | + let string = String(data: data, encoding: .utf8) |
| 59 | + else { |
| 60 | + throw NetworkError.dataToStringFailure(data: data) |
| 61 | + } |
| 62 | + return string |
| 63 | + } |
| 64 | + .eraseToAnyPublisher() |
| 65 | + } |
| 66 | + |
| 67 | + func request<E>( |
| 68 | + with endpoint: E, |
| 69 | + queue: DispatchQueue = .main, |
| 70 | + progressHUD: SNProgressHUD? = nil |
| 71 | + ) -> AnyPublisher<E.Response, Error> where E: Requestable, E.Response == Void { |
| 72 | + dataRequest(with: endpoint) |
| 73 | + .map { _ in return Void() } // Convert received Data to Void |
| 74 | + .eraseToAnyPublisher() |
| 75 | + } |
| 76 | + |
| 77 | + @discardableResult |
| 78 | + internal func dataRequest<E>( |
| 79 | + with endpoint: E, |
| 80 | + queue: DispatchQueue = .main, |
| 81 | + progressHUD: SNProgressHUD? = nil |
| 82 | + ) -> AnyPublisher<Data, Error> where E : Requestable { |
| 83 | + Future<Data, Error> { [weak self] promise in |
| 84 | + self?.dataRequest(with: endpoint, progressHUD: progressHUD) { response in |
| 85 | + switch response.result { |
| 86 | + case .success(let data): |
| 87 | + promise(.success(data)) |
| 88 | + case .failure(let error): |
| 89 | + promise(.failure(error)) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + .receive(on: queue) |
| 94 | + .eraseToAnyPublisher() |
| 95 | + } |
| 96 | +} |
0 commit comments