-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathflagsmith_client.dart
More file actions
587 lines (518 loc) · 17.2 KB
/
flagsmith_client.dart
File metadata and controls
587 lines (518 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
import 'dart:async';
import 'dart:convert';
import 'package:collection/collection.dart' show IterableExtension;
import 'package:dio/dio.dart';
import 'package:flutter_client_sse/constants/sse_request_type_enum.dart';
import 'package:flutter_client_sse/flutter_client_sse.dart';
import 'package:rxdart/subjects.dart';
import '../flagsmith.dart';
import 'version.dart';
/// Flagsmith client initialization
///
/// [config] configuration for http client and endpoints
/// [apiKey] api key for your enviornment
/// [seeds] default values before update from Flagsmith env.
class FlagsmithClient {
static final String authHeader = 'X-Environment-Key';
static final String acceptHeader = 'Accept';
static final String userAgentHeader = 'User-Agent';
static final String applicationJson = 'application/json';
static final String textEventStream = 'text/event-stream';
static final String environmentUpdatedEvent = 'environment_updated';
static final String updatedAtKey = 'updated_at';
void log(String message) {
if (flagsmithDebug) {
// ignore: avoid_print
print('Flagsmith: $message');
}
}
final String apiKey;
final FlagsmithConfig config;
late StorageProvider storageProvider;
CoreStorage? storage;
late Dio _api;
final Map<String, Flag> _flags = {};
final List<Flag> seeds;
Map<String, Flag> get cachedFlags => _flags;
//A map of flag names to the amount of times they have been evaluated in the last 10 seconds
final Map<String, int> flagAnalytics = {};
Timer? _analyticsTimer;
final StreamController<FlagsmithLoading> _loading =
StreamController.broadcast();
Dio get client => _api;
bool flagsmithDebug = false;
late Stream<SSEModel> _sseStream;
double? lastGetFlags;
Identity? cachedUser;
FlagsmithClient(
{this.config = const FlagsmithConfig(),
required this.apiKey,
this.seeds = const <Flag>[],
this.storage}) {
flagsmithDebug = config.isDebug;
_api = _apiClient();
storageProvider = prepareStorage(storage: storage, config: config);
if (config.enableAnalytics) {
_setupAnalyticsTimer(config.analyticsInterval);
}
if (config.enableRealtimeUpdates) {
_setupRealtimeUpdates(config.realtimeUpdatesBaseURI);
}
}
Future<void> _setupAnalyticsTimer(int analyticsInterval) async {
_analyticsTimer?.cancel();
_analyticsTimer = Timer.periodic(
Duration(milliseconds: analyticsInterval), (_) => syncAnalyticsData());
}
Future<Response<dynamic>?> syncAnalyticsData() async {
try {
final valGreaterThan0 =
flagAnalytics.values.firstWhereOrNull((element) => element > 0);
if (valGreaterThan0 != null) {
final res = await _api.post<dynamic>(config.analyticsURI,
data: Map<String, dynamic>.from(flagAnalytics));
if ([200, 201, 202].contains(res.statusCode)) {
flagAnalytics.clear();
}
return res;
}
} on DioException catch (e) {
log('_setupAnalyticsTimer dioError: ${e.error}');
} catch (e) {
log('Exception: _setupAnalyticsTimer $e');
throw FlagsmithException(e);
}
return null;
}
Future<void> _setupRealtimeUpdates(String realtimeUpdatesBaseUrl) async {
final sseUrl = '$realtimeUpdatesBaseUrl$apiKey/stream';
_sseStream = SSEClient.subscribeToSSE(
method: SSERequestType.GET,
url: sseUrl,
header: {
acceptHeader: '$applicationJson , $textEventStream',
},
);
_sseStream.listen(
(event) async {
final shouldGetFlags = await onEventReceived(
event,
lastGetFlags,
);
if (shouldGetFlags) {
await getFeatureFlags(user: cachedUser);
}
},
);
Future.delayed(
Duration(milliseconds: config.reconnectToSSEInterval),
() async {
_setupRealtimeUpdates(config.realtimeUpdatesBaseURI);
},
);
}
Future<bool> onEventReceived(
SSEModel event,
double? lastGetFlags,
) async {
if (event.event != environmentUpdatedEvent) {
return false;
}
double? lastEventUpdate;
try {
final eventData = jsonDecode(event.data ?? '{}') as Map<String, dynamic>;
final updatedAt = eventData[updatedAtKey];
if (updatedAt != null && updatedAt is double) {
lastEventUpdate = updatedAt;
}
} catch (e) {
log('Error parsing JSON: $e');
}
//If the last event update is newer than the last time we got flags,
// then we should update the flags
return (lastEventUpdate ?? 0) > (lastGetFlags ?? 0);
}
static StorageProvider prepareStorage(
{CoreStorage? storage, required FlagsmithConfig config}) {
late CoreStorage store;
if (storage != null) {
store = storage;
} else {
switch (config.storageType) {
case StorageType.custom:
if (storage == null) {
throw FlagsmithConfigException(Exception('When using StorageType.custom, a storage implementation must be provided'));
}
store = storage;
break;
default:
store = InMemoryStorage();
break;
}
}
return StorageProvider(store,
password: config.password, logEnabled: config.isDebug);
}
/// Initialization throught custom init services
///
///
Future<void> initialize() async {
await initStore(seeds: seeds);
}
/// Async initialization
///
/// Returns [FlagsmithClient] after seeds are ready
static Future<FlagsmithClient> init({
FlagsmithConfig config = const FlagsmithConfig(isDebug: true),
required String apiKey,
List<Flag> seeds = const <Flag>[],
CoreStorage? storage,
}) async {
final client = FlagsmithClient(
config: config,
apiKey: apiKey,
seeds: seeds,
storage: storage,
);
await client.initStore(seeds: seeds);
return client;
}
Future<bool> initStore(
{List<Flag> seeds = const <Flag>[], bool clear = false}) async {
if (clear) {
await storageProvider.clear();
}
await storageProvider.seed(items: seeds);
final items = await storageProvider.getAll();
_updateCaches(list: items);
return true;
}
/// Reset
///
/// reseting storage and re-seed defalt values
Future<bool> reset() async {
await storageProvider.clear();
await storageProvider.seed(items: seeds);
_updateCaches(list: seeds);
return true;
}
/// Simple implementation of Http Client
Dio _apiClient() {
var dio = Dio(config.clientOptions)
..options.headers[authHeader] = apiKey
..options.headers[acceptHeader] = applicationJson
..options.headers[userAgentHeader] = getUserAgent()
..options.followRedirects = true;
if (config.isDebug) {
dio.interceptors.add(LogInterceptor(
requestHeader: true,
requestBody: true,
responseBody: true,
));
}
return dio;
}
/// Get a list of existing Features for the given environment and user
///
/// [user] a user in context
/// [traits] a list of user traits
/// [reload] force reload from API
/// Returns a list of feature flags
Future<List<Flag>> getFeatureFlags(
{Identity? user, List<Trait>? traits, bool reload = true}) async {
if (!reload) {
var result = await storageProvider.getAll();
if (result.isNotEmpty) {
result.sort((a, b) => a.feature.name.compareTo(b.feature.name));
}
_updateCaches(list: result);
return result;
}
_loading.add(FlagsmithLoading.loading);
final list =
user == null ? await _getFlags() : await _getUserFlags(user, traits);
_loading.add(FlagsmithLoading.loaded);
return list;
}
/// Check if Feature flag exist and is enabled
///
/// [featureName] an identifier for the feature
/// [user] an identifier for the user
/// [reload] force reload from API
/// Returns true if feature flag exist and enabled, false otherwise
Future<bool> hasFeatureFlag(String featureName,
{Identity? user, bool? reload}) async {
var flag = await _getFlagByName(featureName, user: user, reload: reload);
return flag != null;
}
/// Check if Feature flag exist and is enabled in cache
///
/// [featureName] an identifier for the feature
/// [user] an identifier for the user
/// Returns true if feature flag exist and enabled, false otherwise
///
/// Returns only if [caches] are enabled in config exception otherwise
///
bool hasCachedFeatureFlag(String featureName, {Identity? user}) {
if (!config.caches) {
log('Exception: caches are NOT enabled!');
throw FlagsmithConfigException(Exception('caches are NOT enabled!'));
}
final feature = cachedFlags[featureName];
return feature?.enabled == true;
}
/// Check if Feature flag exist and is enabled
///
/// [featureName] an identifier for the feature
/// [user] an identifier for the user
/// [reload] force reload from API
/// Returns true if feature flag exist and enabled, null otherwise
Future<bool> isFeatureFlagEnabled(String featureName,
{Identity? user, bool? reload}) async {
var flag = await _getFlagByName(featureName, user: user, reload: reload);
return flag?.enabled ?? false;
}
/// Get feature flag value by [featureId] and optionally for a [user]
///
/// [reload] force reload from API
/// Returns String value of Feature Flag
Future<String?> getFeatureFlagValue(String featureId,
{Identity? user, bool? reload}) async {
var flag = await _getFlagByName(featureId, user: user, reload: reload);
return flag?.stateValue;
}
Future<Flag?> _getFlagByName(String featureName,
{Identity? user, bool? reload}) async {
cachedUser = user;
var flags = await getFeatureFlags(user: user, reload: reload ?? false);
var flag = flags
.firstWhereOrNull((element) => element.feature.name == featureName);
_incrementFlagAnalytics(flag);
return flag;
}
/// Get cached feature flag value by [featureId]
/// Returns String value of Feature Flag
String? getCachedFeatureFlagValue(
String featureId,
) {
if (!config.caches) {
log('Exception: caches are NOT enabled!');
throw FlagsmithConfigException(Exception('caches are NOT enabled!'));
}
final feature = cachedFlags[featureId];
_incrementFlagAnalytics(feature);
return feature?.stateValue;
}
/// Internal function for collecting analytical data on flag usage
void _incrementFlagAnalytics(Flag? flag) {
if (flag != null && config.enableAnalytics) {
if (flagAnalytics.containsKey(flag.key)) {
flagAnalytics[flag.key] = flagAnalytics[flag.key]! + 1;
} else {
flagAnalytics[flag.key] = 1;
}
}
}
/// Remove feature flag from storage and caches
Future<bool> removeFeatureFlag(String featureName) async {
var result = await storageProvider.delete(featureName);
if (config.caches) {
_flags.remove(featureName);
}
return result;
}
/// Get user trait for [user] with by [key]
Future<Trait?> getTrait(Identity user, String key) async {
var result = await _getUserTraits(user);
return result.firstWhereOrNull((element) => element.key == key);
}
Future<List<Flag>> _getFlags() async {
try {
var response = await _api.get<List<dynamic>>(config.flagsURI);
if (response.statusCode == 200) {
lastGetFlags = DateTime.now().secondsSinceEpoch;
var list = response.data!
.map<Flag>((dynamic e) => Flag.fromJson(e as Map<String, dynamic>))
.toList();
await storageProvider.saveAll(list);
final saved = await storageProvider.getAll()
..sort((a, b) => a.feature.name.compareTo(b.feature.name));
_updateCaches(list: saved);
return list;
}
return [];
} on DioException catch (e) {
log('_getFlags dioError: ${e.error}');
throw FlagsmithApiException(e);
} catch (e) {
log('Exception: _getFlags $e');
throw FlagsmithException(e);
}
}
// Internal list of [user] flags
Future<List<Flag>> _getUserFlags(Identity user, List<Trait>? traits) async {
try {
cachedUser = user;
var identityData = user.toJson();
if (traits != null && traits.isNotEmpty) {
identityData['traits'] = traits.map((t) => t.toJson()).toList();
}
var response = await _api.post(config.identitiesURI, data: identityData);
if (response.statusCode == 200) {
lastGetFlags = DateTime.now().secondsSinceEpoch;
if (response.data == null) {
return [];
}
var data = FlagsAndTraits.fromJson(response.data!).flags ?? [];
if (data.isNotEmpty) {
data.sort((a, b) => a.feature.name.compareTo(b.feature.name));
}
await storageProvider.saveAll(data);
final saved = await storageProvider.getAll()
..sort((a, b) => a.feature.name.compareTo(b.feature.name));
_updateCaches(list: saved);
return data;
}
return [];
} on DioException catch (e) {
log('_getFlags dioError: ${e.error}');
throw FlagsmithApiException(e);
} catch (e) {
log('Exception: _getFlags $e');
throw FlagsmithException(e);
}
}
/// TRAITS
///
/// Get all `user` traits with `keys`
Future<List<Trait>> getTraits(Identity user, {List<String>? keys}) async {
var result = await _getUserTraits(user);
if (keys == null) {
return result;
}
return result.where((element) => keys.contains(element.key)).toList();
}
/// Internal list of `user` traits
Future<List<Trait>> _getUserTraits(Identity user) async {
try {
cachedUser = user;
var params = {'identifier': user.identifier};
if (user.transient ?? false) {
params['transient'] = 'true';
}
var response = await _api.get<Map<String, dynamic>?>(config.identitiesURI,
queryParameters: params);
if (response.statusCode == 200) {
if (response.data == null) {
return [];
}
return FlagsAndTraits.fromJson(response.data!).traits ?? [];
}
return [];
} on DioException catch (e) {
log('_getFlags dioError: ${e.error}');
throw FlagsmithApiException(e);
} catch (e) {
log('Exception: _getFlags $e');
throw FlagsmithException(e);
}
}
// Update trait for `user` with new value `traits`
Future<TraitWithIdentity?> createTrait(
{required TraitWithIdentity value}) async {
try {
var response =
await _api.post<dynamic>(config.traitsURI, data: value.toJson());
if (response.data == null) {
return null;
}
return TraitWithIdentity.fromJson(response.data as Map<String, dynamic>);
} on DioException catch (e) {
log('_getFlags dioError: ${e.error}');
throw FlagsmithApiException(e);
} catch (e) {
log('Exception: _getFlags $e');
throw FlagsmithException(e);
}
}
/// Bulk update of traits for `user` with list of `value`
Future<List<TraitWithIdentity>?> updateTraits(
{required List<TraitWithIdentity> value}) async {
try {
if (value.isEmpty) {
return null;
}
final identifier = value[0].identity.identifier;
final traitList = value
.map((e) => <String, dynamic>{
'trait_key': e.key,
'trait_value': e.value,
})
.toList();
final data = <String, dynamic>{
'identifier': identifier,
'traits': traitList,
};
var response = await _api.post<dynamic>(
config.identitiesURI,
data: data,
);
if (response.data == null || response.data['traits'] == null) {
return null;
}
final responseData = List<Map<String, dynamic>>.from(
response.data['traits'] as List<dynamic>);
return responseData
.map((e) => TraitWithIdentity(
identity: Identity(identifier: identifier),
key: e['trait_key'] as String,
value: e['trait_value'],
))
.toList();
} on DioException catch (e) {
log('_getFlags dioError: ${e.error}');
throw FlagsmithApiException(e);
} catch (e) {
log('Exception: _getFlags $e');
throw FlagsmithException(e);
}
}
/// Internal updadte caches from list of featurs
void _updateCaches({List<Flag> list = const <Flag>[]}) {
if (!config.caches) {
return;
}
_flags.clear();
_flags.addEntries(list.map((flag) => MapEntry(flag.feature.name, flag)));
}
/// clear all data from storage
Future<bool> clearStore() async {
if (config.caches) {
_flags.clear();
}
return storageProvider.clear();
}
/// stream for listener
Stream<Flag>? stream(String key) => storageProvider.stream(key);
/// basic stream
BehaviorSubject<Flag>? subject(String key) => storageProvider.subject(key);
// Loading from API
Stream<FlagsmithLoading> get loading => _loading.stream;
void close() {
_analyticsTimer?.cancel();
SSEClient.unsubscribeFromSSE();
_loading.close();
}
/// test toggle feature
///
Future<bool> testToggle(String featureName) async {
final result = await storageProvider.togggleFeature(featureName);
if (config.caches) {
final value = await storageProvider.read(featureName);
if (value != null) {
_flags[featureName] = value;
}
}
return result;
}
}