Skip to content

Commit

Permalink
typealias added for completion and response error for cleaner code,…
Browse files Browse the repository at this point in the history
… also, error string code reduced to some common errors
  • Loading branch information
Ayush Awasthi committed Jan 16, 2019
1 parent 4f715b0 commit fe7f9f6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
10 changes: 5 additions & 5 deletions KiwiPods/Networking/NetworkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

import UIKit
import Alamofire

public typealias Completion<Model: ParameterConvertible> = (Response<Model>) -> Void
open class NetworkManager: NSObject {
static public let shared = NetworkManager()
private override init() {
super.init()
}
public func hitApi<ModelClass: ParameterConvertible>(urlRequest: URLRequest, decoder: JSONDecoder = JSONDecoder(), completion: @escaping (Response<ModelClass>) -> Void) {
public func hitApi<ModelClass: ParameterConvertible>(urlRequest: URLRequest, decoder: JSONDecoder = JSONDecoder(), completion: @escaping Completion<ModelClass>) {
Alamofire.request(urlRequest).validate().responseJSON { (response) in

var errorValue: [String: Any]? = [:]
Expand All @@ -34,13 +34,13 @@ open class NetworkManager: NSObject {
/*let className = String(describing: ModelClass.self)
fatalError("Can not parse response in provided model type: \(className)")*/

completion(Response.failed(Response.ResponseError(error: APIErrors.parserError, statusCode: response.response?.statusCode, errorValue: errorValue)))
completion(Response.failed(APIError(error: APIErrors.parserError, statusCode: response.response?.statusCode, errorValue: errorValue)))
}
} catch let error {
completion(Response.failed(Response.ResponseError(error: error, statusCode: response.response?.statusCode, errorValue: errorValue)))
completion(Response.failed(APIError(error: error, statusCode: response.response?.statusCode, errorValue: errorValue)))
}
case .failure(let error):
completion(Response.failed(Response.ResponseError(error: error, statusCode: response.response?.statusCode, errorValue: errorValue)))
completion(Response.failed(APIError(error: error, statusCode: response.response?.statusCode, errorValue: errorValue)))
}
}
}
Expand Down
13 changes: 1 addition & 12 deletions KiwiPods/Networking/NetworkModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public extension ParameterConvertible {
}
}
}
public typealias APIError<Type: ParameterConvertible> = Response<Type>.ResponseError
public enum Response<ResponseType> where ResponseType: ParameterConvertible {
public struct ResponseError {
public let error: Error?
Expand All @@ -109,23 +110,11 @@ public enum Response<ResponseType> where ResponseType: ParameterConvertible {
{
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
}
}
Expand Down

0 comments on commit fe7f9f6

Please sign in to comment.