@@ -14,10 +14,9 @@ final class GoFeatureFlagProvider: FeatureProvider {
14
14
private let ofrepAPI : OfrepAPI
15
15
16
16
private var inMemoryCache : [ String : OfrepEvaluationResponseFlag ] = [ : ]
17
- private var apiRetryAfter : Date ? = nil
17
+ private var apiRetryAfter : Date ?
18
18
private var timer : DispatchSourceTimer ?
19
19
20
-
21
20
init ( options: GoFeatureFlagProviderOptions ) {
22
21
self . options = options
23
22
@@ -29,8 +28,6 @@ final class GoFeatureFlagProvider: FeatureProvider {
29
28
self . ofrepAPI = OfrepAPI ( networkingService: networkService, options: self . options)
30
29
}
31
30
32
-
33
-
34
31
func observe( ) -> AnyPublisher < OpenFeature . ProviderEvent , Never > {
35
32
return eventHandler. observe ( )
36
33
}
@@ -47,7 +44,7 @@ final class GoFeatureFlagProvider: FeatureProvider {
47
44
self . startPolling ( pollInterval: self . options. pollInterval)
48
45
}
49
46
50
- if status == . success_with_changes {
47
+ if status == . successWithChanges {
51
48
self . eventHandler. send ( . ready)
52
49
return
53
50
}
@@ -64,25 +61,24 @@ final class GoFeatureFlagProvider: FeatureProvider {
64
61
self . eventHandler. send ( . stale)
65
62
self . evaluationContext = newContext
66
63
Task {
67
- do {
64
+ do {
68
65
let status = try await self . evaluateFlags ( context: newContext)
69
- if ( status == . success_with_changes || status == . success_no_changes ) {
66
+ if ( status == . successWithChanges || status == . successNoChanges ) {
70
67
self . eventHandler. send ( . ready)
71
68
}
72
- } catch let err as OfrepError {
73
- switch err {
69
+ } catch let error as OfrepError {
70
+ switch error {
74
71
case . apiTooManyRequestsError:
75
72
return // we want to stay stale in that case so we ignore the error.
76
73
default :
77
- throw err
74
+ throw error
78
75
}
79
76
} catch {
80
77
self . eventHandler. send ( . error)
81
78
}
82
79
}
83
80
}
84
81
85
-
86
82
func getBooleanEvaluation( key: String , defaultValue: Bool ,
87
83
context: EvaluationContext ? ) throws -> ProviderEvaluation < Bool > {
88
84
let flagCached = try genericEvaluation ( key: key)
@@ -95,14 +91,13 @@ final class GoFeatureFlagProvider: FeatureProvider {
95
91
reason: flagCached. reason)
96
92
}
97
93
98
-
99
94
private func genericEvaluation( key: String ) throws -> OfrepEvaluationResponseFlag {
100
95
guard let flagCached = self . inMemoryCache [ key] else {
101
96
throw OpenFeatureError . flagNotFoundError ( key: key)
102
97
}
103
98
104
99
if flagCached. isError ( ) {
105
- switch flagCached. errorCode{
100
+ switch flagCached. errorCode {
106
101
case . flagNotFound:
107
102
throw OpenFeatureError . flagNotFoundError ( key: key)
108
103
case . invalidContext:
@@ -123,7 +118,6 @@ final class GoFeatureFlagProvider: FeatureProvider {
123
118
return flagCached
124
119
}
125
120
126
-
127
121
func getStringEvaluation( key: String , defaultValue: String ,
128
122
context: EvaluationContext ? ) throws -> ProviderEvaluation < String > {
129
123
let flagCached = try genericEvaluation ( key: key)
@@ -186,7 +180,7 @@ final class GoFeatureFlagProvider: FeatureProvider {
186
180
187
181
if arrayValue != nil {
188
182
var convertedValue : [ Value ] = [ ]
189
- arrayValue? . forEach { item in
183
+ arrayValue? . forEach { item in
190
184
convertedValue. append ( item. toValue ( ) )
191
185
}
192
186
return ProviderEvaluation < Value > (
@@ -200,17 +194,17 @@ final class GoFeatureFlagProvider: FeatureProvider {
200
194
private func evaluateFlags( context: EvaluationContext ? ) async throws -> BulkEvaluationStatus {
201
195
if self . apiRetryAfter != nil && self . apiRetryAfter! > Date ( ) {
202
196
// we don't want to call the API because we got a 429
203
- return BulkEvaluationStatus . rate_limited
197
+ return BulkEvaluationStatus . rateLimited
204
198
}
205
199
206
200
do {
207
201
let ( ofrepEvalResponse, httpResp) = try await self . ofrepAPI. postBulkEvaluateFlags ( context: context)
208
202
209
203
if httpResp. statusCode == 304 {
210
- return BulkEvaluationStatus . success_no_changes
204
+ return BulkEvaluationStatus . successNoChanges
211
205
}
212
206
213
- if ofrepEvalResponse. isError ( ) {
207
+ if ofrepEvalResponse. isError ( ) {
214
208
switch ofrepEvalResponse. errorCode {
215
209
case . providerNotReady:
216
210
throw OpenFeatureError . providerNotReadyError
@@ -232,9 +226,9 @@ final class GoFeatureFlagProvider: FeatureProvider {
232
226
}
233
227
}
234
228
self . inMemoryCache = inMemoryCacheNew
235
- return BulkEvaluationStatus . success_with_changes
236
- } catch let error as OfrepError {
237
- switch error{
229
+ return BulkEvaluationStatus . successWithChanges
230
+ } catch let error as OfrepError {
231
+ switch error {
238
232
case . apiTooManyRequestsError( let response) :
239
233
self . apiRetryAfter = getRetryAfterDate ( from: response. allHeaderFields)
240
234
throw error
@@ -269,25 +263,22 @@ final class GoFeatureFlagProvider: FeatureProvider {
269
263
return dateFormatter. date ( from: retryAfterValue)
270
264
}
271
265
272
-
273
-
274
-
275
266
func startPolling( pollInterval: TimeInterval ) {
276
267
timer = DispatchSource . makeTimerSource ( queue: DispatchQueue . global ( ) )
277
268
timer? . schedule ( deadline: . now( ) , repeating: pollInterval, leeway: . milliseconds( 100 ) )
278
269
timer? . setEventHandler { [ weak self] in
279
270
guard let weakSelf = self else { return }
280
271
Task {
281
- do {
272
+ do {
282
273
let status = try await weakSelf. evaluateFlags ( context: weakSelf. evaluationContext)
283
- if status == . success_with_changes {
274
+ if status == . successWithChanges {
284
275
weakSelf. eventHandler. send ( . configurationChanged)
285
276
}
286
- } catch let err as OfrepError {
287
- switch err {
277
+ } catch let error as OfrepError {
278
+ switch error {
288
279
case . apiTooManyRequestsError:
289
280
weakSelf. eventHandler. send ( . stale)
290
- throw err
281
+ throw error
291
282
default :
292
283
weakSelf. eventHandler. send ( . error)
293
284
}
0 commit comments