Skip to content

Commit

Permalink
refactor loggingFn -> logHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahKamara authored Mar 19, 2024
1 parent 91dbf13 commit 1f2dc6a
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions PapyrusCore/Sources/Interceptors/CurlInterceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ public struct CurlLogger {
case onError
}

let loggingFn: (String) -> Void
let logHandler: (String) -> Void
let condition: Condition

/// An `Interceptor` that calls a loggingFn with a request based on a condition
/// An `Interceptor` that calls a logHandler with a request based on a condition
/// - Parameters:
/// - condition: must be met for the logging function to be called
/// - loggingFn: a function that implements logging. defaults to `print()`
public init(when condition: Condition, using loggingFn: @escaping (String) -> Void = { print($0) }) {
/// - logHandler: a function that implements logging. defaults to `print()`
public init(when condition: Condition, using logHandler: @escaping (String) -> Void = { print($0) }) {
self.condition = condition
self.loggingFn = loggingFn
self.logHandler = logHandler
}
}

extension CurlLogger: Interceptor {
public func intercept(req: any Request, next: (any Request) async throws -> any Response) async throws -> any Response {
if condition == .always {
loggingFn(req.curl(sortedHeaders: true))
logHandler(req.curl(sortedHeaders: true))
}

do {
let res = try await next(req)
return res
} catch {
if condition == .onError {
loggingFn(req.curl(sortedHeaders: true))
logHandler(req.curl(sortedHeaders: true))
}
throw error
}
Expand Down

0 comments on commit 1f2dc6a

Please sign in to comment.