-
Notifications
You must be signed in to change notification settings - Fork 16
/
configuration.dart
83 lines (79 loc) · 2.61 KB
/
configuration.dart
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
import 'exceptions/exceptions.dart' show MissingConfiguration;
import 'models/models.dart';
class Configuration {
final String apiKey;
final Set<Node>? nodes;
final Node? nearestNode;
late final int numRetries;
final Duration retryInterval;
final Duration connectionTimeout;
final Duration healthcheckInterval;
final Duration cachedSearchResultsTTL;
final bool sendApiKeyAsQueryParam;
Configuration(
this.apiKey, {
this.nodes,
this.nearestNode,
int? numRetries,
this.retryInterval = const Duration(milliseconds: 100),
this.connectionTimeout = const Duration(seconds: 10),
this.healthcheckInterval = const Duration(seconds: 15),
this.cachedSearchResultsTTL = Duration.zero,
this.sendApiKeyAsQueryParam = false,
}) {
if (apiKey.isEmpty) {
throw MissingConfiguration(
'Ensure that Configuration.apiKey is not empty');
}
if (nodes?.isEmpty ?? false) {
throw MissingConfiguration(
'Ensure that Configuration.nodes is not empty');
}
if (nodes == null && nearestNode == null) {
throw MissingConfiguration(
'Ensure that at least one node is present in Configuration');
}
this.numRetries =
numRetries ?? (nodes?.length ?? 0) + (nearestNode == null ? 0 : 1);
}
/// Returns a new [Configuration] object which differs only in the specified
/// values from this object.
Configuration copyWith({
String? apiKey,
Set<Node>? nodes,
Node? nearestNode,
int? numRetries,
Duration? retryInterval,
Duration? connectionTimeout,
Duration? healthcheckInterval,
Duration? cachedSearchResultsTTL,
bool? sendApiKeyAsQueryParam,
}) =>
Configuration(
apiKey ?? this.apiKey,
nodes: nodes ?? this.nodes,
nearestNode: nearestNode ?? this.nearestNode,
numRetries: numRetries ?? this.numRetries,
retryInterval: retryInterval ?? this.retryInterval,
connectionTimeout: connectionTimeout ?? this.connectionTimeout,
healthcheckInterval: healthcheckInterval ?? this.healthcheckInterval,
cachedSearchResultsTTL:
cachedSearchResultsTTL ?? this.cachedSearchResultsTTL,
sendApiKeyAsQueryParam:
sendApiKeyAsQueryParam ?? this.sendApiKeyAsQueryParam,
);
@override
String toString() => '''
{
Api key: $apiKey
Nodes: $nodes
Nearest node: $nearestNode
Retries: $numRetries
Retry interval: $retryInterval
Connection timeout: $connectionTimeout
Health check interval: $healthcheckInterval
Cached search results Time To Live: $cachedSearchResultsTTL
Send api key in query: $sendApiKeyAsQueryParam
}
''';
}