diff --git a/KiwiPods.xcodeproj/project.pbxproj b/KiwiPods.xcodeproj/project.pbxproj index 9c4f318..1b1d12e 100644 --- a/KiwiPods.xcodeproj/project.pbxproj +++ b/KiwiPods.xcodeproj/project.pbxproj @@ -22,6 +22,7 @@ D66ADF5821D504B0004499F2 /* ImagePicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = D66ADF4C21D504AF004499F2 /* ImagePicker.swift */; }; D66ADF5921D504B0004499F2 /* LoadingCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = D66ADF4D21D504AF004499F2 /* LoadingCell.xib */; }; D66ADF5A21D504B0004499F2 /* LoadingCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D66ADF4E21D504B0004499F2 /* LoadingCell.swift */; }; + D69C64D121E334FF00E22013 /* NetworkModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D69C64D021E334FF00E22013 /* NetworkModel.swift */; }; D6B031612189D172003431C6 /* KiwiPods.h in Headers */ = {isa = PBXBuildFile; fileRef = D6B0315F2189D172003431C6 /* KiwiPods.h */; settings = {ATTRIBUTES = (Public, ); }; }; D6B031692189D2C9003431C6 /* NetworkManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6B031682189D2C9003431C6 /* NetworkManager.swift */; }; /* End PBXBuildFile section */ @@ -44,6 +45,7 @@ D66ADF4C21D504AF004499F2 /* ImagePicker.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImagePicker.swift; sourceTree = ""; }; D66ADF4D21D504AF004499F2 /* LoadingCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = LoadingCell.xib; sourceTree = ""; }; D66ADF4E21D504B0004499F2 /* LoadingCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LoadingCell.swift; sourceTree = ""; }; + D69C64D021E334FF00E22013 /* NetworkModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NetworkModel.swift; sourceTree = ""; }; D6B0315C2189D172003431C6 /* KiwiPods.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = KiwiPods.framework; sourceTree = BUILT_PRODUCTS_DIR; }; D6B0315F2189D172003431C6 /* KiwiPods.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KiwiPods.h; sourceTree = ""; }; D6B031602189D172003431C6 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -165,6 +167,7 @@ isa = PBXGroup; children = ( D6B031682189D2C9003431C6 /* NetworkManager.swift */, + D69C64D021E334FF00E22013 /* NetworkModel.swift */, ); path = Networking; sourceTree = ""; @@ -299,6 +302,7 @@ D66ADF5321D504B0004499F2 /* FacebookHandler.swift in Sources */, D66ADF5221D504B0004499F2 /* DeviceMediaHandler.swift in Sources */, D66ADF5121D504B0004499F2 /* TwitterHandler.swift in Sources */, + D69C64D121E334FF00E22013 /* NetworkModel.swift in Sources */, D66ADF4221D50488004499F2 /* UILinkLabel.swift in Sources */, D6B031692189D2C9003431C6 /* NetworkManager.swift in Sources */, D66ADF5021D504B0004499F2 /* FacebookLoginHelper.swift in Sources */, diff --git a/KiwiPods/Networking/NetworkManager.swift b/KiwiPods/Networking/NetworkManager.swift index 2905f95..ff85a6b 100644 --- a/KiwiPods/Networking/NetworkManager.swift +++ b/KiwiPods/Networking/NetworkManager.swift @@ -9,181 +9,6 @@ import UIKit import Alamofire -public enum RequestType: String { - case POST, - GET, - DELETE, - PUT - var httpMethod: HTTPMethod { - switch self { - case .POST: - return HTTPMethod.post - case .GET: - return HTTPMethod.get - case .DELETE: - return HTTPMethod.delete - case .PUT: - return HTTPMethod.put - } - } -} - -//using our own URLRequestConvertible so that networking library can be updated easily -public protocol URLRequestConvertible { - func asURLRequest() -> URLRequest? -} - -public protocol APIConfigurable: URLRequestConvertible { - var type: RequestType { get } - var path: String { get } - var parameters: [String: Any] { get } - var headers: [String: String]? { get } -} - -public extension APIConfigurable { - public func asURLRequest() -> URLRequest? { - var queryItems = "" - if type == .GET, parameters.count > 0 { - queryItems = parameters.reduce("?") { (value: String, arg1: (String, Any)) -> String in - return value + "\(arg1.0)=\(arg1.1)&" - } - queryItems.removeLast() - } - let url = URL(string: (path + queryItems)) - do { - var urlRequest = try URLRequest(url: url!, method: type.httpMethod) - urlRequest.allHTTPHeaderFields = headers - if type != .GET { - urlRequest.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: JSONSerialization.WritingOptions.prettyPrinted) - } - return urlRequest - } catch { - print(error.localizedDescription) - return nil - } - } -} -public protocol ParameterConvertible: Codable { - static func objectFrom(json: Any, decoder: JSONDecoder)throws -> ParameterConvertible? - func toParams()throws -> [String: Any]? -} -extension ParameterConvertible { - public static func objectFrom(json: Any, decoder: JSONDecoder) throws -> ParameterConvertible? { - do { - let data = try JSONSerialization.data(withJSONObject: json, options: JSONSerialization.WritingOptions.prettyPrinted) - let jsonModel = try decoder.decode(self, from: data) - return jsonModel - } catch let error { - throw error - } - } - public func toParams()throws -> [String: Any]? { - do { - let data = try JSONEncoder().encode(self) - if let dict = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String: Any] { - return dict - } - return nil - } catch let error { - - throw error - } - } -} -public enum Response where ResponseType: ParameterConvertible { - public struct ResponseError { - public let error: Error? - public let statusCode: Int? - public let errorValue: [String: Any]? - public init(error: Error?, statusCode: Int?, errorValue: [String: Any]? = nil) { - self.error = error - self.statusCode = statusCode - self.errorValue = errorValue - } - public func errorMessage() -> String? - { - if let dict = errorValue - { - if let errors = dict["errors"] as? [String: Any], let error = errors["error"] as? String, error.count > 0 - { - return error - } - - if let error = dict["status"] as? String, error.count > 0 - { - return error - } - - if let usernameDict = dict["username"] as? [String: Any], let name = usernameDict["name"] as? String, name == "ValidatorError" - { - return "Username is already in use" - } - - if let emailDict = dict["email"] as? [String: Any], let name = emailDict["name"] as? String, name == "ValidatorError" - { - return "Username is already in use" - } - } - - return nil - } - } - public struct ResponseValue { - let value: ResponseType - let statusCode: Int? - } - case success(ResponseValue), - failed(ResponseError) - - public var responseError: ResponseError? { - switch self { - case .success: - return nil - case .failed(let value): - return value - } - } - - public var value: ResponseType? { - switch self { - case .success(let value): - return value.value - case .failed: - return nil - } - } - public var error: Error? { - switch self { - case .failed(let error): - return error.error - case .success: - return nil - } - } - public var statusCode: Int? { - switch self { - case .success(let response): - return response.statusCode - case .failed(let response): - return response.statusCode - } - } - public var isSuccess: Bool { - switch self { - case .success: - return true - case .failed: - return false - } - } -} -public enum APIErrors: Error { - case noDataRecieved, - parserError, - oauthTokenError, - invalidRequest(String), - cancelled -} open class NetworkManager: NSObject { static public let shared = NetworkManager() private override init() { diff --git a/KiwiPods/Networking/NetworkModel.swift b/KiwiPods/Networking/NetworkModel.swift new file mode 100644 index 0000000..cf7a0da --- /dev/null +++ b/KiwiPods/Networking/NetworkModel.swift @@ -0,0 +1,189 @@ +// +// NetworkModel.swift +// topfan-3-ios +// +// Created by Ayush Awasthi on 28/11/18. +// Copyright © 2018 KiwiTech. All rights reserved. +// + +import UIKit +import Alamofire + +public enum RequestType: String { + case POST, + GET, + DELETE, + PUT + public var httpMethod: HTTPMethod { + switch self { + case .POST: + return HTTPMethod.post + case .GET: + return HTTPMethod.get + case .DELETE: + return HTTPMethod.delete + case .PUT: + return HTTPMethod.put + } + } +} + +//using our own URLRequestConvertible so that networking library can be updated easily +public protocol URLRequestConvertible { + func asURLRequest() -> URLRequest? +} + +public protocol APIConfigurable: URLRequestConvertible { + var type: RequestType { get } + var path: String { get } + var parameters: [String: Any] { get } + var headers: [String: String]? { get } +} + +public extension APIConfigurable { + public func asURLRequest() -> URLRequest? { + var queryItems = "" + if type == .GET, parameters.count > 0 { + queryItems = parameters.reduce("?") { (value: String, arg1: (String, Any)) -> String in + return value + "\(arg1.0)=\(arg1.1)&" + } + queryItems.removeLast() + } + let url = URL(string: (path + queryItems)) + do { + var urlRequest = try URLRequest(url: url!, method: type.httpMethod) + urlRequest.allHTTPHeaderFields = headers + if type != .GET { + urlRequest.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: JSONSerialization.WritingOptions.prettyPrinted) + } + urlRequest.cachePolicy = .reloadIgnoringCacheData + return urlRequest + } catch { + print(error.localizedDescription) + return nil + } + } +} +public protocol ParameterConvertible: Codable { + static func objectFrom(json: Any, decoder: JSONDecoder)throws -> ParameterConvertible? + func toParams()throws -> [String: Any]? +} +public extension ParameterConvertible { + static public func objectFrom(json: Any, decoder: JSONDecoder) throws -> ParameterConvertible? { + do { + let data = try JSONSerialization.data(withJSONObject: json, options: JSONSerialization.WritingOptions.prettyPrinted) + let jsonModel = try decoder.decode(self, from: data) + return jsonModel + } catch let error { + throw error + } + } + public func toParams()throws -> [String: Any]? { + do { + let data = try JSONEncoder().encode(self) + if let dict = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String: Any] { + return dict + } + return nil + } catch let error { + throw error + } + } +} +public enum Response where ResponseType: ParameterConvertible { + public struct ResponseError { + public let error: Error? + public let statusCode: Int? + public let errorValue: [String: Any]? + public init(error: Error?, statusCode: Int?, errorValue: [String: Any]? = nil) { + self.error = error + self.statusCode = statusCode + self.errorValue = errorValue + } + public func errorMessage() -> String? + { + if let dict = errorValue + { + if let error = dict["error"] as? String { + return error + } + if let errors = dict["errors"] as? [String: Any], let error = errors["error"] as? String, error.count > 0 + { + return error + } + + if let error = dict["status"] as? String, error.count > 0 + { + return error + } + + if let usernameDict = dict["username"] as? [String: Any], let name = usernameDict["name"] as? String, name == "ValidatorError" + { + return "Username is already in use" + } + + if let emailDict = dict["email"] as? [String: Any], let name = emailDict["name"] as? String, name == "ValidatorError" + { + return "Username is already in use" + } + } + + return nil + } + } + public struct ResponseValue { + public let value: ResponseType + public let statusCode: Int? + } + case success(ResponseValue), + failed(ResponseError) + + public var responseError: ResponseError? { + switch self { + case .success: + return nil + case .failed(let value): + return value + } + } + + public var value: ResponseType? { + switch self { + case .success(let value): + return value.value + case .failed: + return nil + } + } + public var error: Error? { + switch self { + case .failed(let error): + return error.error + case .success: + return nil + } + } + public var statusCode: Int? { + switch self { + case .success(let response): + return response.statusCode + case .failed(let response): + return response.statusCode + } + } + public var isSuccess: Bool { + switch self { + case .success: + return true + case .failed: + return false + } + } +} +public enum APIErrors: Error { + case noDataRecieved, + parserError, + oauthTokenError, + invalidRequest(String), + cancelled +}