Last active
June 16, 2020 20:27
-
-
Save furkankaplan/2dcf4e64dd064efa2e6230eb6e5a5028 to your computer and use it in GitHub Desktop.
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
// | |
// NetworkManager.swift | |
// kanban3d-ios | |
// | |
// Created by Furkan Kaplan on 15.12.2019. | |
// Copyright © 2019 Furkan Kaplan. All rights reserved. | |
// | |
import Foundation | |
enum RequestType: String { | |
case POST = "POST" | |
case GET = "GET" | |
} | |
class NetworkManager { | |
static let shared: NetworkManager = NetworkManager() | |
func request<T: Decodable, T2, T3: Encodable>(selfReferance: T2.Type, model: T.Type?, type: RequestType, url: String, parameters: T3, completion: @escaping ((_ isSuccess: Bool, _ errorMessage: String?, _ data: T?) -> Void)) { | |
let session = URLSession.shared | |
var urlRequest: URLRequest = URLRequest(url: URL(string: url)!) | |
do { | |
let encoder = JSONEncoder() | |
encoder.dateEncodingStrategy = .iso8601 | |
urlRequest.httpBody = try encoder.encode(parameters) | |
} catch { | |
print("Error during json encoding") | |
} | |
urlRequest.httpMethod = RequestType.POST.rawValue | |
urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type") | |
let task = session.dataTask(with: urlRequest, completionHandler: { data, response, error in | |
// Check the response | |
self.printDivider() | |
print("Network Request Starts") | |
self.printDivider() | |
print("File: \(T2.self)") | |
print("Model: \(T.self)") | |
guard let response = response else { | |
print("Response is nil") | |
self.printDivider() | |
return | |
} | |
print("Url: \(response.url!)") | |
// Check if an error occured | |
if let error = error { | |
// HERE you can manage the error | |
print("Error: \(error.localizedDescription)") | |
self.printDivider() | |
return | |
} | |
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { | |
print("Status code: \(httpStatus.statusCode)") | |
do { | |
let json: ResponseModel<T> = try JSONDecoder().decode(ResponseModel<T>.self, from: data!) | |
print("Error message: \(json.error!.message)") | |
self.printDivider() | |
completion(false, json.error!.message, nil) | |
} catch { | |
print("Error during error JSON serialization: \(error.localizedDescription)") | |
} | |
return | |
} | |
// Serialize the data into an object | |
do { | |
print("Status code: 200") | |
print("Data: \(String(data: data!, encoding: .utf8)!)") | |
self.printDivider() | |
let json: ResponseModel<T> = try JSONDecoder().decode(ResponseModel<T>.self, from: data!) | |
completion(true, nil, json.data) | |
} catch { | |
print("Error during JSON serialization: \(error.localizedDescription)") | |
} | |
}) | |
task.resume() | |
} | |
func printDivider() { | |
print("############################") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment