@@ -20,7 +20,7 @@ public enum QoderProviderDescriptor {
2020 defaultEnabled: false ,
2121 isPrimaryProvider: false ,
2222 usesAccountFallback: false ,
23- browserCookieOrder: ProviderBrowserCookieDefaults . defaultImportOrder ,
23+ browserCookieOrder: ProviderBrowserCookieDefaults . qoderCookieImportOrder ,
2424 dashboardURL: " https://qoder.com/account/usage " ,
2525 statusPageURL: nil ,
2626 statusLinkURL: nil ) ,
@@ -46,7 +46,11 @@ struct QoderWebFetchStrategy: ProviderFetchStrategy {
4646 let kind : ProviderFetchKind = . web
4747
4848 func isAvailable( _ context: ProviderFetchContext ) async -> Bool {
49- guard context. settings? . qoder? . cookieSource != . off else { return false }
49+ let cookieSource = context. settings? . qoder? . cookieSource ?? . auto
50+ guard cookieSource != . off else { return false }
51+ if cookieSource == . manual {
52+ return CookieHeaderNormalizer . normalize ( context. settings? . qoder? . manualCookieHeader) != nil
53+ }
5054 #if os(macOS)
5155 return true
5256 #else
@@ -55,26 +59,35 @@ struct QoderWebFetchStrategy: ProviderFetchStrategy {
5559 }
5660
5761 func fetch( _ context: ProviderFetchContext ) async throws -> ProviderFetchResult {
58- #if os(macOS)
5962 let cookieSource = context. settings? . qoder? . cookieSource ?? . auto
63+ var attemptedSourceLabel : String ?
6064 do {
61- let ( cookieHeader, sourceLabel) = try Self . resolveCookieHeader ( context: context, allowCached: true )
62- let snapshot = try await QoderUsageFetcher . fetchUsage ( cookieHeader: cookieHeader)
65+ let ( cookieHeader, sourceLabel) = try Self . resolveCookieHeader (
66+ context: context,
67+ allowCached: true ,
68+ skippingSourceLabel: nil )
69+ attemptedSourceLabel = sourceLabel
70+ let snapshot = try await QoderUsageFetcher . fetchUsage (
71+ cookieHeader: cookieHeader,
72+ timeout: context. webTimeout)
6373 return self . makeResult ( usage: snapshot. toUsageSnapshot ( ) , sourceLabel: sourceLabel)
6474 } catch QoderUsageError . invalidCredentials where cookieSource != . manual {
6575 CookieHeaderCache . clear ( provider: . qoder)
66- let ( cookieHeader, sourceLabel) = try Self . resolveCookieHeader ( context: context, allowCached: false )
67- let snapshot = try await QoderUsageFetcher . fetchUsage ( cookieHeader: cookieHeader)
76+ let ( cookieHeader, sourceLabel) = try Self . resolveCookieHeader (
77+ context: context,
78+ allowCached: false ,
79+ skippingSourceLabel: attemptedSourceLabel)
80+ let snapshot = try await QoderUsageFetcher . fetchUsage (
81+ cookieHeader: cookieHeader,
82+ timeout: context. webTimeout)
6883 return self . makeResult ( usage: snapshot. toUsageSnapshot ( ) , sourceLabel: sourceLabel)
6984 }
70- #else
71- throw QoderUsageError . missingCredentials
72- #endif
7385 }
7486
7587 private static func resolveCookieHeader(
7688 context: ProviderFetchContext ,
77- allowCached: Bool ) throws -> ( cookieHeader: String , sourceLabel: String )
89+ allowCached: Bool ,
90+ skippingSourceLabel: String ? ) throws -> ( cookieHeader: String , sourceLabel: String )
7891 {
7992 if context. settings? . qoder? . cookieSource == . manual {
8093 guard let manual = CookieHeaderNormalizer . normalize ( context. settings? . qoder? . manualCookieHeader) else {
@@ -86,17 +99,24 @@ struct QoderWebFetchStrategy: ProviderFetchStrategy {
8699 #if os(macOS)
87100 if allowCached,
88101 let cached = CookieHeaderCache . load ( provider: . qoder) ,
89- !cached. cookieHeader. trimmingCharacters ( in: . whitespacesAndNewlines) . isEmpty
102+ !cached. cookieHeader. trimmingCharacters ( in: . whitespacesAndNewlines) . isEmpty,
103+ shouldUseSourceLabel ( cached. sourceLabel, skipping: skippingSourceLabel)
90104 {
91- return ( cached. cookieHeader, " cached " )
105+ return ( cached. cookieHeader, cached. sourceLabel )
92106 }
93107
94- let session : QoderCookieImporter . SessionInfo
108+ let sessions : [ QoderCookieImporter . SessionInfo ]
95109 do {
96- session = try QoderCookieImporter . importSession ( browserDetection: context. browserDetection)
110+ sessions = try QoderCookieImporter . importSessions ( browserDetection: context. browserDetection)
97111 } catch {
98112 throw QoderUsageError . missingCredentials
99113 }
114+ guard let session = sessions. first ( where: { Self . shouldUseSourceLabel (
115+ $0. sourceLabel,
116+ skipping: skippingSourceLabel) } )
117+ else {
118+ throw QoderUsageError . missingCredentials
119+ }
100120 guard !session. cookies. isEmpty else {
101121 throw QoderUsageError . missingCredentials
102122 }
@@ -110,6 +130,11 @@ struct QoderWebFetchStrategy: ProviderFetchStrategy {
110130 #endif
111131 }
112132
133+ private static func shouldUseSourceLabel( _ sourceLabel: String , skipping skippedLabel: String ? ) -> Bool {
134+ guard let skippedLabel else { return true }
135+ return sourceLabel != skippedLabel
136+ }
137+
113138 func shouldFallback( on _: Error , context _: ProviderFetchContext ) -> Bool {
114139 false
115140 }
0 commit comments