|
| 1 | +// |
| 2 | +// Copyright (C) 2017 DB Systel GmbH. |
| 3 | +// DB Systel GmbH; Jürgen-Ponto-Platz 1; D-60329 Frankfurt am Main; Germany; http://www.dbsystel.de/ |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | +// copy of this software and associated documentation files (the "Software"), |
| 7 | +// to deal in the Software without restriction, including without limitation |
| 8 | +// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | +// and/or sell copies of the Software, and to permit persons to whom the |
| 10 | +// Software is furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in |
| 13 | +// all copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | +// DEALINGS IN THE SOFTWARE. |
| 22 | +// |
| 23 | + |
| 24 | +import Foundation |
| 25 | +import Dispatch |
| 26 | + |
| 27 | +/** |
| 28 | + `BasicNetworkService` handles network request for resources by using a given `NetworkAccess`. |
| 29 | + |
| 30 | + **Example**: |
| 31 | + ```swift |
| 32 | + // Just use an URLSession for the networkAccess. |
| 33 | + let basicNetworkService: NetworkService = BasicNetworkService(networkAccess: URLSession(configuration: .default)) |
| 34 | + ``` |
| 35 | + |
| 36 | + - seealso: `NetworkService` |
| 37 | + */ |
| 38 | +public final class BasicNetworkService: NetworkService { |
| 39 | + let networkAccess: NetworkAccess |
| 40 | + let networkResponseProcessor: NetworkResponseProcessor |
| 41 | + |
| 42 | + /** |
| 43 | + Creates an `BasicNetworkService` instance with a given network access to execute requests on. |
| 44 | + |
| 45 | + - parameter networkAccess: provides basic access to the network. |
| 46 | + */ |
| 47 | + public init(networkAccess: NetworkAccess) { |
| 48 | + self.networkAccess = networkAccess |
| 49 | + self.networkResponseProcessor = NetworkResponseProcessor() |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + Fetches a resource asynchronously from remote location. Execution of the requests starts immediately. |
| 54 | + Execution happens on no specific queue. It dependes on the network access which queue is used. |
| 55 | + Once execution is finished either the completion block or the error block gets called. |
| 56 | + You decide on which queue these blocks get executed. |
| 57 | + |
| 58 | + **Example**: |
| 59 | + ```swift |
| 60 | + let networkService: NetworkService = // |
| 61 | + let resource: Resource<String> = // |
| 62 | + |
| 63 | + networkService.request(queue: .main, resource: resource, onCompletionWithResponse: { htmlText, response in |
| 64 | + print(htmlText, response) |
| 65 | + }, onError: { error in |
| 66 | + // Handle errors |
| 67 | + }) |
| 68 | + ``` |
| 69 | + |
| 70 | + - parameter queue: The `DispatchQueue` to execute the completion and error block on. |
| 71 | + - parameter resource: The resource you want to fetch. |
| 72 | + - parameter onCompletionWithResponse: Callback which gets called when fetching and transforming into model succeeds. |
| 73 | + - parameter onError: Callback which gets called when fetching or transforming fails. |
| 74 | + |
| 75 | + - returns: a running network task |
| 76 | + */ |
| 77 | + @discardableResult |
| 78 | + public func request<Result>(queue: DispatchQueue, resource: Resource<Result>, onCompletionWithResponse: @escaping (Result, HTTPURLResponse) -> Void, |
| 79 | + onError: @escaping (NetworkError) -> Void) -> NetworkTask { |
| 80 | + let request = resource.request.asURLRequest() |
| 81 | + let dataTask = networkAccess.load(request: request, callback: { data, response, error in |
| 82 | + self.networkResponseProcessor.processAsyncResponse(queue: queue, response: response, resource: resource, data: data, |
| 83 | + error: error, onCompletion: onCompletionWithResponse, onError: onError) |
| 84 | + }) |
| 85 | + return dataTask |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments