Last active
May 23, 2021 07:12
-
-
Save bharathreddys77/f320df671d3fcce717955e7c9ae57e96 to your computer and use it in GitHub Desktop.
Network Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fileprivate enum ServiceType:String { | |
case GET,POST,PUT,DELETE | |
} | |
struct APIController:NetworkProtocol { | |
func verifyUserLogin(_ req: Loginrequest, completion: @escaping (LoginResponse?, NetworkError?) -> Void) { | |
networkRequestResult(urlString: Constants.API_NEW_PATIENT, type: .POST, header: nil, encodingData: req, completion: completion) | |
} | |
} | |
extension APIController { | |
private func networkRequestResult<T:Decodable,Q: Encodable>(urlString: String,type:PIServiceType,header:[String:String]?,encodingData: Q,completion: @escaping (Result<T,NetworkingControllerError>) -> Void) { | |
guard let url = URL(string: Constants.BASE_URL+urlString) else { | |
DispatchQueue.main.async { completion(.failure(NetworkingControllerError.invalidRequest)) } | |
return | |
} | |
var request = URLRequest(url: url) | |
request.httpMethod = type.rawValue | |
request.addValue("application/json", forHTTPHeaderField: "Accept") | |
request.addValue("application/json", forHTTPHeaderField: "Content-Type") | |
if type != .GET { | |
do { | |
let jsonBody = try JSONEncoder().encode(encodingData) | |
request.httpBody = jsonBody | |
} catch { | |
DispatchQueue.main.async { completion(.failure(NetworkingControllerError.invalidRequest)) } | |
return | |
} | |
} | |
let task = URLSession.shared.dataTask(with: request) { (data,response, err) in | |
if let e = err { | |
print("Failed to fetch data:", e.localizedDescription, "Error Description\(e)") | |
DispatchQueue.main.async { | |
completion(.failure(NetworkingControllerError.forwardedError(Error: e)))} | |
return | |
} | |
if let r = response { | |
let httpresponse = (r as! HTTPURLResponse) | |
print("statusCode \(httpresponse.statusCode)") | |
if httpresponse.statusCode != 200 { | |
DispatchQueue.main.async { | |
completion(.failure(NetworkingControllerError.noDataError)) | |
} | |
return | |
} | |
} | |
guard let data = data else { | |
DispatchQueue.main.async { | |
completion(.failure(NetworkingControllerError.noDataError))} | |
return | |
} | |
do { | |
let dataReceived = try JSONDecoder().decode(T.self, from: data) | |
DispatchQueue.main.async { | |
completion(.success(dataReceived)) | |
} | |
} catch let jsonErr { | |
print("page parse failed error \(jsonErr)") | |
DispatchQueue.main.async { | |
completion(.failure(NetworkingControllerError.dataParseError(error: jsonErr))) | |
} | |
} | |
} | |
task.resume() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum NetworkingControllerError:Error { | |
case Non200StatusCode | |
case invalidRequest | |
case NoInternetConnection | |
case forwardedError(Error:Error) | |
case noDataError | |
case apiError(error:NetworkError?) | |
case dataParseError(error:Error) | |
case customError(title:String?,message:String) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController:UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let loginRequest = Loginrequest(username: "bharath", password: "bharath") | |
APIController().verifyUserLogin(loginRequest) { (response, error) in | |
if let e = error { | |
print("Login API Error \(e.localizedDescription)") | |
switch e { | |
case .APINotFoundError: break | |
case .InputDataError: break | |
case .Non200StatusCode: break | |
default: | |
break | |
} | |
} else { | |
if let r = response { | |
if r.status ?? false { | |
print("login is success full") | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment