2 // LoggeableRequest.swift
3 // alamofire_activity_logger
5 // Created by Manu on 30/5/16.
6 // Copyright © 2016 manuege. All rights reserved.
12 A struct that put together the relevant info from a Response
14 public struct ResponseInfo {
15 public var httpResponse: HTTPURLResponse?
16 public var data: Data?
17 public var error: Error?
18 public var elapsedTime: TimeInterval
22 Make a Request conform this protocol to be able to log its request/response
24 public protocol LoggeableRequest: AnyObject {
27 var request: URLRequest? { get }
30 Use this method to fetch the info needed to buld a `ResponseInfo` instance. Once the `ResponseInfo` has been build, you must call the `completion` parameter .
31 - parameter completion: The block that must be called when the asynchronous process has finished.
33 func fetchResponseInfo(completion: @escaping (ResponseInfo) -> Void)
36 public extension LoggeableRequest {
39 Log the request and response with the given level and options
41 public func log(level: LogLevel = .all, options: [LogOption] = LogOption.defaultOptions, printer: Printer = NativePrinter()) -> Self {
43 guard level != .none else {
47 let debugOption = options.contains(.onlyDebug)
48 if debugOption && !appIsDebugMode {
52 Logger.logRequest(request: request, level: level, options: options, printer: printer)
53 fetchResponseInfo { response in
54 Logger.logResponse(request: self.request, response: response, level: level, options: options, printer: printer)
62 extension DataRequest: LoggeableRequest {
63 public func fetchResponseInfo(completion: @escaping (ResponseInfo) -> Void) {
65 responseData { (response) in
67 var error: Error? = nil
68 if case .failure(let e) = response.result {
72 let logResponse = ResponseInfo(httpResponse: response.response,
75 elapsedTime: response.timeline.requestDuration)
76 completion(logResponse)
82 extension DownloadRequest: LoggeableRequest {
83 public func fetchResponseInfo(completion: @escaping (ResponseInfo) -> Void) {
85 responseData { (response) in
87 var error: Error? = nil
90 switch response.result {
91 case let .success(value):
93 case let .failure(value):
97 let logResponse = ResponseInfo(httpResponse: response.response,
100 elapsedTime: response.timeline.requestDuration)
101 completion(logResponse)