From 50fdcdeb5cb53d9804654bec291e7353e9278bb0 Mon Sep 17 00:00:00 2001 From: Server Date: Mon, 28 Apr 2025 11:19:00 -0700 Subject: [PATCH] Allow async adaptRequest --- .../SimpleHTTP/Interceptor/CompositeInterceptor.swift | 9 ++++++--- Sources/SimpleHTTP/Interceptor/Interceptor.swift | 2 +- Sources/SimpleHTTP/Session/Session.swift | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Sources/SimpleHTTP/Interceptor/CompositeInterceptor.swift b/Sources/SimpleHTTP/Interceptor/CompositeInterceptor.swift index 4b1c168..8c489fd 100644 --- a/Sources/SimpleHTTP/Interceptor/CompositeInterceptor.swift +++ b/Sources/SimpleHTTP/Interceptor/CompositeInterceptor.swift @@ -15,10 +15,13 @@ public struct CompositeInterceptor: ExpressibleByArrayLiteral, Sequence { } extension CompositeInterceptor: Interceptor { - public func adaptRequest(_ request: Request) -> Request { - reduce(request) { request, interceptor in - interceptor.adaptRequest(request) + public func adaptRequest(_ request: Request) async -> Request { + var request = request + for interceptor in interceptors { + request = await interceptor.adaptRequest(request) } + + return request } public func shouldRescueRequest(_ request: Request, error: Error) async throws -> Bool { diff --git a/Sources/SimpleHTTP/Interceptor/Interceptor.swift b/Sources/SimpleHTTP/Interceptor/Interceptor.swift index be3a8be..e45df34 100644 --- a/Sources/SimpleHTTP/Interceptor/Interceptor.swift +++ b/Sources/SimpleHTTP/Interceptor/Interceptor.swift @@ -6,7 +6,7 @@ public typealias Interceptor = RequestInterceptor & ResponseInterceptor /// a protocol intercepting a session request public protocol RequestInterceptor { /// Should be called before making the request to provide modifications to `request` - func adaptRequest(_ request: Request) -> Request + func adaptRequest(_ request: Request) async -> Request /// catch and retry a failed request /// - Returns: nil if the request should not be retried. Otherwise a publisher that will be executed before diff --git a/Sources/SimpleHTTP/Session/Session.swift b/Sources/SimpleHTTP/Session/Session.swift index 9fb5873..ed64540 100644 --- a/Sources/SimpleHTTP/Session/Session.swift +++ b/Sources/SimpleHTTP/Session/Session.swift @@ -66,7 +66,7 @@ public class Session { extension Session { private func dataPublisher(for request: Request) async throws -> Response { - let modifiedRequest = config.interceptor.adaptRequest(request) + let modifiedRequest = await config.interceptor.adaptRequest(request) let urlRequest = try modifiedRequest .toURLRequest(encoder: config.encoder, relativeTo: baseURL, accepting: config.decoder)