diff --git a/LeadKitAdditions/LeadKitAdditions.xcodeproj/project.pbxproj b/LeadKitAdditions/LeadKitAdditions.xcodeproj/project.pbxproj index b0dcbd6..9204b0a 100644 --- a/LeadKitAdditions/LeadKitAdditions.xcodeproj/project.pbxproj +++ b/LeadKitAdditions/LeadKitAdditions.xcodeproj/project.pbxproj @@ -28,6 +28,7 @@ EF05EDE91EAFA8A000CAE7B6 /* PassCodeHolderProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF05EDE81EAFA8A000CAE7B6 /* PassCodeHolderProtocol.swift */; }; EF05EDEB1EAFA8E600CAE7B6 /* PassCodeError.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF05EDEA1EAFA8E600CAE7B6 /* PassCodeError.swift */; }; EF05EDED1EAFA96D00CAE7B6 /* PassCodeHolder.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF05EDEC1EAFA96D00CAE7B6 /* PassCodeHolder.swift */; }; + EF05EDF41EB0D05400CAE7B6 /* SimpleNetworkService.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF05EDF31EB0D05400CAE7B6 /* SimpleNetworkService.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -60,6 +61,7 @@ EF05EDE81EAFA8A000CAE7B6 /* PassCodeHolderProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PassCodeHolderProtocol.swift; sourceTree = ""; }; EF05EDEA1EAFA8E600CAE7B6 /* PassCodeError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PassCodeError.swift; sourceTree = ""; }; EF05EDEC1EAFA96D00CAE7B6 /* PassCodeHolder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PassCodeHolder.swift; sourceTree = ""; }; + EF05EDF31EB0D05400CAE7B6 /* SimpleNetworkService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SimpleNetworkService.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -130,6 +132,7 @@ EF05EDBD1EAF706200CAE7B6 /* ApiResponse.swift */, EF05EDBE1EAF706200CAE7B6 /* BaseDateFormatter.swift */, EF05EDBF1EAF706200CAE7B6 /* DefaultNetworkService.swift */, + EF05EDF31EB0D05400CAE7B6 /* SimpleNetworkService.swift */, ); path = Classes; sourceTree = ""; @@ -413,6 +416,7 @@ buildActionMask = 2147483647; files = ( EF05EDB81EAF704800CAE7B6 /* UserDefaults+UserService.swift in Sources */, + EF05EDF41EB0D05400CAE7B6 /* SimpleNetworkService.swift in Sources */, EF05EDE11EAFA74200CAE7B6 /* BasePassCodeViewController.swift in Sources */, EF05EDC61EAF70EB00CAE7B6 /* TouchIDService.swift in Sources */, EF05EDE31EAFA7A600CAE7B6 /* BasePassCodeViewModel.swift in Sources */, diff --git a/LeadKitAdditions/LeadKitAdditions/Classes/DefaultNetworkService.swift b/LeadKitAdditions/LeadKitAdditions/Classes/DefaultNetworkService.swift index 3b5b057..4ff5f9f 100644 --- a/LeadKitAdditions/LeadKitAdditions/Classes/DefaultNetworkService.swift +++ b/LeadKitAdditions/LeadKitAdditions/Classes/DefaultNetworkService.swift @@ -28,14 +28,16 @@ import RxSwift import RxCocoa import RxAlamofire -public let defaultTimeoutInterval = 20.0 - open class DefaultNetworkService: NetworkService { static let retryLimit = 3 - open class func baseUrl() -> String { - fatalError("base url should be overrided") + open class var baseUrl: String { + fatalError("You should override this var") + } + + open class var defaultTimeoutInterval: TimeInterval { + fatalError("You should override this var") } public override init(sessionManager: SessionManager) { @@ -46,22 +48,22 @@ open class DefaultNetworkService: NetworkService { // MARK: - Default Values - open class func serverTrustPolicies() -> [String: ServerTrustPolicy] { + open class var serverTrustPolicies: [String: ServerTrustPolicy] { return [ - DefaultNetworkService.baseUrl(): .disableEvaluation + DefaultNetworkService.baseUrl: .disableEvaluation ] } - open class func configuration() -> URLSessionConfiguration { + open class var configuration: URLSessionConfiguration { let configuration = URLSessionConfiguration.default configuration.timeoutIntervalForRequest = defaultTimeoutInterval return configuration } - open class func sessionManager() -> SessionManager { - let sessionManager = SessionManager(configuration: configuration(), - serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies())) + open class var sessionManager: SessionManager { + let sessionManager = SessionManager(configuration: configuration, + serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)) return sessionManager } @@ -71,7 +73,7 @@ extension ApiRequestParameters { init(url: String, parameters: [String: Any] = [:]) { - self.init(url: DefaultNetworkService.baseUrl() + url, + self.init(url: DefaultNetworkService.baseUrl + url, method: .post, parameters: parameters, encoding: JSONEncoding.default, diff --git a/LeadKitAdditions/LeadKitAdditions/Classes/SimpleNetworkService.swift b/LeadKitAdditions/LeadKitAdditions/Classes/SimpleNetworkService.swift new file mode 100644 index 0000000..2ed8156 --- /dev/null +++ b/LeadKitAdditions/LeadKitAdditions/Classes/SimpleNetworkService.swift @@ -0,0 +1,70 @@ +// +// Copyright (c) 2017 Touch Instinct +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the Software), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// + +import LeadKit +import Alamofire +import ObjectMapper +import RxSwift +import RxCocoa +import RxAlamofire + +open class SimpleNetworkService: DefaultNetworkService { + + // Singleton + static let shared = SimpleNetworkService() + + public convenience init() { + self.init(sessionManager: SimpleNetworkService.sessionManager) + } + + // MARK: - Public methods + + open func request(with parameters: ApiRequestParameters) -> Observable { + let apiResponseRequest = rxRequest(with: parameters) as Observable<(response: HTTPURLResponse, model: ApiResponse)> + + return apiResponseRequest + .handleConnectionErrors() + .map { + if $0.model.errorCode == 0 { + return try T(JSON: try cast($0.model.result) as [String: Any]) + } else { + throw ApiError(apiResponse: $0.model) + } + } + } + + open func requestForResult(with parameters: ApiRequestParameters) -> Observable { + let apiResponseRequest = rxRequest(with: parameters) as Observable<(response: HTTPURLResponse, model: ApiResponse)> + + return apiResponseRequest + .handleConnectionErrors() + .map { + if $0.model.errorCode == 0, + let result = $0.model.result as? Bool { + return result + } else { + throw ApiError(apiResponse: $0.model) + } + } + } + +}