|
| 1 | +import Combine |
| 2 | +import Foundation |
| 3 | + |
| 4 | +/// Use this object to make network calls and receive decoded values wrapped into Combine's `AnyPublisher`. |
| 5 | +public struct CombineCaller { |
| 6 | + public typealias AnyURLSessionDataPublisher = AnyPublisher<URLSession.DataTaskPublisher.Output, URLSession.DataTaskPublisher.Failure> |
| 7 | + |
| 8 | + private let middleware: [URLRequestPlugable] |
| 9 | + private let utility: CallerUtility |
| 10 | + |
| 11 | + /// Gets the data task publisher. |
| 12 | + private let getDataDataTaskPublisher: (URLRequest) -> AnyURLSessionDataPublisher |
| 13 | + |
| 14 | + /// Initialises an object which can make network calls. |
| 15 | + /// - Parameters: |
| 16 | + /// - urlSession: Session that would make the actual network call. |
| 17 | + /// - decoder: Decoder that would decode the received data from the network call. |
| 18 | + /// - middleware: Middleware that is injected in the networking events. |
| 19 | + public init(urlSession: URLSession = .shared, decoder: JSONDecoder, middleware: [URLRequestPlugable] = []) { |
| 20 | + self.init( |
| 21 | + decoder: decoder, |
| 22 | + getDataPublisher: { urlSession.dataTaskPublisher(for: $0).eraseToAnyPublisher() }, |
| 23 | + middleware: middleware |
| 24 | + ) |
| 25 | + } |
| 26 | + |
| 27 | + /// Initialises an object which can make network calls. |
| 28 | + /// - Parameters: |
| 29 | + /// - decoder: Decoder that would decode the received data from the network call. |
| 30 | + /// - getDataPublisher: A closure that returns a data task publisher. |
| 31 | + init( |
| 32 | + decoder: JSONDecoder, |
| 33 | + getDataPublisher: @escaping (URLRequest) -> AnyURLSessionDataPublisher, |
| 34 | + middleware: [URLRequestPlugable] = [] |
| 35 | + ) { |
| 36 | + self.middleware = middleware |
| 37 | + self.getDataDataTaskPublisher = getDataPublisher |
| 38 | + self.utility = .init(decoder: decoder) |
| 39 | + } |
| 40 | + |
| 41 | + /// Method which calls the network request. |
| 42 | + /// - Parameters: |
| 43 | + /// - request: The `URLRequest` that should be called. |
| 44 | + /// - errorType: The error type to be decoded from the body in non-success cases. |
| 45 | + /// - Returns: The result from the network call wrapped into `AnyPublisher`. |
| 46 | + public func call<D: Decodable, DE: DecodableError>( |
| 47 | + using request: URLRequest, |
| 48 | + errorType: DE.Type |
| 49 | + ) -> AnyPublisher<D, NetworkingError> { |
| 50 | + getDataDataTaskPublisher(request) |
| 51 | + .attachMiddleware(middleware, for: request) |
| 52 | + .tryMap { try utility.checkResponseForErrors(data: $0.data, urlResponse: $0.response, errorType: errorType) } |
| 53 | + .tryMap(utility.decodeIfNecessary) |
| 54 | + .mapError(utility.mapError) |
| 55 | + .attachCompletionMiddleware(middleware, request: request) |
| 56 | + .eraseToAnyPublisher() |
| 57 | + } |
| 58 | + |
| 59 | + /// Method which calls the network request without expecting a response body. |
| 60 | + /// - Parameters: |
| 61 | + /// - request: The `URLRequest` that should be called. |
| 62 | + /// - errorType: The error type to be decoded from the body in non-success cases. |
| 63 | + /// - Returns: The result from the network call wrapped into `AnyPublisher`. |
| 64 | + public func call<DE: DecodableError>( |
| 65 | + using request: URLRequest, |
| 66 | + errorType: DE.Type |
| 67 | + ) -> AnyPublisher<Void, NetworkingError> { |
| 68 | + getDataDataTaskPublisher(request) |
| 69 | + .attachMiddleware(middleware, for: request) |
| 70 | + .tryMap { try utility.checkResponseForErrors(data: $0.data, urlResponse: $0.response, errorType: errorType) } |
| 71 | + .tryMap(utility.tryMapEmptyResponseBody(data:)) |
| 72 | + .mapError(utility.mapError) |
| 73 | + .attachCompletionMiddleware(middleware, request: request) |
| 74 | + .eraseToAnyPublisher() |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +public extension CombineCaller { |
| 79 | + /// Convenient method which calls the builded network request using the `URLRequestBuilder` object. |
| 80 | + /// The building and the error handling of the `URLRequest` are handled here. |
| 81 | + /// - Parameters: |
| 82 | + /// - builder: The builder from which the `URLRequest` will be constructed and called. |
| 83 | + /// - errorType: The error type to be decoded from the body in non-success cases. |
| 84 | + /// - Returns: The result from the network call wrapped into `AnyPublisher`. |
| 85 | + func call<D: Decodable, E: DecodableError>( |
| 86 | + using builder: URLRequestBuilder, |
| 87 | + errorType: E.Type |
| 88 | + ) -> AnyPublisher<D, NetworkingError> { |
| 89 | + do { |
| 90 | + let urlRequest = try builder.build() |
| 91 | + return call(using: urlRequest, errorType: errorType) |
| 92 | + } catch { |
| 93 | + return Fail(error: utility.mapError(error)) |
| 94 | + .attachCompletionMiddleware(middleware, request: nil) |
| 95 | + .eraseToAnyPublisher() |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /// Convenient method which calls the builded network request using the `URLRequestBuilder` object without expecting a response body. |
| 100 | + /// The building and the error handling of the `URLRequest` are handled here. |
| 101 | + /// - Parameters: |
| 102 | + /// - builder: The builder from which the `URLRequest` will be constructed and called. |
| 103 | + /// - errorType: The error type to be decoded from the body in non-success cases. |
| 104 | + /// - Returns: The result from the network call wrapped into `AnyPublisher`. |
| 105 | + func call<E: DecodableError>( |
| 106 | + using builder: URLRequestBuilder, |
| 107 | + errorType: E.Type |
| 108 | + ) -> AnyPublisher<Void, NetworkingError> { |
| 109 | + do { |
| 110 | + let urlRequest = try builder.build() |
| 111 | + return call(using: urlRequest, errorType: errorType) |
| 112 | + } catch { |
| 113 | + return Fail(error: utility.mapError(error)) |
| 114 | + .attachCompletionMiddleware(middleware, request: nil) |
| 115 | + .eraseToAnyPublisher() |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +private extension CombineCaller.AnyURLSessionDataPublisher { |
| 121 | + func attachMiddleware( |
| 122 | + _ middleware: [URLRequestPlugable], |
| 123 | + for request: URLRequest |
| 124 | + ) -> Publishers.HandleEvents<Self> { |
| 125 | + handleEvents( |
| 126 | + receiveSubscription: { _ in |
| 127 | + middleware.forEach { $0.onRequest(request) } |
| 128 | + }, |
| 129 | + receiveOutput: { data, response in |
| 130 | + middleware.forEach { $0.onResponse(data: data, response: response) } |
| 131 | + } |
| 132 | + ) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +private extension Publisher where Failure == NetworkingError { |
| 137 | + func attachCompletionMiddleware( |
| 138 | + _ middleware: [URLRequestPlugable], |
| 139 | + request: URLRequest? |
| 140 | + ) -> Publishers.HandleEvents<Self> { |
| 141 | + handleEvents(receiveCompletion: { completion in |
| 142 | + switch completion { |
| 143 | + case .failure(let error): |
| 144 | + middleware.forEach { $0.onError(error, request: request) } |
| 145 | + default: |
| 146 | + return |
| 147 | + } |
| 148 | + }) |
| 149 | + } |
| 150 | +} |
0 commit comments