Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release/1.0.1 #28

Merged
merged 7 commits into from
Oct 26, 2024
Merged
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
```

Expand Down
2 changes: 1 addition & 1 deletion Sources/Harbor/HAuthProviderProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation

public protocol HAuthProviderProtocol {
func getAuthorizationHeader() async -> HAuthorizationHeader
func authFailed()
func authFailed() async
}

public struct HAuthorizationHeader {
Expand Down
39 changes: 27 additions & 12 deletions Sources/Harbor/HRequestManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ extension HRequestManager {
}
case 401:
if await !hasNewAuthorizationHeader(request: request) {
Self.config.authProvider?.authFailed()
await 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
Expand Down Expand Up @@ -185,9 +187,11 @@ extension HRequestManager {
return .success
case 401:
if await !hasNewAuthorizationHeader(request: request) {
Self.config.authProvider?.authFailed()
await 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
Expand Down Expand Up @@ -233,7 +237,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
Expand All @@ -250,6 +254,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
}

Expand Down Expand Up @@ -314,14 +326,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
}
}

Expand Down Expand Up @@ -356,7 +371,7 @@ private extension HRequestManager {
SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
}
}

var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
return false
Expand Down