This file contains hidden or 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 NetworkEventLogger: EventMonitor { | |
let queue = DispatchQueue(label: "com...") | |
... | |
} |
This file contains hidden or 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
let monitor = NetworkEventMonitor() | |
let session = Session(eventMonitors: [monitor]) |
This file contains hidden or 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 NetworkEventLogger: EventMonitor { | |
func request<Value>(_ request: DataRequest, didParseResponse response: DataResponse<Value, AFError>) { | |
print("\n") | |
print("🛰 NetworkEventLogger - Response") | |
print( | |
"∘ URL: \(request.request?.url?.absoluteString ?? "") \n" + | |
"∘ StatusCode: \(response.response?.statusCode ?? 0) \n" + | |
"∘ Duration: \(response.metrics?.taskInterval.duration ?? 0) \n" | |
) | |
This file contains hidden or 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 NetworkEventLogger: EventMonitor { | |
func requestDidResume(_ request: Request) { | |
print("\n") | |
print("🚀 NetworkEventLogger - Request") | |
print( | |
"∘ URL: \(request.request?.url?.absoluteString ?? "") \n" + | |
"∘ Headers: \(request.request?.allHTTPHeaderFields ?? [:]) \n" + | |
"∘ Method: \(request.request?.httpMethod ?? "") \n" | |
) | |
This file contains hidden or 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
public protocol EventMonitor { | |
... | |
func requestDidResume(_ request: Request) | |
func request<Value>(_ request: DataRequest, didParseResponse response: DataResponse<Value, AFError>) | |
... | |
} |
This file contains hidden or 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
print("URL : \(response.result?.url?.absoluteString ?? "")") | |
print("Status code : \(response.response?.statusCode ?? 0)") | |
if let object = try? JSONSerialization.jsonObject(with: self, options: [.mutableContainers]), | |
let data = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted]), | |
let prettyPrintedString = String(data: data, encoding: .utf8) else { | |
print("Response data : \(prettyPrintedString") | |
} |
This file contains hidden or 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
public let AF = Session.default |
This file contains hidden or 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
let interceptor = Interceptor() | |
let session = Session(interceptor: interceptor) | |
session.request(Router.getProducts(pageSize: pageSize, page: page)) | |
.responseDecodable(of: [Product].self) { response in | |
... | |
} |
This file contains hidden or 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 Interceptor: RequestInterceptor { | |
func adapt(_ urlRequest: URLRequest, | |
for session: Session, | |
completion: @escaping (Result<URLRequest, Error>)) -> Void { | |
var urlRequest = urlRequest | |
if let accessToken = KeychainService.loadAccessToken() { | |
request.headers.add(.authorization(bearerToken: accessToken)) | |
completion(.success(urlRequest)) | |
} else { |
This file contains hidden or 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
func getProducts(pageSize: Int, | |
page: Int, | |
completion: @escaping (Result<[Product], Error>) -> Void) { | |
AF.request(Router.getProducts(pageSize: pageSize, page: page)) | |
.responseDecodable(of: [Product].self) { response in | |
... | |
} | |
} |
NewerOlder