DVHttpHostConfig.fromConfig constructor
Reads one entry of the dartvel.http.hosts block.
Strict, because the generator and the running application both read the
block through this and a key it skipped would be a setting the pubspec
states and nothing applies: retry: for retries: builds, runs, and
retries on the default policy. Anything not understood throws an
ArgumentError naming the key.
Implementation
factory DVHttpHostConfig.fromConfig(String name, Map<String, Object?> map) {
final String at = 'dartvel.http.hosts.$name';
_onlyKeys(at, map, const <String>{
'baseUrl',
'auth',
'headers',
'timeout',
'retries',
'circuitBreaker',
'pool',
});
final Object? baseUrl = map['baseUrl'];
if (baseUrl is! String || baseUrl.isEmpty) {
throw ArgumentError('$at needs a baseUrl.');
}
final Uri? parsed = Uri.tryParse(baseUrl);
if (parsed == null ||
(parsed.scheme != 'https' && parsed.scheme != 'http') ||
parsed.host.isEmpty ||
parsed.hasQuery ||
parsed.hasFragment) {
throw ArgumentError('$at.baseUrl "$baseUrl" is not an absolute http or '
'https URL with no query or fragment.');
}
String? bearer;
final Map<Object?, Object?>? auth = _block(at, map, 'auth');
if (auth != null) {
for (final MapEntry<Object?, Object?> entry in auth.entries) {
if (entry.key == 'bearer') {
if (entry.value is! String || (entry.value! as String).isEmpty) {
throw ArgumentError('$at.auth.bearer names a secret, and '
'"${entry.value}" is not a name.');
}
bearer = entry.value! as String;
} else {
// Refused rather than ignored: an auth scheme that silently does
// nothing sends unauthenticated requests and says nothing about it.
throw ArgumentError(
'$at.auth.${entry.key} is not a supported auth scheme; bearer '
'is.');
}
}
}
DVHttpRetryPolicy retries = const DVHttpRetryPolicy();
final Map<Object?, Object?>? retry = _block(at, map, 'retries');
if (retry != null) {
_onlyKeys('$at.retries', retry,
const <String>{'attempts', 'delay', 'backoff', 'jitter'});
final Object? backoff = retry['backoff'] ?? 'exponential';
if (backoff != 'exponential' && backoff != 'constant') {
throw ArgumentError('$at.retries.backoff "$backoff" is not a backoff; '
'use exponential or constant.');
}
final Object? jitter = retry['jitter'] ?? true;
if (jitter is! bool) {
throw ArgumentError('$at.retries.jitter must be true or false, not '
'"$jitter".');
}
retries = DVHttpRetryPolicy(
attempts: _int('$at.retries.attempts', retry['attempts'], 3, min: 1),
baseDelay: _duration('$at.retries.delay', retry['delay'],
const Duration(milliseconds: 200)),
exponential: backoff == 'exponential',
jitter: jitter,
);
}
DVHttpBreakerPolicy? breaker;
final Map<Object?, Object?>? circuit = _block(at, map, 'circuitBreaker');
if (circuit != null) {
_onlyKeys('$at.circuitBreaker', circuit, const <String>{
'failureRate',
'window',
'cooldown',
'minimumRequests',
});
final Object? rate = circuit['failureRate'] ?? 0.5;
if (rate is! num || rate <= 0 || rate > 1) {
throw ArgumentError('$at.circuitBreaker.failureRate must be a share '
'above 0 and at most 1, not "$rate".');
}
breaker = DVHttpBreakerPolicy(
failureRate: rate.toDouble(),
window: _duration('$at.circuitBreaker.window', circuit['window'],
const Duration(seconds: 30)),
cooldown: _duration('$at.circuitBreaker.cooldown', circuit['cooldown'],
const Duration(seconds: 60)),
minimumRequests: _int('$at.circuitBreaker.minimumRequests',
circuit['minimumRequests'], 5,
min: 1),
);
}
int? maxConcurrent;
final Map<Object?, Object?>? pool = _block(at, map, 'pool');
if (pool != null) {
_onlyKeys('$at.pool', pool, const <String>{'maxConcurrent'});
if (pool['maxConcurrent'] != null) {
maxConcurrent =
_int('$at.pool.maxConcurrent', pool['maxConcurrent'], 0, min: 1);
}
}
final Map<Object?, Object?>? headers = _block(at, map, 'headers');
return DVHttpHostConfig(
baseUrl: baseUrl,
bearerSecret: bearer,
headers: headers == null
? const <String, String>{}
: <String, String>{
for (final MapEntry<Object?, Object?> e in headers.entries)
'${e.key}': '${e.value}',
},
timeout: _duration(
'$at.timeout', map['timeout'], const Duration(seconds: 30)),
retries: retries,
breaker: breaker,
maxConcurrent: maxConcurrent,
);
}