From 191a7aee3ada9268e913dd5432b8c755bc0d7d65 Mon Sep 17 00:00:00 2001 From: Javier Manzo Date: Mon, 21 Oct 2024 22:42:43 -0300 Subject: [PATCH 1/3] Correct how custom headers are set --- Sources/Harbor/HRequestManager.swift | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/Sources/Harbor/HRequestManager.swift b/Sources/Harbor/HRequestManager.swift index 330675f..ca02ffc 100644 --- a/Sources/Harbor/HRequestManager.swift +++ b/Sources/Harbor/HRequestManager.swift @@ -233,7 +233,7 @@ internal extension HRequestManager { var urlRequest = URLRequest(url: url) - urlRequest.allHTTPHeaderFields = getHeaderParameters(requestHeaderParameters: request.headerParameters) + urlRequest.httpMethod = request.httpMethod.rawValue // TODO: Move to a config class urlRequest.httpShouldHandleCookies = false @@ -250,6 +250,14 @@ internal extension HRequestManager { } } + if let defaultHeaders = Self.config.defaultHeaderParameters { + urlRequest.allHTTPHeaderFields = mergeHeaderParameters(currentHeaders: urlRequest.allHTTPHeaderFields, newHeaders: defaultHeaders) + } + + if let requestHeaderParameters = request.headerParameters { + urlRequest.allHTTPHeaderFields = mergeHeaderParameters(currentHeaders: urlRequest.allHTTPHeaderFields, newHeaders: requestHeaderParameters) + } + return urlRequest } @@ -314,14 +322,17 @@ internal extension HRequestManager { return fieldString } - static func getHeaderParameters(requestHeaderParameters: [String: String]? = nil) -> [String: String] { - var headers: [String: String] = Self.config.defaultHeaderParameters ?? [String: String]() + static func mergeHeaderParameters(currentHeaders: [String: String]?, newHeaders: [String: String]) -> [String: String] { + if let currentHeaders { + var headers: [String: String] = currentHeaders - if let requestHeaderParameters, !requestHeaderParameters.isEmpty { - headers.merge(requestHeaderParameters, uniquingKeysWith: { (_, new) in new }) + if !newHeaders.isEmpty { + headers.merge(newHeaders, uniquingKeysWith: { (_, new) in new }) + } + return headers + } else { + return newHeaders } - - return headers } } @@ -356,7 +367,7 @@ private extension HRequestManager { SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress) } } - + var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0) if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false { return false From e65878cdef14c2b053b6ea04e330ebe24b2b2492 Mon Sep 17 00:00:00 2001 From: Javier Manzo Date: Wed, 23 Oct 2024 10:05:06 -0300 Subject: [PATCH 2/3] Retry request when receives 401 statusCode --- Sources/Harbor/HRequestManager.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Sources/Harbor/HRequestManager.swift b/Sources/Harbor/HRequestManager.swift index 330675f..efd4e76 100644 --- a/Sources/Harbor/HRequestManager.swift +++ b/Sources/Harbor/HRequestManager.swift @@ -87,8 +87,10 @@ extension HRequestManager { case 401: if await !hasNewAuthorizationHeader(request: request) { Self.config.authProvider?.authFailed() + return .error(.authNeeded) + } else { + return await self.request(model: model, request: request) } - return .error(.authNeeded) default: if let retries = request.retries, retries > 0 { var mutableRequest = request @@ -186,8 +188,10 @@ extension HRequestManager { case 401: if await !hasNewAuthorizationHeader(request: request) { Self.config.authProvider?.authFailed() + return .error(.authNeeded) + } else { + return await self.request(request: request) } - return .error(.authNeeded) default: if let retries = request.retries, retries > 0 { var mutableRequest = request From 321496c1f788fcb0629aa21dddfc4ad6cd321235 Mon Sep 17 00:00:00 2001 From: Javier Manzo Date: Thu, 24 Oct 2024 00:25:39 -0300 Subject: [PATCH 3/3] Set authFailed method async to handle it in a safe way --- README.md | 6 +++++- Sources/Harbor/HAuthProviderProtocol.swift | 2 +- Sources/Harbor/HRequestManager.swift | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ef93ca4..0dd071b 100644 --- a/README.md +++ b/README.md @@ -104,9 +104,13 @@ You need to create a class that implements `HAuthProviderProtocol`: ```swift class MyAuthProvider: HAuthProviderProtocol { - func getCredentialHeader() -> HAuthorizationHeader { + func getAuthorizationHeader() async -> HAuthorizationHeader { // Return a HAuthorizationHeader instance } + + func authFailed() async { + // This method is called when the request receives a 401 status code + } } ``` diff --git a/Sources/Harbor/HAuthProviderProtocol.swift b/Sources/Harbor/HAuthProviderProtocol.swift index c328a42..93ffe1f 100644 --- a/Sources/Harbor/HAuthProviderProtocol.swift +++ b/Sources/Harbor/HAuthProviderProtocol.swift @@ -9,7 +9,7 @@ import Foundation public protocol HAuthProviderProtocol { func getAuthorizationHeader() async -> HAuthorizationHeader - func authFailed() + func authFailed() async } public struct HAuthorizationHeader { diff --git a/Sources/Harbor/HRequestManager.swift b/Sources/Harbor/HRequestManager.swift index ca02ffc..e582fd0 100644 --- a/Sources/Harbor/HRequestManager.swift +++ b/Sources/Harbor/HRequestManager.swift @@ -86,7 +86,7 @@ extension HRequestManager { } case 401: if await !hasNewAuthorizationHeader(request: request) { - Self.config.authProvider?.authFailed() + await Self.config.authProvider?.authFailed() } return .error(.authNeeded) default: @@ -185,7 +185,7 @@ extension HRequestManager { return .success case 401: if await !hasNewAuthorizationHeader(request: request) { - Self.config.authProvider?.authFailed() + await Self.config.authProvider?.authFailed() } return .error(.authNeeded) default: