Skip to content

Commit

Permalink
Merge pull request #1 from KiwiTechLLC/networkManagerChanges
Browse files Browse the repository at this point in the history
Network manager changes
  • Loading branch information
anuj-gola authored Jan 7, 2019
2 parents 622bd67 + 2e7e2df commit cbc9e5d
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 175 deletions.
4 changes: 4 additions & 0 deletions KiwiPods.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -44,6 +45,7 @@
D66ADF4C21D504AF004499F2 /* ImagePicker.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImagePicker.swift; sourceTree = "<group>"; };
D66ADF4D21D504AF004499F2 /* LoadingCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = LoadingCell.xib; sourceTree = "<group>"; };
D66ADF4E21D504B0004499F2 /* LoadingCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LoadingCell.swift; sourceTree = "<group>"; };
D69C64D021E334FF00E22013 /* NetworkModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NetworkModel.swift; sourceTree = "<group>"; };
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 = "<group>"; };
D6B031602189D172003431C6 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -165,6 +167,7 @@
isa = PBXGroup;
children = (
D6B031682189D2C9003431C6 /* NetworkManager.swift */,
D69C64D021E334FF00E22013 /* NetworkModel.swift */,
);
path = Networking;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down
175 changes: 0 additions & 175 deletions KiwiPods/Networking/NetworkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResponseType> 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() {
Expand Down
Loading

0 comments on commit cbc9e5d

Please sign in to comment.